Program Flow 


Contents of this section:

  1. A Simple if Statement
  2. A Simple if-else statement
  3. Nested if-else statement
  4. A Simple for statement
  5. A while Loop
  6. Switch

1. A Simple if Statement

    Code Listing: 

//
// File name: If.cpp
// Purpose:   This program illustrates a simple if statement.
	      It reads in two integers and prints out a
              message on the screen according to their values.
//

#include <iostream>
using namespace std;

int main()
{
  int a, b;

  cout << "Enter first number: ";
  cin >> a;
  cout << "Enter second number: ";
  cin >> b;

  if(a < b) 
	  cout << "First number is less than second.\n";

  return 0;
}

    Running session:

mercury[35]% CC If.cpp -LANG:std -o If
mercury[36]% If
Enter first number: 5
Enter second number: 7
First number is less than second.
mercury[37]% 

2. A Simple if-else statement

Code listing:

//
// File name: IfElse.cpp
// Purpose:   This program illustrates a simple if-else statement.
//

#include <iostream>
using namespace std;

int main()
{
  int a, b;

  cout << "Enter first number: ";
  cin >> a;
  cout << "Enter second number: ";
  cin >> b;

  if(a < b) 
	  cout << "First number is less than second.\n";
  else
	  cout << "First number is greater than or equal to second.\n";

  return 0;

}

Running session:

mercury[43]% CC IfElse.cpp -LANG:std -o IfElse
mercury[44]% IfElse
Enter first number: 6
Enter second number: 3
First number is greater than or equal to second.
mercury[45]% 

3. Nested if-else statement

Code listing:

// Author:    Herbert Schildt
// File name: NestedIf.cpp 
// Purpose:   Generate a random number and let the user guess it.
//

#include <iostream>
// <cstdlib> is needed in order to use the rand().
// For older compilers, use <stdlib.h>
#include <stdlib.h> 
using namespace std;

int main()
{
  int magic;  // magic number
  int guess;  // user's guess

  magic = rand(); // get a random number
  
  cout << "Enter your guess: ";
  cin >> guess;

  if(guess == magic) 
	  // Notice the "==" operator, which compares two values.   
	  cout << "** Right **";
  cout << "The magic number was: " << magic << endl;
  return 0;
}

Running session:

mercury[50]% CC NestedIf.cpp -LANG:std -o NestedIf
mercury[51]% NestedIf
Enter your guess: 879
The magic number was: 16838
mercury[52]% 

 

4. A Simple for Statement

Code Lising

//
// Author: Herbert Schildt
// File name: For.cpp
// Purpose:   Generate the square root of 1 to 10
//
#include <iostream>
#include <math.h> // for newer compilers, use <cmath>
using namespace std;

int main()
{
  int num;
  double sq_root;

  for(num=1; num < 10; num++) {
    sq_root = sqrt((double) num); //casting num from integer to double 
    				  // then taking its square root
    cout << num << "  " << sq_root << '\n';
  }

  return 0;
}

Running Session

mercury[58]% CC For.cpp -LANG:std -o For
mercury[59]% For
1    1
2    1.41421
3    1.73205
4    2
5    2.23606
6    2.44949
7    2.64575
8    2.82842
9    3
mercury[60]% 


5. A while Loop 

Code Listing

// Author:      Herbert Schildt
// Modified by: Qiang Hu
// File name:   NestedIf.cpp 
// Purpose:     Generate a random number between 0 and 9 and let the user 
//              guess it. Use a while loop.  Exit when user guessed right.
//

#include <iostream>
// <cstdlib> is needed in order to use the rand().
// For older compilers, use <stdlib.h>
#include <stdlib.h> 
using namespace std;

int main()
{
  int magic;  // magic number
  int guess;  // user's guess

  cout << "I will come up with a magic number between 0 and 9 ";
  cout << "and ask you to guess it." << endl;

  magic = rand()%10; // get a random number between 0 and 9
  
  cout << "Enter your guess: ";
  cin >> guess;

  while (guess != magic)  // as long as guess is incorrect
  {
	  if(guess > magic)
	  {
	          cout << "Too big! Guess again..." << endl;
	  }
	  else            // guess is less than magic
	  {
		  cout << "Too small! Guess again..." << endl;
	  }
	  cin >> guess;
  }
  cout << "You are RIGHT!" << endl;;
  return 0;
}

Running Session

mercury[69]% CC While.cpp -LANG:std -o While
mercury[70]% While
I will come up with a magic number between 0 and 9 and ask you to guess it.
Enter your guess: 6
Too small! Guess again...
7
Too small! Guess again...
8
You are RIGHT!
mercury[71]% 

6. switch

Code Listing

//
// File name: Switch.cpp
// Purpose:   Demonstrate the use of switch statement
//
#include <iostream>
using namespace std;

int main()
{
	int choice;

	cout << "Enter an integer number: 1 - 5 " ;
	cin >> choice;

	switch (choice)
	{
		case 1:
		   cout << "You entered 1.";
		   break;
		case 2:
		   cout << "You entered 2.";
		   break;
		case 3:
		   cout << "You entered 3.";
		   break;
		case 4:
		   cout << "You entered 4.";
		   break;
		case 5:
		   cout << "You entered 5.";
		   break;
		default:
		   cout << "Invalid input.";
	}

	return 0;

}


Running Session

mercury[75]% CC Switch.cpp -LANG:std -o Switch
mercury[76]% Switch
Enter an integer number: 1 - 5 4
You entered 4.
mercury[77]% 


Last modified: Sunday, 25-Mar-2012 18:34:02 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page