+ All Categories
Home > Documents > Flow Of Control

Flow Of Control

Date post: 10-Feb-2016
Category:
Upload: talmai
View: 38 times
Download: 0 times
Share this document with a friend
Description:
Flow Of Control. Statements. (I) Statements – Any type of instruction given to computer to perform any kind of action is called Statements. - PowerPoint PPT Presentation
28
Flow Of Control
Transcript
Page 1: Flow Of Control

Flow Of Control

Page 2: Flow Of Control

• (I) Statements – Any type of instruction given to computer to perform any kind of action is called Statements.

• (II) Null statement – It is a useful statement in those where the syntax of the language requires the presence of a statement but where the logic of the program does not.

• ; // it is a null statement• (III) Compound Statnement – It is equivalent to the block. It is treated as a single unit and may appear anywhere in the program where a single statement may appear.

•  (IV) Every statements ends with the ; (semi-colon).

Statements

Page 3: Flow Of Control

• In a program, statements may be executed sequentially, selectively or iteratively.

• Sequence – The sequence construct means the statements are being executed sequentially.

• Every C++ program begins with the first statement of main ( ).

•  Statement 1  Statement 2 Statement 3•   In c++, the program execution starts with the first statement of main () and ends with the last statement of main (). Therefore, the main () is also called driver function as it drives the program.

•  

Statement Flow Control

Page 4: Flow Of Control

• A false value is 0 in c++ and non-zero is considered as true in c++.

• It means the repetition of a set of statements depending upon a condition test. Till the time a condition is true , a set of construction are repeated again and again as soon as the condition become false the repetition stops.

• False (exit)

• True• LOOP BODY

Iteration

Condition?

Statement 1

Statement 2

Page 5: Flow Of Control

• The selection construct means the execution of statements depending upon a condition-test. If a condition is true, a course of action is followed otherwise another course of statement is followed.

• • True Course of

• action

• False• Another course-of-action

Selection Statement

Condition? Statement 1 Statement 2

Statement 1

Statement 2

Page 6: Flow Of Control

• It is also called conditional statements because they allow choosing the set-of-instruction for execution depending upon an expression’s truth value.

•  There are two types of selection statements:

• Syntax of if statement•  If (expression)• Statement;• If statement - An If statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed otherwise another course-of-action is followed.  

• An integer variable’s truth value when it stores a non-zero value and it is false when it stores a zero.

Page 7: Flow Of Control

• There is another form of if statement that is if-else statement. • Syntax• If (expression)• Statement 1;• else • statement 2•

Note : if an if else statement , only the code associated with if or code associated with else executes, never both.

IF ELSE CONDITION

Page 8: Flow Of Control

• The braces { } are not required if only ONE statement follows if or else.• If you put a semicolon after the test condition, the if statement ends there.

The block or statements following are no more part of it in such cases.• i.e.• k=0 ; k=0 ;• if( i==0 ); if( i==0 ) • { { • Cout<<k; cout<<k;• } }• Here semicolon terminates Here if the value i=0 then • the if statement and thus it only it will print value of k doesn’t affect &

will print k otherwise not

Continue

Page 9: Flow Of Control

• A if statement that contain another if statement

• In a nested if statement, a dangling else statement goes with the preceding unmatched if statement.

•  

Nested If

If (expression1){ : If (expresson 2) Statement 1; Else Statement 2;} Body if else;

If (expression1)body of if;else{ : If (expresson 2) Statement 1; Else Statement 2;}

If (expression1){ : If (expresson 2) Statement 1; else Statement 2;}else{ : If (expresson 2) Statement 1; Else Statement 2;}

Page 10: Flow Of Control

• A common programming construct in c++ is the if-else-of ladder, which is often called the if-else-is staircase because of its structure.

• General form;• if (exp1)st1;• else• if(exp2)st2;• else• if(exp3)st3;• : :• else st4;•  The exp are evaluated from top to bottom. As soon as an exp evaluates to

true, the statement associated with it is executed and the rest of the ladder is bypassed. It none exp are true, the final else gets executed. If the final else is missing, no action takes place if all other conditions

The if-else-ladder

Page 11: Flow Of Control

• (4) The alternate to if• if(exp1)• exp2• else (exp3)• can be written as• exp1? exp2: exp3;• it work same as the if statement.• For e.g.• int c; ALTERNATIVE• if (a>b)• c=a;• else int c=a>b?a:b;• c=b;•  

Alternate of If

Page 12: Flow Of Control

Comparing if and?1Compare to if-else sequence,?: offer more concise, clean and compact code, but it is less obvious to it.

2 When ?: operator is used in its nested form, it becomes complex and difficult to understand. 3As compare to ?: if is more flexible as it can have multiple statements, multiple assignments and expressions in its body.

One equal (=) sign is used to assign a value, but TWO equal (=) sign (I.e., ==) are used to check to see if values are equal to one another.

Page 13: Flow Of Control

a multi branch selection statement known as switch.

Syntax of switch case:

  Switch(exp)

{ Case constant 1: st1 ;

break;

Case constant 2: st2 ;

break;

Case constant 3: st3 ;

break;

Case constant 4: st4 ;

break;

 default : st n; }

 

The expression is evaluated and its values are matched against the values of the constant specified in

the case statement . when a match is found, the statement sequence associated with that case is

executed until the break statement.

SWITCH Statement

Page 14: Flow Of Control

• A case statement cannot exist by itself, outside of a switch. The break statement used under switch. When a break statement is encounters in a switch statement, program execution jumps to the line of code following the switch statement i.e., outside the body of switch statement.

• In example after this slide In 1st block program will execute according to user choice but in 2nd block program the program will print all statements because of missing break statement.

•  

Page 15: Flow Of Control

#include<iostream.h>

#include<conio.h>

Void main()

{

clrscr();

int dow;

cout<<” Enter the no. of week day’s”;

cin>>dow;

switch(dow)

{

case 1 : cout<<”\n”<<Sunday”;

break;

case 2 : cout<<”\n”<<Monday”;

break;

case 3 : cout<<”Tuesday”;

break;

case 4 : cout<<”Wednesday”;

break;

default: cout<<”wrong no. of day”;

}

getch();

}

#include<iostream.h>

#include<conio.h>

Void main()

{

clrscr();

int dow;

cout<<” Enter the no. of week day’s”;

cin>>dow;

switch(dow)

{

case 1 : cout<<”\n”<<Sunday”;

case 2 : cout<<”\n”<<Monday”;

case 3 : cout<<”Tuesday”;

case 4 : cout<<”Wednesday”;

default: cout<<”wrong no. of day”;

}

getch();

}

Page 16: Flow Of Control

Switch vs. If-elseSwitch vs. if else

1The switch can only test for equality whereas if can evaluate a relational or logical expression.

2 Switch statement selects its branch by testing the value of same variable whereas the if-else construction lets you a series of expression that may involve unrelated variables and complex expressions.3 The if-else is more versatile for the two statements .4 If-else can handle floating-point tests and characters and integer whereas the switch can handle only integer.5 Switch case label is more constant than if-else statement.6 A switch is more efficient than nested if-else statement.

Page 17: Flow Of Control

• It allow a set of instructions to repeatedly until the condition is fulfilled.

• Three types of loops: for loop, while loop, do-while loop.

• Elemets that control a loop (parts of a loop)•  • Every loop has its elements that control and govern its execution.

Iteration statements

Page 18: Flow Of Control

• Initialization Expression(s) – before entering a loop, its control variable must be initialized.

• The initialization expression(s) is executed only once, in the beginning of the loop.

• Test Expression – It is an expression whose truth values decide whether the loop-body will be executed or not. If the test expression evaluates to true , the loop-body gets executed, otherwise the loop is terminated.

• Update Expression(s) – The update expression(s) change the value of the loop variable. It is executed at the end of the loop after the loop-body is executed.

• The Body-Of-The-Loop – The statements that are executed repeatedly as long as the test-expression is nonzero) form the body of the loop.

Page 19: Flow Of Control

• The for loop is the easiest to understand of the c++ loops.•  Syntax  For (initialization expression(s); test-expression; update

expression(s))• {Body of the loop;• }•  Explains with help of example

•  Initialization Test Update• expression expression expression •  for( i=0 ; i<=10 ; ++i )• { cout<<”\n”<<i;• }

The For Loop

Page 20: Flow Of Control

  Initialization exp is executed i.e., i=0 which gives first value 1 to

variable iThen, test expression is evaluated i.e., i<=10 which results into true

value i.e., 1Since, the test- expression is true the body-of-the-loop i.e., cout<<”\

n”<<I is executed which prints the current value of I on the next line.

After executing the loop-body, the update expression i.e., ++I is executed which increments the value of i.

After the update expression is executed, the test- expression is again evaluates till the sequence of step no. 3 executed, otherwise the loop terminates.

Page 21: Flow Of Control

for ( a=0; a<=5; a++)cout<<a;

In this the loop repeat till the condition is false. And its output is 0 1 2 3 4 5

for (a=0; a<=5; a++);cout<<a;

This loop does not repeat because of the termination of the loop by the semi-colon so it will display 5.

THE FOR LOOP VARIATION 1. MULTIPLE INITIALIZATION AND UPDATE EXPRESSIONS – A FOR LOOP MAY CONTAIN MULTIPLE INITIALIZATION AND/OR MULTIPLE UPDATE EXPRESSIONS. THESE MULTIPLE EXPRESSIONS MUST BE SEPARATED BY THE COMMA.EXAMPLE FOR (I=1, SUM=0; I<=N; SUM+=I, ++I)COUT<<”\N”<<I;☼ THE COMMA OPERATOR CAN SERVE IN A FOR LOOP WHEN YOU NEED MORE THAN ONE INDEX

Page 22: Flow Of Control

2. PREFER PREFIX INCREMENT/DECREMENT OVER POSTFIX TO BE USED ALONE. USE ++I OR --I INSTEAD OF I++ OR I -- BECAUSE WHEN THESE USED ALONE, PREFIX ARE FASTER THAN POSTFIX.1 FOR ( A=0; A<=5; ++A) RATHER THAN, PREFER 1 THAN 2.2 FOR ( A=0; A<=5; A++)  3. OPTIONAL EXPRESSIONS – THE LOOP-CONTROL EXPRESSIONS IN A FOR LOOP STATEMENT ARE OPTIONAL.FOR EXAMPLE;A=0; FOR (; A<=5; A++) IN THIS INITIALIZATION-EXP SKIPED BECAUSE THE EARLY INITIALIZATION OF AN I.E., A=0.4. INFINITE LOOP – AN INFINITE FOR LOOP CAN BE CREATED BY OMITTING THE TEST EXPRESSION.FOR (A=35; ; --A) 5. EMPTY LOOP – IF A FOR LOOP DOESN’T CONTAIN ANY LOOP BODY THAN IT IS AN EMPTY LOOP.6. DECLARACTION OF VARIABLES IN THE LOOP – VARIABLE CANE DECLARE ANYWHERE IN THE PROGRAM.

Page 23: Flow Of Control

IT IS AN ENTRY-CONTROLLED LOOP.THE SYNTAX OF WHILE LOOP ISWHILE (EXPRESSION) LOOP-BODY IN A WHILE LOOP, A LOOP CONTROL VARIABLE SHOULD BE INITIALIZED BEFORE THE LOOP BEGINS AS AN UNINITIALIZED VARIABLE CAN BE USED IN AN EXPRESSION. THE VARIABLE SHOULD BE UPDATED INSIDE THE BODY-OF-THE-WHILE.FOR EXAMPLE: WAP TO CALCULATE THE FACTORIAL OF AN INTEGER.VARIATION IN A WHILE LOOP A WHILE LOOP CAN BE AN EMPTY LOOP OR AN INFINITE LOOP. THE DO-WHILE LOOP- IT IS AN EXIT-CONTROLLED I.E., IT EVALUATES ITS TEST-EXPRESSION AT THE BOTTOM OF THE LOOP AFTER EXECUTING ITS LOOP-BODY STATEMENTS. THIS MEANS THAT A DO-WHILE LOOP ALWAYS EXECUTES AT LEAST ONCE.SYNTAXDO{STATEMENTS;} WHILE (TEST-EXPRESSION); 

The While loop & The Do-While LOOp

Page 24: Flow Of Control

THE JUMP STATEMENTSit unconditionally transfer program control within a function. C++ has four statements that perform an unconditional branch: return,goto,break and continue. The goto statement – it can transfer the program control anywhere in the program. The target destination of a goto statement is marked by a label.   syntax goto label;:label: for example a=0;start:cout<<”\n”<<++a;if (a<50) goto start;it will print no. from 1 to 50.If a label appears just before a closing brace, a null statement must follow the label.

Page 25: Flow Of Control

THE BREAK STATEMENT- IT ENABLES A PROGRAM TO SKIP OVER PART OF THE CODE. A BREAK STATEMENT TERMINATES THE SMALLEST ENCLOSING WHILE, DO-WHILE, FOR OR SWITCH STATEMENTS.

A BREAK STATEMENT SKIPS THE REST OF THE LOOP AND JUMPS OVER TO THE STATEMENT FOLLOWING THE LOOP.A BREAK USED IN A SWITCH STATEMENT WILL AFFECT ONLY THAT SWITCH I.E., IT WILL TERMINATE ONLY THE VERY SWITCH IT APPEARS IN. IT DOES NOT AFFECT ANY LOOP THE SWITCH HAPPENS TO BE IN.

while (test exp){ statement1;if(val>2000)break:statement2;}statement3;

for (int ;expression; update){ statement1;if(val>2000)break;statement2;}statement3;

do{ statement 1;if(val>2000)break;statement2;}while(test expression);statement3;

Page 26: Flow Of Control

THE CONTINUE STATEMENT – INSTEAD OF FORCING TERMINATION, IT FORCES THE NEXT ITERATION OF THE LOOP TO TAKE PLACE, SKIPPING ANY CODE IN BETWEEN. while (test exp){ statement1;if(val>2000)continue;:statement2;}statement3;

for (int ;expression; update){ statement1;if(val>2000)continue;statement2;}statement3;

do{ statement 1;if(val>2000)continue;statement2;}while(test expression);statement3;

The continue statement skips the rest of the loop statements and causes the next iteration of the loop.

Page 27: Flow Of Control

THE EXIT FUNCTION – IT CAUSES THE PROGRAM TO TERMINATE AS SOON AS IT IS ENCOUNTERED, NO MATTER WHERE IT APPEARS IN THE PROGRAM LISTING. THE EXIT FUNCTION HAS BEEN DEFINED UNDER A HEADER FILE PROCESS.H WHICH BE INCLUDED IN A PROGRAM THAT USES EXIT() FUNCTION.

Page 28: Flow Of Control

HARSHDEEP SINGH

XI – B

ROLL NO. 23


Recommended