Control
Structures
CONTROL STRUCTURES
Control
Structures
Branching/ Looping/ Jumping/
Selection Iteration Jump
Statements Statement Statements
• If • sFor • Break
• If Else • While • Continue
• Nested If • Do While • Goto
• If Else If
• Switch
BRANCHING/SELECTION STATEMENTS
• If
• If Else
• Nested If
• If Else If
• Switch
BRANCHING/SELECTION STATEMENTS
• If Statement
• Indicates decision is to be made
• Contains an expression that can be true or false
• Performs action only if the condition is true
• Single-entry/single-exit
• Diamond symbol (decision symbol)
• Syntax:
if ( expression )
execute statement if expression is true
normal statements after if..
BRANCHING/SELECTION STATEMENTS
• If Statement
• Flowchart of IF Statement
A decision can be made on
any expression.
true 0 (Zero) - false
grade >= 60 print “Passed”
Non-Zero - true
Example:
false 3,-4,9 are true
Nothing
BRANCHING/SELECTION STATEMENTS
• If Statement
• E.g.:
If student’s grade is greater than or equal to 60 Print
“Passed”
if ( grade >= 60 ) cout << "Passed";
BRANCHING/SELECTION STATEMENTS
• If Else
• Indicates decision is to be made
• Contains an expression that can be true or false
• Performs different action if the condition is true
and different action if condition is false
• Single-entry/Two-exit
• Diamond symbol (decision symbol)
• Syntax:
if ( expression )
execute statement if expression is true else
execute statement if expression is false
BRANCHING/SELECTION STATEMENTS
• If Else
• Flowchart of IF Else Statement
true
grade >= 60 print “Passed”
false
print “Failed”
BRANCHING/SELECTION STATEMENTS
• If Else Statement
• E.g.:
If student’s grade is greater than or equal to 60 Print
“Passed” otherwise Print “Failed”
if ( grade >= 60 ) cout << "Passed";
else
cout << “Failed";
BRANCHING/SELECTION STATEMENTS
• Nested If
• If inside another If
• Indicates decision after another decision is to be
made
• Performs again expression check if the condition is
true
• Single-Restrictive-entry
• Syntax:
if ( expression )
if ( expression )
execute statement if both if expressions are true normal
statements outside ifs
BRANCHING/SELECTION STATEMENTS
• Nested If
• Flowchart of Nested IF Statement
true true
attendance >= 75 Marks >= 60 print “Passed”
false false
BRANCHING/SELECTION STATEMENTS
• Nested If Statement
• E.g.:
If student’s attendance is more than or equal to 75 and the
grade is greater than or equal to 60 Print “Passed”
if ( attendance>= 75 ) if ( grade >= 60 )
cout << "Passed";
BRANCHING/SELECTION STATEMENTS
• If Else If Ladder
• Indicates decision after another decision is to be
made(i.e. Multiple Cases)
• Contains an expression that can be true or false
• Performs again expression check if the condition is
false
• Single-More-Restrictive-entry
• Syntax:
if ( expression )
else if ( expression ) else if ( expression )
execute statement if expression is true
BRANCHING/SELECTION STATEMENTS
• If Else If
• Flowchart of IF Else IF Statement
false true
attendance >= 75 Medical Proof ? Allowed 4 Exam
true false
Allowed 4 Exam
BRANCHING/SELECTION STATEMENTS
• If Else If Statement
• E.g.:
If student’s attendance is more than or equal to 75 then print
“Allowed 4 Exam” otherwise if student is having medical proof
only then Print “Allowed 4 Exam”
if ( attendance>= 75 )
else if ( medical == true )
cout << “Allowed 4 Exam";
BRANCHING/SELECTION STATEMENTS
• If Else If Ladder
• E.g.:
if ( grade >= 90 ) cout << // 90 and above
"A";
else if ( grade >= 80 ) cout // 80-89
<< "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) cout // 60-69
<< "D";
else // less than 60
cout << "F";
BRANCHING/SELECTION STATEMENTS
• Switch
• Indicates decision is to be made
• Contains multiple cases(Multiple Branching Statement)
• Single-entry/Multiple-exit
• Syntax:
switch ( literal )
{ case 1:
execute statement if case1 matches
case 2:
execute statement if case2 matches
}
BRANCHING/SELECTION STATEMENTS
• Switch
• E.g.:
If the character Entered If by user is A Print “Vowel: A”
the character Entered If by user is E Print “Vowel: E”
the character Entered If by user is I Print “Vowel: I”
the character Entered If by user is O Print “Vowel: O”
the character Entered by user is U Print “Vowel: U”
switch( character ) case ‘E’:
{ cout << “Vowel: E"; break;
case ‘A’: .
cout << “Vowel: A"; break; }
LOOPING/ITERATION STATEMENTS
• For
• While
• Do While
LOOPING/ITERATION STATEMENTS
• For Loop
• Indicates Iteration/Repetition of statements
• Contains an initialization, expression and updation
• Keep on performing action till the condition is true
• Syntax:
for ( initialization ; expression/test ; updation )
execute statement if expression is true
LOOPING/ITERATION STATEMENTS
• For Loop
• E.g.:
Printing Numbers from 1 to 20.
for ( int i=1; i<=20; i++)
{
cout << i <<endl;
}
LOOPING/ITERATION STATEMENTS
• While Loop
• Indicates Iteration/Repetition of statements
• Contains an expression that should be true in order
to perform actions repeatedly
• Keep on performing action till the condition is true
• Syntax:
while ( expression/test )
{
execute statement if expression is true
}
LOOPING/ITERATION STATEMENTS
• While Loop
• E.g.:
Printing Numbers from 1 to 20.
int n=1;
while ( n<=20)
{
cout << i <<endl; n++;
}
LOOPING/ITERATION STATEMENTS
• Do While Loop
• Indicates Iteration/Repetition of statements
• Contains an expression that should be true in order to
continue actions repeatedly
• Perform Operation and then check(Exit-Controlled)
• Perform action at least once even if condition is false
• Syntax:
do
{
execute statement if previously checked expression was true
} while ( expression/test );
LOOPING/ITERATION STATEMENTS
• Do While Loop
• E.g.:
Printing Numbers from 1 to 20.
int n=1;
do {
cout << i <<endl; n++;
} while ( n<=20) ;
JUMPING/JUMP STATEMENTS
• Break
• Continue
• Goto
JUMPING/JUMP STATEMENTS
• Break
• Stops the execution of the Loop
• Allows to exit from current Iteration
• Mostly used with branching statements
• Syntax:
loop
{
execute statements
if (expression) break;
execute statements
}
JUMPING/JUMP STATEMENTS
• Break
• E.g.:
Stop Printing Numbers when printed till 3.
int n=1;
while ( n<=20)
{
cout << i <<endl; if(n==3)
break;
n++;
}
JUMPING/JUMP STATEMENTS
• Continue
• Skip the current iteration
• Prevent statements to be executed once only
• Mostly used with branching statements
• Syntax:
loop
{
execute statements
if (expression) continue;
execute statements
}
JUMPING/JUMP STATEMENTS
• Continue
• E.g.:
Print Numbers from 1 to 10 but 3 should not be printed.
int n=1;
while ( n<=10)
{
if(n==3) continue;
cout << i <<endl;
n++;
}
JUMPING/JUMP STATEMENTS
• Goto
• Jump to a particular location
• Allows to go to a particular labelled statement in a
program
• Mostly used with branching statements
• Syntax:
loop execute statements
{ }
execute statements labelName:
if (expression) statements after label
goto labelName;
JUMPING/JUMP STATEMENTS
• Goto
• E.g.:
Print Numbers from 1 and 2 and jump to finished.
int n=1;
while ( n<=10)
{ if(n==3)
goto finish;
cout << i
<<endl; n++;
finish: }
cout<<“Finished”;