You are on page 1of 18

Lesson 4: Control Flow

Conditional Statements
What are control
statements?
 Control statements in C help the
computer execute a certain logical
statement and decide whether to
enable the control of the flow
through a certain set of statements
or not.
 it is used to direct the execution of
statements under certain
conditions.
Types of Control Statements in C

1.Decision-making control statements


a.Simple if statement
b. If-else statements
c.Nested if-else statements
d. else-if ladder
2. Conditional statements
3. Goto statements in C
4. Loop control statements in C
a. While Loop
b. Do-while Loop
c. For Loop
Simple if statement

 are carried out to perform some operation


when the condition is only true.
Syntax of the if statement:

if( condition)
{
Statement 1;
}
Ex. Simple if statement
If-else Statement

execute statements based on true or false


under certain conditions, therefore; you use if-
else statements. If the condition is true, then if
block will be executed otherwise the else block
is executed.
Syntax of the if statement:

if( condition)
Statement1;
else
Statement2;
If-else Statement
Nested if-else Statements

The nested if-else statements consist of another if


or else. Therefore; if the condition of “if” is true
(i.e., an outer if) then outer if’s if block is executed
which contains another if (that is inner if) and if
the condition of if block is true, statements under
if block will be executed else the statements of
inner if’s “else” block will be executed.
Syntax of the nested if else statement:

if( condition)
Statement1;
else
Statement2;
Nested if-else Statements

Syntax of the nested if else statement:


Nested if-else Statements

Syntax of the nested if else statement:


Else-if Ladder Statements

contain multiple else-if, when either of the


condition is true the statements under that
particular “if” will be executed otherwise the
statements under the else block will be
executed.
Syntax of the nested if else statement:
Else-if Ladder Statements

Syntax of the nested if else statement:


Conditional Control Statements in C

the control is transferred to that particular case label and


executed the statements under it. If none of the cases are
matched with the switch expression, then the default
statement is executed.
Syntax of switch statement:
switch(expression) {
case label 1: statement ;
break;
case label 2: statement;
break;
case label n: statement n;
default: default statement;
}
other statements;
Goto Statements in C

are used to transfer the flow of control in a


program, goto statement is also known as a
jump control statement because it is used to
jump to the specified part of the program. The
label in goto statement is a name used to
direct the branch to a specified point in the
program.
Syntax of go to statement:
goto labelname;
Ex. goto odd;
Loop Control Statements in C

Loop- refers to a set of statements in a


program executed repeatedly either a
fixed number of times or until some
condition is TRUE or FALSE.
 It is also the control structures in
which a block of instructions is
repeated until a condition is fulfilled.
Why use loop?

Where need repetition of same code a number


of times at that place use Loop in place of
writing more than one statements. The way of
the repetition will form a circle that’s why
repetition statements are called loops.
 Advantages of using looping statement:
1. Reduce the length of the code
2. Take less memory space
For Loop statement

Syntax of For loop statement


for(initialization; conditional expression;
increment/decrement expression)
{
Statement1;
Statement2;
Statement3;
}
For Loop statement

 Initilization –is an assignment statement


that is used to set a loop control variable
 Conditional expression – is a relational
expression that determines when the loop
will exit by testing the loop control variable
gainst some value.
 Increment/ decrement – defines how the
loop control variable will change each time
the loop is repeated.

You might also like