we have to discuss following:- statements statement flow control selection statement iteration...

21
PRESENTATION OF C++

Upload: sharlene-mitchell

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

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

Page 1: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

PRESENTATION OF C++

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

Topic :- “Flow of control”

We have to discuss following:- StatementsStatement flow controlSelection statementIteration statement

Page 3: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration 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; : }

Page 4: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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)

Page 5: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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

(fig 2)

Page 6: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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

Page 7: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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.

Page 8: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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

Page 9: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration 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 ; }

Page 10: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration 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 ; }

Page 11: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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

Page 12: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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>

Page 13: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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>

Page 14: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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

Page 15: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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

Page 16: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration 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 }

Page 17: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration 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);

Page 18: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

‘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 }

Page 19: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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>

Page 20: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

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>

Page 21: We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement

Presented By:-

Name : Shakti PrakashClass: 11th Section: ‘B’Roll Number : 11Subject : Computer ScienceTopic :Flow Of Control

rahul
rahul