Upload
hitesh-wagle
View
203
Download
7
Embed Size (px)
Citation preview
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 statement2. if else statement3. nested if statement4. switch statement5. do while statement6. for statement7. break statement8. 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) statement;Where :
if :KeywordCondition :Any Arithmetic/Logical/Relational Expressionstatement : 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)
statement1;else
statement2;Where
if, else : KeywordsCondition : Any Arithmetic /Logical/relational Expression
statement1,statement2 : Any valid C/C++ single or compound statement
Example of ( if- else statement )
if Syntax
If else syntax
Prepared By : Rakesh Kumar3. 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){
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;}
Switch Syntax
Prepared By : Rakesh Kumar4. 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 { 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) 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)
Do while loop syntax
While Loop Syntax
Prepared By : Rakesh Kumar {
Statement;}
ORInitialization;
for( ; condition ; increment/decrement ) {
statement; }
ORinitialization;
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.
Syntaxbreak;
Break statement can be used as a unconditional statement as well as a conditional statement
Example of Break as a Unconditional statement// inside do – while loop
#include<iostream>#include<iomanip>
// inside while loop
#include<iostream>#include<iomanip>
// in for loop
#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;}
Result : No output
#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;}
Result : 1,2,3,…………….49
#include<conio.h>using namespace std;int main(){ int x; for(x=1;x<=100;x++) { cout<<x;
break; x++; }
getch(); return 0;}
Result : 1
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.
Syntaxcontinue;
Example of Continue statement 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;}
Result :1,3,5,7…………97,99
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;}
Result :2,4,6,8,10………..98,100
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 2 3 4 5 6 7 8 …99 100