Technology Advances

Program Flow

1. A Simple if Statement

    Code Listing: 


// 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:

 

Enter first number: 5
Enter second number: 7
First number is less than second.
 

 

2. A Simple if-else statement

Code listing:


// 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:

Enter first number: 6
Enter second number: 3
First number is greater than or equal to second.
 

 

3. Nested if-else statement

Code listing:

// 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:

Enter your guess: 879
The magic number was: 16838
 

4. A Simple for Statement

Code Lising

// 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

1 1
2 1.41421
3 1.73205
4 2
5 2.23606
6 2.44949
7 2.64575
8 2.82842
9 3
 

5. A while Loop 

Code Listing

// 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

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!

 

6. switch

Code Listing

// 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

Enter an integer number: 1 - 5 4
You entered 4.