0% found this document useful (0 votes)
43 views

Program Flow: Contents of This Section

This document provides examples of different types of program flow control in C++ including if statements, if-else statements, nested if-else statements, for loops, while loops, and switch statements. Each example includes sample code and output to demonstrate how each statement works. The examples cover basic conditional logic, looping through a range of numbers, prompting a user for input in a loop until a condition is met, and selecting different code paths based on an integer value.

Uploaded by

muddasaryamin
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Program Flow: Contents of This Section

This document provides examples of different types of program flow control in C++ including if statements, if-else statements, nested if-else statements, for loops, while loops, and switch statements. Each example includes sample code and output to demonstrate how each statement works. The examples cover basic conditional logic, looping through a range of numbers, prompting a user for input in a loop until a condition is met, and selecting different code paths based on an integer value.

Uploaded by

muddasaryamin
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

`

Program Flow
Contents of this section:
1. 2. 3. 4. 5. 6. A Simple if Statement A Simple if-else statement Nested if-else statement A Simple for statement A while Loop 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; int guess;

// magic number // 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: // Modified by: // File name: // Purpose: // right. // Herbert Schildt Qiang Hu NestedIf.cpp Generate a random number between 0 and 9 and let the user guess it. Use a while loop. Exit when user guessed

#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; int guess;

// magic number // 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 << break; case 2: cout << break; case 3: cout << break; case 4: cout << break; case 5: cout << break; default: cout << } return 0;

"You entered 1."; "You entered 2."; "You entered 3."; "You entered 4."; "You entered 5."; "Invalid input.";

You might also like