Chapter Three Control Statements Flow control A flow control statement can cause a change in the subsequent controls of flow to differ from the natural sequential order in which the instructions are listed. Flow control roughly categorized into 3. These are: 1. selection statements 2. Iteration/Looping/Repetition statements 3. Jumping statements Selection Statements The program can decide which statements to execute based on a condition. C++ provides several types of selection statements: 1. if statements, 2. if-else statements, 3. nested statements, and 4. switch statements if Statements The syntax for if statement is shown here: if (Boolean_expression) { statement(s); } It executes an action if and only if the condition is true . Example; #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; // checks if the number is positive if (number > 0) { cout << "You entered a positive integer: " << number << endl; } cout << "This statement is always executed."; return 0; } Cont.. if-else Statements An if-else statement decides which statements to execute based on if the condition is true or false. Example; #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; if (number >= 0) { cout << "You entered a positive integer: " << number << endl; } else { cout << "You entered a negative integer: " << number << endl; } cout << "This line is always printed."; return 0;} Cont.. Nested if Statements An if statement can be inside another if statement to form a nested if statement. Example; #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer: "; cin >> num; // outer if condition if (num != 0) { // inner if condition if ((num % 2) == 0) { cout << "The number is even." << endl; } Cont.. // inner else condition else { cout << "The number is odd." << endl; } } // outer else condition else { cout << "The number is 0 and it is neither even nor odd." << endl; } cout << "This line is always printed." << endl; } Cont.. Switch Statements Switch Statements Syntax Example ; / Program to build a simple calculator using switch Statement #include <iostream> using namespace std; int main() { char oper; float num1, num2; cout << "Enter an operator (+, -, *, /): "; cin >> oper; cout << "Enter two numbers: " << endl; cin >> num1 >> num2; switch (oper) { case '+': cout << num1 << " + " << num2 << " = " << num1 + num2; break; Cont.… Cont.… case ‘-’: cout << num1 << " - " << num2 << " = " << num1 - num2; break; case '*': cout << num1 << " * " << num2 << " = " << num1 * num2; break; case '/': cout << num1 << " / " << num2 << " = " << num1 / num2; break; default: // operator is doesn't match any case constant (+, -, *, /) cout << "Error! The operator is not correct"; break; } return 0; } Looping statements it is required to repeatedly evaluate a statement or a whole block of statements with increment/decrement of some data in order to arrive at a result. C++ provides several types of iteration statements: 1. while loop, 2. do … while loop, 3. for loop The while loop Syntax: while (conditional expression) statements; ➢ This implies the statement following it will be carried out as long as the conditional expression evaluates true, i.e. it is more than zero. Cont.… Determine the square roots of numbers 0, 10, 20, 30, and 40. and display them. #include <iostream> #include <cmath> using namespace std; int main(){ int n=10, N, i=0; while(i<5){ N = i*n; cout <<“Number = “ << N <<“\tSquare root = “<< sqrt(N) <<endl; i++; } return 0; } do … while loop The use of do…while loop is similar to while loop except for the fact that the while condition is evaluated at the end of program. So, at least one computation would be carried out even if the while condition turns out false in the execution of do … while loop. Example; / C++ Program to print numbers from 1 to 5 #include <iostream> using namespace std; int main() { int i = 1; / do...while loop from 1 to 5 do { cout << i << " "; ++i; } while (i <= 5); return 0; } Cont.. for loop Previous statement; for ( init; cond; inc /dec ) { statement; } Next statement; Example; // C++ Program to display a text 5 times #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << "Hello World! " << endl; } return 0; } Cont.. break vs. continue The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. Example; / program to print the value of i #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { // break condition if (i == 3) { break; } cout << i << endl; } return 0; } Cont.. Example; // program to print the value of i #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { // condition to continue if (i == 3) { continue; } cout << i << endl; } return 0; } Cont.. Review Exercise 1. Write a program in C++ to check whether a number is prime or not 2. Write a program in C++ to find the sum of the series 1 + 1/22 + 1/33 + ..+ 1/nn. 3. Write a program that will ask the user to input n positive numbers. The program will terminate if one of those number is not positive. 4. Write a program in C++ to display the first n terms of Fibonacci series. Sample Output: Input number of terms to display: 10 Here is the Fibonacci series upto to 10 terms: 0 1 1 2 3 5 8 13 21 34 5. Write a program in C++ to display the pattern like a diamond Sample output Input number of rows (half of the diamond): 5 * *** ***** ******* ********* ******* ***** *** * 6. Write a program in C++ to display Pascal's triangle like pyramid Sample output Input number of rows: 5 1 1 1 1 2 1 1 3 3 1 4 6 4 1 7. Write a program in C++ to find the first and last digit of a number Sample output Input any number: 5679 The first digit of 5679 is: 5 The last digit of 5679 is: 9 Cont..