You are on page 1of 5

Prepared By : Rakesh Kumar

FLOW OF CONTROL
Flow of control refers to the order in which program statements are performed. The order of execution of program statement can be controlled using following statement 1. if statement 2. if else statement 3. nested if statement 4. switch statement 5. do while statement 6. for statement 7. break statement 8. continue. 1. IF statement : it is a selection statement, i,e an associated statement execute if the associated condition is true. The syntax is as follows if ( Condition) if Syntax statement; Where : if :Keyword Condition :Any Arithmetic/Logical/Relational Expression statement : Any C/C++ valid single /compound statement Example
#include<iostream> #include<conio.h> using namespace std; int main() { int age; cout<<"\n Enter age :"; cin>>age; if(age>=18) cout<<"\n You are eligible for vote"; getch(); return 0; } #include<iostream> #include<conio.h> using namespace std; int main() { int a,b,c,lar; cout<<"\n Enter value of a,b and C :"; cin>>a>>b>>c; lar =a; if(lar<b) lar =b; if(lar<c) lar=c; cout<<"\n Largest no :"<<lar; getch(); return 0;

2. IF ELSE STATEMENT: This is also a selection statement , just like if , but here an action is also followed when associated condition is false. i,e in both case , it follow a certain course of action. if ( condition) If else syntax statement1; else statement2; Where if, else : Keywords Condition : Any Arithmetic /Logical/relational Expression statement1,statement2 : Any valid C/C++ single or compound statement Example of ( if- else statement )

Prepared By : Rakesh Kumar 3. Nested If( embedded if ) : An if statement can be placed inside another if or if else statement, There is no syntax or guidelines for nested if . it is totally govern by the requirement . Example of Nested if statement 4 Switch Statement :

switch(condition) { Switch Syntax case const1: statement1: break; case const2: statement2; break; . .. case const n: statement n; break; default: statement; } The condition in switch case statement always results in either integer / char type values. switch statement can not evaluate floating or string type values. Statement inside switch can not be written without case. Switch also do not supports two constant with same values. Example of switch statement
#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int day; cout<<"\n Enter day number of a day in a week :"; cin>>day; switch(day) { case 1: cout<<"\n Monday"; break; case 2: cout<<"\n Tuesday"; break; case 3: cout<<"\n Wednesday"; break; case 4: cout<<"\n Thursday"; break; case 5: cout<<"\n Friday"; break; case 6: cout<<"\n Saturday"; break; case 7: cout<<"\n Sunday"; break; default: cout<<"\n Wrong Choice.... Try again"; } getch(); return 0; }

Prepared By : Rakesh Kumar 4. Do While Statement : This is also called post test loop, i,e the associated statement execute first and then the associated condition is checked. do { Do while loop syntax statement; }while(condition); Example of Do while loop
#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; x=0; do { x=x+1; cout<<setw(6)<<x; }while(x<100); getch(); return 0; } #include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int row,col; row=0; do { row++; col=0; do { col++; cout<<setw(6)<<col; }while(col<row); cout<<endl; }while(row<5); getch(); return 0;

5. While Statement : This is also called Entry Controlled Loop. it means firstly condition is checked and if the condition is true then the associated statement run. While( condition) While Loop Syntax statement ;
#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int row,col; row=0; while(row<5) { row++; col=0; while(col<row) { col++; cout<<setw(6)<<col; } cout<<endl; } getch(); return 0; }

6. For loop : This loop is basically used when the no. of repetition is exactly known to you. The syntax is as follows for(initialization; condition; increment/decrement)

Prepared By : Rakesh Kumar { Statement; } OR Initialization; for( ; condition ; increment/decrement ) { statement; } OR initialization; for( ; condition ;) { statement; increment/decrement; } ORDER OF EXECUTION: for(initialization ; condition ; increment / decrement) { statement; } Example of for loop statement
// using third syntax #include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; x=0; for(;x<100;) { x= x+1; cout<<setw(6)<<x; } getch(); return 0; } II Version #include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; for(x=0;x<100;) { x= x+1; cout<<setw(6)<<x; } getch(); return 0; }

I- Version
#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; for(x=1;x<=100;x=x+1) { cout<<setw(6)<<x; } getch(); return 0; }

Break Statement: Break statement is a unconditional statement, it is used to terminate any loop abnormally. and send the control , at the very first statement following that loop. Syntax break; Break statement can be used as a unconditional statement as well as a conditional statement
// inside do while loop #include<iostream> #include<iomanip>

Example of Break as a Unconditional statement // inside while loop // in for loop


#include<iostream> #include<iomanip> #include<iostream> #include<iomanip>

Prepared By : Rakesh Kumar


#include<conio.h> using namespace std; int main() { int x; x=0; do { x= x+1; break; cout<<x; }while(x<100); getch(); return 0; } #include<conio.h> using namespace std; int main() { int x; x=0; while(x<100) { x= x+1; if(x>50) break; cout<<x; } getch(); return 0; } #include<conio.h> using namespace std; int main() { int x; for(x=1;x<=100;x++) { cout<<x; break; x++; } getch(); return 0; }

Result : No output

Result : 1

Result : 1,2,3,.49

Continue Statement : This statement let control skip the remaining part of the loop and directly jump on the conditional checking statement. Continue statement can be used as conditional as well as unconditional statement. Syntax continue; In do while loop
#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; x=0; do { x= x+1; if(x%2==0) continue; cout<<setw(6)<<x; }while(x<100); getch(); return 0; }

Example of Continue statement In While loop


#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; x=0; while(x<100) { x= x+1; if(x%2!=0) continue; cout<<setw(6)<<x; } getch(); return 0; }

For loop
#include<iostream> #include<iomanip> #include<conio.h> using namespace std; int main() { int x; for(x=1;x<=100;x++) { cout<<x; continue; x++; } getch(); return 0; }

Result :1,3,5,797,99

Result :2,4,6,8,10..98,100

Result: 1 2 3 4 5 6 7 8 99 100

You might also like