Statements
Statement 1 Statement 2 Statement 3
Respective statements If condition is false If condition is false Condition ? Respective statements If condition is true If condition is true
Condition ? Condition ? Statement 1 Statement 2 The exit condition false The loop body true
‘if’ is called single selection structure because it select or ignore a single action. Syntax(general form):- if(condition) { c++ expression or statement ; }
A nested if is an if that has another if in its if’s body or in its else’s body. Syntax :- If(condition ) { c++ expression ; } if(condition ) { c++ expression ; } else { C++ expression ; } else { C++ expression ; } If(condition ) { c++ expression ; } if(condition ) { c++ expression ; } else { C++ expression ; } else { C++ expression ; }
Void main() { int age; Cout<<”Enter the age”; Cin>>age; If (age>=60) { Cout<<”Eligible”; }.else { Cout<<“Not”; }. getch (); } Examples of Selection Statement #include Void main() { int grade; Cout<<”Enter the grade”; Cin>>grade; If (grade>=60) { Cout<<”pass”; }. getch () } #include
Void main() { float a,b,Result; Char ch; Cout<<”Enter the number 1:”; Cout<<”Enter the number 2:”; Cin>>a>>b; Cin>>ch;.. if (ch==‘-’) Result=a-b;.else if (ch==‘+’) Result=a+b;.. else { Cout<<“Wrong entry”; } Cout<<Result;. getch (); } #include Void main() { int grade; Cout<<”Enter the grade in marks:”; Cin>>grade; switch (grade) { Case 90 :cout<<“A”; break; Case 80:cout<<“A”; break; Case 70 :cout<<“A”; break; Case 50 :cout<<“A”; break; Case 40 :cout<<“A”; break; default; } Cout<< “Wrong Entry”;. getch (); } #include Ex. Of Nested if Ex. Of Switch
The iteration statement allows a set of instruction to be perform repeatedly until a certain condition is felled. It is also known as Loop’s. Loops are of four types that is:
Examples of For and Nested Loop Void main() { int i;.. for (i =10;i<=10;i++) { Cout<<“\n”<<i; }. getch (); } #include Void main() { int i,j;.. for (i =1;i<5;i++) Cout<<“\n”; for (j=1;j<i; j++) Cout<<“*”;. getch (); } #include
Void main() { int i=1;.. while (i<=10) { cout<<i; } i++;. getch (); } #include Void main() { int i=1; do { cout<<i; } i++; while(i<=10). getch (); } #include Example of while and do-while loop