Control Structures
Control Structures
Structures
CONTROL STRUCTURES
Control
Structures
• 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
Nothing
BRANCHING/SELECTION STATEMENTS
• If Statement
• E.g.:
true
grade >= 60 print “Passed”
false
print “Failed”
BRANCHING/SELECTION STATEMENTS
• If Else Statement
• E.g.:
true true
attendance >= 75 Marks >= 60 print “Passed”
false false
BRANCHING/SELECTION STATEMENTS
• Nested If Statement
• E.g.:
false true
attendance >= 75 Medical Proof ? Allowed 4 Exam
true false
Allowed 4 Exam
BRANCHING/SELECTION STATEMENTS
• If Else If Statement
• E.g.:
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";
• 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.
• 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:
int n=1;
while ( n<=10)
{ if(n==3)
goto finish;
cout << i
<<endl; n++;
finish: }
cout<<“Finished”;