Control Structure
Control Structure
Control statements enable us to specify the flow of program control; ie, the order in which the instructions in a
program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one
section of code to another.
There are four types of control statements in C:
Decision making statements
Selection statements
Iteration statements
Jump statements
M.A.Ansari Page 1
2. if else : f else statement checks the given condition. if test expression is evaluated to true, statements inside the
body of if are executed. if test expression is evaluated to false, statements inside the body of else are executed.
if (test condition)
{
one or more
Statements ;
}
else
{
one or more
Statements ;
}
3.Nested if else: Nesting means using one if-else construct within another one. ... When a condition
is true, then it will process the If block otherwise it will process an else block. In this case, the condition
is true hence the If a block is executed and the value is printed on the output screen.
Syntax: if (test condition)
{
if (test condition)
{
one or more Statements;
}
else
{
one or more Statements;
}
}
else
{
one or more
Statements;
}
M.A.Ansari Page 2
4.else if ladder : Sometimes we keep write if-else constructions inside the else parts only. These constructions
are known as if-else ladder (or) else-if ladder (or) if-else-if ladder.
Syntax:
if (test condition 1)
{
one or more Statements;
}
else if (test condition 2)
{
one or more Statements;
}
else if (test condition 3)
{
one or more Statements;
}
else if (test condition 4)
{
one or more Statements;
}
else
{
M.A.Ansari Page 3
one or more statements;
}
switch case :
Switch is a statement used to select one out of multiple options.
Switch is generally known as multi-way branch statement.
Switch is more suitable when a single expression is compared for equality with multiple
constants.
Generally switch is felt as ‘easier to use’ compared with an equivalent if-else construction.
Syntax:
switch(expression)
{
case 1:
/* Statement/s */
break;
case 2:
/* Statement/s */
break;
case n:
/* Statement/s */
break;
default:
/* Statement/s */
}
M.A.Ansari Page 4
Looping : Looping statement defines a set of repetitive statements . These statements are repeated, with same or
different parameters for a number of times.
Looping statements are also known as iterative or repetitive statement.
There are three types of looping statement in C.
for loop
while loop
do...while loop
Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of
looping way.
while loop:
while loop is an entry controlled looping construct. We use while loop to repeat set of statements when number of
iterations are not known prior to its execution. It provides flexibility to define loop without initialization and update
parts (present in for loop).
Looping statements whose condition is checked prior to the execution of its body is called as Entry controlled loop.
Basic syntax of while loop is as follows:
while(condition)
{
// Body of while loop
}
M.A.Ansari Page 5
for loop: For loop is an entry controlled looping statement. It is used to repeat set of statements until some
condition is met.
Looping statements whose condition is checked prior to the execution of its body is called as Entry controlled loop.
Basic syntax of for loop is as follows:
for( expression1; expression2; expression3)
{
Single statement
or
Block of statements;
}
In the above syntax:
expression1 - Initialisese variables.
expression2 - Condtional expression, as long as this condition is true, loop will keep executing.
expression3 - expression3 is the modifier which may be simple increment of a variable.
do...while loop : do...while is an exit controlled looping statement. We use do...while loop when there is a need
to check condition after execution of loop body. do...while loop in any case executes minimum once.
M.A.Ansari Page 6
Looping statements whose condition is checked after execution of its loop body is called as Exit controlled loop
We use do...while loop if we need to execute loop body minimum once.
Syntax :
do
{
// Body of do while loop
} while (condition);
Jump statements:
Unlike conditional and looping statement, jump statement provides unconditional way to transfer control from one
part of program to other. C supports three jump statements.
break
continue
goto
1. Break statement : break is jump statement used to terminate a switch or loop on some desired condition. On
execution, it immediately transfer program control outside the body of loop or switch. In the case of nested
switch or loop, it terminates the innermost switch or loop.
Syntax : break;
M.A.Ansari Page 7
2. Continue statement : continue is a jump statement used inside loop. It skips loop body and continues to
next iteration. continue statement on execution immediately transfers program control from loop body to next
part of the loop. It works opposite of break statement.
Syntax of continue statement
continue;
3. goto statement : goto is a jump statement used to transfer program control unconditionally from one part
of a function to another.
Syntax of goto statement :
label:
goto label;
M.A.Ansari Page 8