FLOW OF CONTROL
IF
IF ELSE
ELSE IF LADDER
NESTED IF
SWITCH
Size: 183.53 KB
Language: en
Added: Sep 15, 2014
Slides: 33 pages
Slide Content
C++ Language By:-AAKASH KAUSHIK #9289817971, 98919893083 [email protected]
UNIT -5 FLOW OF CONTROL
FLOW OF CONTROL IT REFERS TO THE FLOW IN WHICH A PROGRAM EXECUTION TAKES PLACE. AAKASH KAUSHIK 9891983083,9289817971
FLOW OF CONTROL 1.)Sequential IN 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
CONDITIONS
IF IF-ELSE ELSE IF LADDER NESTED IF SWITCH CONDITIONAL OPERATOR TYPE OF CONDITIONS AAKASH KAUSHIK 9891983083,9289817971
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
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
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
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; else default statement; statement x; THE ELSE IF LADDER AAKASH KAUSHIK 9891983083,9289817971
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 on Until 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
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
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
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.
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
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
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
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
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
#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
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 Switch Switch can only test for equality Switch 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
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 Switch Switch can only test for equality Switch 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
ITERATIONS/LOOPING
ITERATIONS/LOOPING AAKASH KAUSHIK 9891983083,9289817971 Same block/set of statements is executed repeatedly (again & again) until a specific condition is fulfilled.
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 execution 2.)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 terminated 3.) 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.
FOR loop WHILE loop Do-WHILE loop Nested loops Type of LOOPS AAKASH KAUSHIK 9891983083,9289817971
FOR LOOP EXECUTION SEQUENCE AAKASH KAUSHIK 9891983083,9289817971 INTIALIZATION CONDITION CHECKING TERMINATE LOOP BODY EXECUTION UPDATION FALSE T R U E START
Syntax:- Initialization While(condition) { - - - - - - - - - - - - - - - } WHILE LOOP AAKASH KAUSHIK 9891983083,9289817971 BODY OF LOOP + UPDATION
WHILE LOOP EXECUTION SEQUENCE AAKASH KAUSHIK 9891983083,9289817971 INTIALIZATION CONDITION CHECKING TERMINATE LOOP FALSE T R U E START BODY EXECUTION + UPDATION
Syntax:- Initialization do { - - - - - - - - - - - - - - - } While(condition) DO WHILE LOOP AAKASH KAUSHIK 9891983083,9289817971 BODY OF LOOP + UPDATION
Do-WHILE LOOP EXECUTION SEQUENCE AAKASH KAUSHIK 9891983083,9289817971 INTIALIZATION BODY EXECUTION + UPDATION CONDITION CHECKING START TERMINATE LOOP F A L S E T R U E