FLOW OF CONTROL FLOW OF CONTROL In C
















- Slides: 16

FLOW OF CONTROL

FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called sequential execution of program. STATEMENT 1 STATEMENT 2 STATEMENT 3 1/15/2022 S KIRAN PGT(COMP) KV CLRI

FLOW OF CONTROL However, often this is not what we desire. For example, if we ask the user to make a selection, and the user enters an invalid choice, ideally we’d like to ask the user to make another choice. This is not possible in a sequential execution of program. Fortunately, C++ provides control flow statements (also called flow control statements), which allow the programmer to change the Compiler’s path through the program. There are quite a few different types of control flow statements. 1/15/2022 S KIRAN PGT(COMP) KV CLRI

FLOW OF CONROL STATEMENT FLOW CONTROL : SELECTION CONSTRUCT Here execution of statements depends upon a condition-test. If a condition test evaluates to true a set of statements is executed else another set of statements is executed. 1/15/2022 S KIRAN PGT(COMP) KV CLRI

SELECTION CONSTRUCT COND ITION STATEMENT 1 STATEMENT 2 1/15/2022 S KIRAN PGT(COMP) KV CLRI STATEMENT 1 STATEMENT 2

SELECTION CONSTRUCT Different C++ Selection Statements are if statement if… else. . . construct if … else construct switch statement 1/15/2022 S KIRAN PGT(COMP) KV CLRI

if Statement SYNTAX : if ( condition ) { statement true; } 1/15/2022 S KIRAN PGT(COMP) KV CLRI

if Statement #include <iostream. h> void main() { int x = -1; if ( x > 0 ) { cout << "x is a positive number"; } getch(); } 1/15/2022 S KIRAN PGT(COMP) KV CLRI

if … else… construct if ( condition ) { statement true; } else { statement false; } 1/15/2022 S KIRAN PGT(COMP) KV CLRI

If … else… construct #include <iostream. h> void main() { int x = -1; if ( x > 0 ) { cout << "x is a positive number"; } else { cout << "x is a negative or zero"; } getch(); } 1/15/2022 S KIRAN PGT(COMP) KV CLRI

if…else if … else if ( condition-1 ) { statement; // condition-1 is true } else if ( condition-2 ) { statement; // condition-2 is true } else if ( condition-3 ) { statement; // condition-3 is true } else { // default case: statement; // all above conditions were false } 1/15/2022 S KIRAN PGT(COMP) KV CLRI

if…else if … else #include <iostream. h> int main() { int x = -1; if ( x > 0 ) { cout << "x is positive"; } else if ( x < 0 ) { cout << "x is negative"; } else { cout << "x is zero"; } return 0; } 1/15/2022 S KIRAN PGT(COMP) KV CLRI

if…else if … else Example 2 : Program to create the equivalent of four-function calculator. The program requires the user to enter two numbers and an operator. It then carries the specified arithmetical operation: addition, subtraction, multiplication or division of two numbers. Finally display the results 1/15/2022 S KIRAN PGT(COMP) KV CLRI

Program 2 #include<iostream. h> #include<conio. h> void main() { char ch; float a, b, result; clrscr(); cout<<"Enter two numbers"; cin>>a>>b; cout<<" n Enter the operator(+, -, /, *); cin>>ch; 1/15/2022 if(ch=='+') result = a+b; else if(ch=='-') result = a-b; else if(ch=='*') result = a*b; else if(ch=='/') result = a/b; else cout<<"Invalid Operator“ cout<<"n The Calculated result is = "<<result; } S KIRAN PGT(COMP) KV CLRI

Program Write a program to accept student information like, name, rollno, marks in 3 subjects and calculate the total, average and grade. Grade is calculated as 1/15/2022 AVERAGE GRADE > 90 A > 75 B > 60 C > 40 D < 40 E S KIRAN PGT(COMP) KV CLRI

RECAPITULATION What is sequential Execution of a program? When do we make use of selection statements? What are the different types of selection statements available? 1/15/2022 S KIRAN PGT(COMP) KV CLRI