we have to discuss following:- statements statement flow control selection statement iteration...
DESCRIPTION
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; : }TRANSCRIPT
PRESENTATION OF C++
Topic :- “Flow of control”
We have to discuss following:- StatementsStatement flow controlSelection statementIteration statement
Statements
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; : }
Statement flow control
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)
Selection .The selection construct means the execution of statement(s) depending upon a condition test. (see fig2)
(fig 2)
Iteration. The iteration construct means repetition of set-of –statement depending upon condition-test.
Selection Statement
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 and2. Switch.
a. The ‘if’ Statement of C++ ‘if’
‘if’ is called single selection structure because it select or ignore a single action.Syntax(general form):- if(condition) { c++ expression or statement ; }
‘if else’
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 ; }
b. Nested ‘ifs’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 ; }
2. The ‘switch’ Statement
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; }
Examples of Selection Statement
Example of if
Void main(){ int grade;Cout<<”Enter the grade”;Cin>>grade; If (grade>=60){Cout<<”pass”;}. getch ()}
#include<iostream.h>#include<conio.h>
Example of if -else
Void main(){ int age;Cout<<”Enter the age”;Cin>>age; If (age>=60){Cout<<”Eligible”;}.else{Cout<<“Not”;}. getch ();}
#include<iostream.h>#include<conio.h>
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<iostream.h>#include<conio.h>
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<iostream.h>#include<conio.h>
Iteration Statement
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 loopDo-while loopNested loop
‘For’ Loop
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 ; }
‘While’ Loop
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 }
The ‘do-while’ Loop
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);
‘Nested’ loop
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 loopfor (initialization expression 2; test-expression 2; update expression 2) c++ statement; //inner loop }
Examples of Iteration Statement
Example of for loop
Void main(){ int i; .. for (i=10;i<=10;i++){Cout<<“\n”<<i;}. getch ();}
#include<iostream.h>#include<conio.h>
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<iostream.h>#include<conio.h>
Example of while loop
Void main(){ int i=1; .. while (i<=10){Cout<<i;}i++;. getch ();}
#include<iostream.h>#include<conio.h>
Example of do-while loop
Void main(){ int i=1; do{Cout<<i;}i++; while(i<=10). getch ();}
#include<iostream.h>#include<conio.h>
Presented By:-
Name : Shakti PrakashClass: 11th Section: ‘B’Roll Number : 11Subject : Computer ScienceTopic :Flow Of Control