Statements .

26
PRESENTAT ION OF C++ Made by Indianno va http://indiannova.in

Transcript of Statements .

Page 1: Statements .

http://indiannova.in

PRESENTATION OF C++

Made byIndiannova

Page 2: Statements .

http://indiannova.in

Topic :- “Flow of Control”

We have to discuss following:-

Statements

Statement Flow Control

Selection Statements

Iteration Statements

Jump Statements

Page 3: Statements .

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; : }http://indiannova.in

Page 4: Statements .

http://indiannova.in

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.

Statement

1

Statement

2

Statement

3

Page 5: Statements .

http://indiannova.in

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

Respective statements

• If condition is false Condition • ?

Respective statements

Page 6: Statements .

http://indiannova.in

Condition?

Statement 1

Statement 2

The exit condition

false

The loop body

true

Iteration.

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

Page 7: Statements .

http://indiannova.in

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:

A. If and

B. Switch.

Page 8: Statements .

http://indiannova.in

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: Statements .

http://indiannova.in

‘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: Statements .

http://indiannova.in

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: Statements .

http://indiannova.in

Syntax:- switch(expression)

{case constant 1: c++ statement 1;

break ;

case constant 2:c++ statement 2;

break;

:

:

:

case constant n: c++ statement n;

break;

}

The ‘switch’ StatementThis 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.

Page 12: Statements .

http://indiannova.in

Void main(){ int age;Cout<<”Enter the age”;Cin>>age; If (age>=60){Cout<<”Eligible”;}.else{Cout<<“Not”;}. getch (); }

Examples of Selection Statement

#include<iostream.h>#include<conio.h>Void main(){ int grade;Cout<<”Enter the grade”;Cin>>grade; If (grade>=60){Cout<<”pass”;}. getch ()}

#include<iostream.h>#include<conio.h>

Page 13: Statements .

http://indiannova.in

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>

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>

Ex. Of Nested if Ex. Of Switch

Page 14: Statements .

http:

//in

dian

nova

.in

ITERATION STATEMENTThe 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

Page 15: Statements .

http://indiannova.in

‘For’ LoopFor 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 ; }

1

Page 16: Statements .

http://indiannova.in

‘While’ LoopWhile 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 }

2

Page 17: Statements .

http://indiannova.in

The ‘do-while’ Loop

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

3

Page 18: Statements .

http://indiannova.in

‘Nested’ loopA 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 }

4

Page 19: Statements .

http://indiannova.in

Examples of For and Nested Loop

Void main(){ int i; .. for (i =10;i<=10;i++){Cout<<“\n”<<i;}. getch ();}

#include<iostream.h>#include<conio.h>

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: Statements .

http://indiannova.in

Void main(){ int i=1; .. while (i<=10){cout<<i;}i++;. getch ();}

#include<iostream.h>#include<conio.h>

Void main(){ int i=1; do{cout<<i;}i++; while(i<=10). getch ();}

#include<iostream.h>#include<conio.h>

Example of while and do-while loop

Page 21: Statements .

http://indiannova.in

Jump StatementsC++ has four statements that perform an unconditional branch: return,goto,break and continue . The return and goto statements can be used anywhere in a program. The break and continue statements can be used in conjuction with any of the loop statements.

Page 22: Statements .

http://indiannova.in

break statements

The break statements enables a program to skip over part of the code. A break statement terminates the smallest enclosing while,do while, for and switch statement.

Execution resumes at the statement immediately following the body of the terminated statement.

Page 23: Statements .

http://indiannova.in

continuecontinue is another jump statement like the break statement as both the statements skip over part of the code. But the continue statement is somewhat different from break. Instead of forcing termination, it forces the next iteration of the loop to took place, skipping any code in between.

Page 24: Statements .

HTTP://INDIANNOVA.IN

exit function

It is a pre-defined function defined in header files <process.h> and <stdlib.h>. This function is used to terminate the execution of the program.

Page 25: Statements .

http://indiannova.in

goto statementA goto statement can transfer the program

control anywhere in the program. The target destination of a goto statement is marked by a label. Both of these appear in the same function. The syntax for this statement is

goto label;where label is a valid C++ identifier.

Page 26: Statements .

http://indiannova.in

Presented By:-

Name : Shubham SharmaClass: 11th Section: ‘A’Roll Number : 40

Subject : Computer ScienceTopic :Flow Of Control