c++ programming unit 5 flow of control

33
C++ Language By:-AAKASH KAUSHIK #9289817971, 98919893083 [email protected]

Upload: aakash-kumar

Post on 13-Jan-2017

1.089 views

Category:

Education


2 download

TRANSCRIPT

Page 1: C++ programming Unit 5 flow of control

C++ LanguageBy:-AAKASH KAUSHIK#9289817971, [email protected]

Page 2: C++ programming Unit 5 flow of control

UNIT -5FLOW OF CONTROL

Page 3: C++ programming Unit 5 flow of control

FLOW OF CONTROLIT REFERS TO THE FLOW IN WHICH A PROGRAM EXECUTION TAKES PLACE.

AAKASH KAUSHIK 9891983083,9289817971

Page 4: C++ programming Unit 5 flow of control

FLOW OF CONTROL1.)SequentialIN this each statement of a program is executed sequentially i.e, one after another

2.)Conditional/Decision Making

3.)Iterations/Looping

AAKASH KAUSHIK 9891983083,9289817971

Page 5: C++ programming Unit 5 flow of control

CONDITIONS

Page 6: C++ programming Unit 5 flow of control

IFIF-ELSEELSE IF LADDERNESTED IFSWITCH CONDITIONAL OPERATOR

TYPE OF CONDITIONS

AAKASH KAUSHIK 9891983083,9289817971

Page 7: C++ programming Unit 5 flow of control

It takes a condition in parenthesis and a code block .If the condition will results into true the code block will execute or if the condition will false the code block will be skipped.

The general form of a simple if statement is

if (test expression){statement-block;}statement-x;

IF CONDITION

AAKASH KAUSHIK 9891983083,9289817971

Page 8: C++ programming Unit 5 flow of control

It is the extension of simple if statement. It contains an additional else block also. If the condition will results into true the if code block will execute or if the condition will false the else code block will execute.if (test expression){True-block statement(s)}else{False-block statement(s)}statement-x

IF ELSE CONDITION

AAKASH KAUSHIK 9891983083,9289817971

Page 9: C++ programming Unit 5 flow of control

EXAMPLE #include<iostream.h>#include<conio.h> void main() { int num;cout<<"Enter any number : ";cin>>num; if(num>0) cout<<num<<" is Positive."; else cout<<num<<" is Negative."; }

AAKASH KAUSHIK 9891983083,9289817971

output

Page 10: C++ programming Unit 5 flow of control

There is another way of putting ifs together when multiple decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form:if (condition 1)statement 1 ;else if (condition 2)statement 2; else if (condition n)statement n;elsedefault statement;statement x;

THE ELSE IF LADDER

AAKASH KAUSHIK 9891983083,9289817971

Page 11: C++ programming Unit 5 flow of control

If the condition-1 is true statement-1 will be executed; otherwise it continues to perform the other test condition-2 then condition-3 and so onUntil any condition results into true otherwise by default else block i.e, default-statement will get executed and control will be transferred to statement –X.

The else block is Optional and Default.

THE ELSE IF LADDER

AAKASH KAUSHIK 9891983083,9289817971

Page 12: C++ programming Unit 5 flow of control

EXAMPLE#include<iostream.h>#include<conio.h> void main() { int amt,discount; cout<<"Enter amount : "; cin>>amt; if(amt>20000) discount=15; else if(amt>15000) discount=10; else if(amt>10000) discount=5; else discount=0; cout<<"You will get "<<discount<<"% discount."; }

output

Page 13: C++ programming Unit 5 flow of control

Nested if also known as if within if or condition within condition. When a series of decisions are involved, we may have to use more than one if...else statements in nested form i.e., body of an if or else part will also be a condition(if..else).

NESTED IF

AAKASH KAUSHIK 9891983083,9289817971

Page 14: C++ programming Unit 5 flow of control

if (test condition1){

if (test condition 2){statement-1;} else{ statement-2;}

} else{ statement-3;}statement-x;

NESTED IF

AAKASH KAUSHIK 9891983083,9289817971

If condition-1 will true then its body will execute and condition-2 is tested if it will true statement-1 will execute otherwise statement-2 will execute or if condition-1 will false then as a result statement-3 will execute and in all the cases control will be transferred to statement-X.

Page 15: C++ programming Unit 5 flow of control

AAKASH KAUSHIK 9891983083,9289817971

EXAMPLE#include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<"\nEnter value of A ,B,C "; cin>>a>>b>>c; if(a>b) {

if(a>c) cout<<"\n\nA is Greatest"; else cout<<"\n\nC is Greatest";

} else {

if(b>c) cout<<"\n\nB is Greatest"; else cout<<"\n\nC is Greatest";

} }

output

Page 16: C++ programming Unit 5 flow of control

C++ Provides a multiple-branch selection statement known as SWITCH. This selection statement tests the value of an expression against a list of integer or character constants. When a match is found, the statement associated with that constant are executed if no match is found then the default statement gets executed.

THE SWITCH STATEMENT

AAKASH KAUSHIK 9891983083,9289817971

Page 17: C++ programming Unit 5 flow of control

switch(expression){case constant 1: statement sequence 1;

break;case constant 2: statement sequence 2; break;case constant 3: statement sequence 3;

break;case constant n-1: statement sequence n-1;

break;default : statement sequence n;}//default statement is optional

SYNTAX:-

AAKASH KAUSHIK 9891983083,9289817971

Page 18: C++ programming Unit 5 flow of control

AAKASH KAUSHIK 9891983083,9289817971

#include<iostream.h>#include<conio.h>void main(){clrscr();int dow;cout<<"Enter weekday number(1-7)";cin>>dow;switch(dow){case 1:cout<<"MONDAY";

break;case 2:cout<<"TUESDAY";

break;case 3:cout<<"WEDNESDAY";

break;case 4:cout<<"THURSDAY";

break;case 5:cout<<"FRIDAY";

break;case 6:cout<<"SATURDAY";

break;case 7:cout<<"SUNDAY";

break;default:cout<<"wrong number of day";

break;}getch();}

Example of switch

output

Page 19: C++ programming Unit 5 flow of control

The break statement used under switch is one of C++ jump statements . whenever a case constant is matched with expression in the switch that particular part starts execution and lasts until any break statement is encountered or until the end of switch. Due to break statements the program execution jumps to line following the switch i.e., outside the body of the switch statement otherwise it will execute all the statements following the matched case in the switch.

Usage of break in switch

AAKASH KAUSHIK 9891983083,9289817971

Page 20: C++ programming Unit 5 flow of control

#include<iostream.h>#include<conio.h>void main(){clrscr();int num;cout<<“enter any num”;cin>>num;switch(num){case 1:cout<<“One\n”;case 2:cout<<“Two\n”; break;case 3:cout<<“Three\n”;case 4:cout<<“Four\n”; break;default:cout<<“wrong entry”;}getch();}

Example of Usage of break in switch

output

Page 21: C++ programming Unit 5 flow of control

Switch v/s if-else..If else..It can handle logical or relational expressions i.e., multiple conditions.If else is more versatile as it can handle ranges.If else can work upon int , char & floating point data also.If else construction lets you use a series of expressions that may involve unrelated variables

SwitchSwitch can only test for equalitySwitch case label must be only a single value.Switch works only with int & char data types.The switch statement selects its branches by testing the value of same variable(against a set of constants)

AAKASH KAUSHIK 9891983083,9289817971

Page 22: C++ programming Unit 5 flow of control

Switch v/s if-else..If else..It can handle logical or relational expressions i.e., multiple conditions.If else is more versatile as it can handle ranges.If else can work upon int , char & floating point data also.If else construction lets you use a series of expressions that may involve unrelated variables

SwitchSwitch can only test for equalitySwitch case label must be only a single value.Switch works only with int & char data types.The switch statement selects its branches by testing the value of same variable(against a set of constants)

AAKASH KAUSHIK 9891983083,9289817971

Page 23: C++ programming Unit 5 flow of control

ITERATIONS/LOOPING

Page 24: C++ programming Unit 5 flow of control

ITERATIONS/LOOPING

AAKASH KAUSHIK 9891983083,9289817971

Same block/set of statements is executed repeatedly (again & again) until a specific condition is fulfilled.

Page 25: C++ programming Unit 5 flow of control

PARTS OF LOOP

AAKASH KAUSHIK 9891983083,9289817971

1.)Initialization Expression- It is the initialization Statement of the Control variable( variable used to control the loop) .i.e., The value from which loop will start it’s execution2.)Test Expression/Condition - the condition up to which loop will keep on execution. If the condition results true loop-body will execute again otherwise the loop is terminated3.)Updation- the loop variable is incremented or decremented according to the needs for which the next iteration will be performed.4.)Body of loop- Code Block/Statements need to be executed repeatedly until the conditions results true.

Page 26: C++ programming Unit 5 flow of control

FOR loopWHILE loopDo-WHILE loopNested loops

Type of LOOPS

AAKASH KAUSHIK 9891983083,9289817971

Page 27: C++ programming Unit 5 flow of control

Syntax:-for(intializtion ; condition ; updation){- - - - - - - - - - - - - - - }

FOR LOOP

AAKASH KAUSHIK 9891983083,9289817971

BODY OF LOOP

Page 28: C++ programming Unit 5 flow of control

FOR LOOP EXECUTION SEQUENCE

AAKASH KAUSHIK 9891983083,9289817971

INTIALIZATIONCONDITION CHECKING

TERMINATELOOP

BODYEXECUTION

UPDATION

FALSE

TRUE

START

Page 29: C++ programming Unit 5 flow of control

Syntax:-InitializationWhile(condition){- - - - -- - - - - - - - - -}

WHILE LOOP

AAKASH KAUSHIK 9891983083,9289817971

BODY OF LOOP +

UPDATION

Page 30: C++ programming Unit 5 flow of control

WHILE LOOP EXECUTION SEQUENCE

AAKASH KAUSHIK 9891983083,9289817971

INTIALIZATIONCONDITION CHECKING

TERMINATELOOPFALSE

TRUE

START

BODYEXECUTION

+UPDATION

Page 31: C++ programming Unit 5 flow of control

Syntax:-Initializationdo{- - - - -- - - - - - - - - -}While(condition)

DO WHILE LOOP

AAKASH KAUSHIK 9891983083,9289817971

BODY OF LOOP +

UPDATION

Page 32: C++ programming Unit 5 flow of control

DO-WHILE LOOP EXECUTION SEQUENCE

AAKASH KAUSHIK 9891983083,9289817971

INTIALIZATION BODY

EXECUTION+

UPDATION

CONDITIONCHECKING

START

TERMINATE

LOOP

FALSE

T R U E

Page 33: C++ programming Unit 5 flow of control

THANK YOU

AAKASH KAUSHIK 9891983083,9289817971