You are on page 1of 26

Control Structures

Session 7
Control Structures

• The general execution of a program is from top


to bottom.
• This kind of flow is called sequential flow
• But at times the flow of the program may be
needed to be controlled basing on some
conditions
• Thus special statements are designed to control
the flow the porgram
• These statements used to control the flow of
execution of the program are called the control
structures
Control Structures In C
Control Structures

Decision Making Looping Non Conditional

If For Break

Simple IF While Continue

If .. else Do..While GoTo

If LAdder

Nested If

Switch
Decision Making Statements
Session 7 a
If Statement

• If statement is used to check a condition


and execute the respective statemetns
only.
• The if has been classified as
– Simple if
– If .. Else
– If Ladder
– Nested If
Simple If
• Syntax
if (cond)
{
statements;
} True
• This statement checks the Cond?
given condition and
– if condition is true the
statements are executed
and remaining program
continues, Statements

False
– or else if the condition is
false, the statements are
not executed but
remaining program will
continue.
If .. Else..

• Syntax:
if (cond )
{
False True
statements-1; Cond?

}
Statements-1 Statements-1
else
{
statements-2;
}
If Ladder
• Syntax:
if(cond-1) • This will check if cond1 is
true or not
{
• If cond1 is true then
statements-1; statements1 will be
} executed
else if (cond-2) • Else cond2 is checked
{ • If cond2 is true
statements-2; statements 2 are
} executed
.… • Else cond3 …. To any
number of conditions
….
• If no condi9tion is true the
else else statements are
{ executed.
Statements;
}
Nested IF
• Wrting an if inside another if is called the nested if
• Unless the first cond is true the next condition is not
checked.
• C allows 32 levels of nesting at maximum.
• Syntax:
if(cond1)
{
if(cond2)
{
….
}
}
Switch Statement

• Syntax: • Works similar to if..ladder


switch (var) • Will compare the var
{ value with the case
case val1 : code-1; break; values for equity only.
case val2 : code-2; break;
• When ever a match is

found the respective code

is executed.
default : code-d;
• If no match found, default
}
code is executed
• Can compare only integer
and character values.
Looping Control Structures
Session 7 b
Loops

• Loop is an iterative control structure.


• Loops are employed to perform a repetitive task.
• One single execute of loop is called an iteration.
• The C offers the following three looping controls
– While Loop
– Do..While Loop
– For Loop
Looping Parameters

• The Loop of a program maintains


separate variable called the looping
variable or integrator or counter variable to
count its iterations.
• Programmer shall mark the following
statements on this variable
– Initialization
– Condition
– Incrementation or Decrementation
Initialization

• This statement assigns (stores) an initial


value in to the looping variable before
starting the loop.
• This will inform the starting value of the
loop from where the iterations are
counted.
• Example I = 1;
Condition

• The condition is a logical statement on the


looping variable
• The condition as long as , is true , the loop
continues to execute.
• When the condition fails the loop stops its
execution.
• When condition is error some ,
– the loop may not start or
– If starts may not end
Incrimination or Discrimination

• When the variable value is not changing,


the condition holds true always.
• So, the variable value is to be increased or
decreased to make the condition false at
one or the other iteration.
• If this statement is error-some,
– The loop may not end at all.
– Or may result in in-appropriate number of
iterations
The Looping Controls

• C gave us three looping controls.


– While
– Do..While
– For
• While and For are Pre-Conditioned Loops.
• Do..While is a Post Conditioned Loop.
While Loop

Syntex: Flow: Initialization


initialization;
while(cond) False
Cond ?
{
True
statements;
inc/dec; stop Statements
}
Inc / dec
Do…While Loop

Syntex: Flow: Initialization


initialization;
do Statements
{
statements; Inc / dec
inc/dec;
} while(cond); False
Cond ?

stop True
For Loop

Syntex: Flow: Initialization


for(initi;cond;inc/dec)
{ False
Cond ?
statements;
True
}
stop
Statements

Inc / dec
Non Conditional Control Statements

• The non conditional control statements will


transfer the flow of control unconditionally.
– break this statement is used to terminate a loop
abruptly before it encounters its condition.
– continue this statement is used to skip one of the
iterations of a loop, the loop is not completely
terminated because of this statement.
– goto <label> this statement will transfer the flow of
control to that point of the program identified by the
<label>.
Jump Statements

Break()

Goto label
Continue()

Exit()
label
The goto statement transfers control to any other
statement within the same function in a C program.
It actually violates the rules of a strictly structured
programming language.
They reduce program reliability and make program
difficult to maintain.
statement
The break statement is used to terminate a case in a
switch statement.
It can also be used for abrupt termination of a loop.

When the break statement is encountered in a loop, the


loop is terminated immediately and control is passed
to the statement following the loop.
statement
The continue statement causes the next iteration of the
enclosing loop to begin.
When this statement is encountered, the remaining
statements in the body of the loop are skipped and the
control is passed on to the re-initialization step.
function
The exit() is used to break out of the program.

The use of this function causes immediate termination


of the program and control rests in the hands of the
Operating System.

You might also like