Presentation is loading. Please wait.

Presentation is loading. Please wait.

We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.

Similar presentations


Presentation on theme: "We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement."— Presentation transcript:

1

2 We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

3 Statements are the instruction given to the computers to perform any kind of action.  Compound Statement(Block) A compound statement in C++ is a sequence of statement enclosed by pair of braces{ }. For instance, { statement1; statement2; : }

4 In program, statement may be executed sequently, selectively or iteratively. Sequence. The sequence construct means the statement are being executed sequentially. This represent the default flow of statement (see fig 1) (fig 1)

5 Selection.The selection construct means the execution of statement(s) depending upon a condition test. (see fig2) (fig 2)

6 Iteration. The iteration construct means repetition of set-of – statement depending upon condition-test.

7 The selection statement allow to choose the set-of-instruction for execution depending upon an expression truth value.C++ provide two type of selection statement: 1. If and 2. Switch.

8 ‘if’ is called single selection structure because it select or ignore a single action. Syntax(general form):- if(condition) { c++ expression or statement ; }

9 This type of structure is called double selection structure because it select between two different action. If the action is true that it will print true part and if action is false then it will print false part. Syntax:- if(condition) { c++ expression or statement ; } else { c++ expression or statement ; }

10 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 ; }

11 This selection statement successively test the value of an expression against a list of integer or character constant. When a match is found, the statement associated with that constant are executed. Syntax:- switch(expression) {case constant 1: c++ statement 1; break ; case constant 2:c++ statement 2; break; : case constant n: c++ statement n; break; }

12 Example of if Void main() { int grade; Cout<<”Enter the grade”; Cin>>grade; If (grade>=60) { Cout<<”pass”; }. getch () } #include Example of if -else Void main() { int age; Cout<<”Enter the age”; Cin>>age; If (age>=60) { Cout<<”Eligible”; }.else { Cout<<“Not”; }. getch (); } #include

13 Example of nested-if 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 Example of switch 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

14 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: For loop While loop Do-while loop Nested loop

15 1)For loop is easiest to understand of the loops. All its loop-control element are gathered in one place, while in other loop construction of c++,they are scattered about the program. Syntax :- for (initialization expression(s); test-expression; update expression(s)) { c++ statement ; }

16 2)While loop is an entry controlled loop. The loop iterates while the expression evaluates to true. When expression become false, the program control passes to the line after the loop-body code. Syntax :- while (condition) { c++ statement }

17 3)The do-while loop is an exit-controlled loop i.e., it evaluate its test expression at the bottom of the loop after executing its loop-body statement. It means that a do-while loop always execute at least once. Syntax:- do { c++ statement; } while(test-expression);

18 4)A loop may contain another loop in its body. This form is known as nested loop. Syntax :- for (initialization expression 1; test-expression 1; update expression 1) { c++ statement ; //outer loop for (initialization expression 2; test-expression 2; update expression 2) c++ statement; //inner loop }

19 Example of for loop Void main() { int i;.. for (i=10;i<=10;i++) { Cout<<“\n”<<i; }. getch (); } #include Example of nested loop Void main() { int i,j;.. for (i=1;i<5;i++) Cout<<“\n”; for (j=1;j<i;j++) Cout<<“*”;. getch (); } #include

20 Example of while loop Void main() { int i=1;.. while (i<=10) { Cout<<i; } i++;. getch (); } #include Example of do-while loop Void main() { int i=1; do { Cout<<i; } i++; while(i<=10). getch (); } #include

21 Presented By:- Name : Shakti Prakash Class: 11 th Section: ‘B’ Roll Number : 11 Subject : Computer Science Topic :Flow Of Control


Download ppt "We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement."

Similar presentations


Ads by Google