Download presentation
Presentation is loading. Please wait.
Published byCory Lloyd Modified over 8 years ago
2
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. S KIRAN PGT(COMP) KV CLRI STATEMENT 1 STATEMENT 2 STATEMENT 3 3/21/2016
3
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. S KIRAN PGT(COMP) KV CLRI3/21/2016
4
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. S KIRAN PGT(COMP) KV CLRI3/21/2016
5
SELECTION CONSTRUCT S KIRAN PGT(COMP) KV CLRI COND ITION STATEMENT 1STATEMENT 2 STATEMENT 1 STATEMENT 2 3/21/2016
6
SELECTION CONSTRUCT Different C++ Selection Statements are if statement if… else...construct if … else if … else construct switch statement S KIRAN PGT(COMP) KV CLRI3/21/2016
7
if Statement SYNTAX : if ( condition ) { statement true; } S KIRAN PGT(COMP) KV CLRI3/21/2016
8
if Statement #include void main() { int x = -1; if ( x > 0 ) { cout << "x is a positive number"; } getch(); } S KIRAN PGT(COMP) KV CLRI3/21/2016
9
if … else… construct if ( condition ) { statement true; } else { statement false; } S KIRAN PGT(COMP) KV CLRI3/21/2016
10
If … else… construct #include void main() { int x = -1; if ( x > 0 ) { cout << "x is a positive number"; } else { cout << "x is a negative or zero"; } getch(); } S KIRAN PGT(COMP) KV CLRI3/21/2016
11
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 } S KIRAN PGT(COMP) KV CLRI3/21/2016
12
if…else if … else #include 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; } S KIRAN PGT(COMP) KV CLRI3/21/2016
13
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 S KIRAN PGT(COMP) KV CLRI3/21/2016
14
Program 2 #include if(ch=='+') #include result = a+b; void main()else if(ch=='-') {result = a-b; char ch;else if(ch=='*') float a, b, result;result = a*b; clrscr();else if(ch=='/') cout<<"Enter two numbers";result = a/b; cin>>a>>b;else cout<<" \n Enter the operator(+,-,/,*); cout<<"Invalid Operator“ cin>>ch; cout<<"\n The Calculated result is = "<<result; } S KIRAN PGT(COMP) KV CLRI3/21/2016
15
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 S KIRAN PGT(COMP) KV CLRI AVERAGEGRADE > 90A > 75B > 60C > 40D < 40E 3/21/2016
16
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? S KIRAN PGT(COMP) KV CLRI3/21/2016
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.