UNIT # 2: CONTROL STATEMENTS
Control Structures
Control Structures are just a way to specify the flow of control in a program. Any algorithm or
program can be more clearly understood if they use self-contained modules called as logic or
control structures. It basically analyzes and chooses in which direction a program flows, based
on certain parameters or conditions. There are three basic types of logic, or flow of control,
known as:
1. Sequence logic, or Sequential flow
2. Selection logic, or Conditional flow
3. Iteration logic, or Repetitive flow
1. Sequence Logic or Sequential Flow
It is the most common control structure of a program. It may consist of a single statement or a
sequence of statements with a single entry and single exit. Sequential logic as the name suggests
follows a serial or sequential flow in which the flow depends on the series of instructions given
to the computer. Unless new instructions are given, the modules are executed in the obvious
sequence. The sequence of the instructions is from top to bottom.
2. Selection Logic or Conditional Flow
The selection control structure allows one set of statements to be executed if a condition is true
and another set of actions to be executed if a condition is false. The statements written in the
code block only gets executed if the specified condition is True. The Number of Conditions may
vary depending on the requirement of any program.
1
3. Iteration Logic or Repetative Flow
It is also known as Looping Structures. In programming, we have to repeat same task over and
over. Writing same set of instructions over and over is impractical. Looping statements helps to
repeat or iterate the same block of statements multiple number of time as per the requirement of
the user. Using loops in helps to reduce length of the program and are convenient and flexible to
handle.
2
Any program that is coded using any of the control structures specified above is called a
structured program and the approach is called as Structured Programming
CONTROL STATEMENTS
SELECTION SEQUENCE ITERATION
CONDITIONAL UNCONDITIONAL while for do while
if Ternary switch goto
continue break
Operator (? case
:)
3
Selection Statements or Conditional Statements
There are two types of Selection statements;
1. If Statement
2. Switch case
1. If statement
The most common selection statement is the if statement. There are four types of if statements
based on number of conditions to be used in any program;
i) Simple if: used when we have to check only one condition in the program
Syntax: if (condition)
{
Block of statements
}
The block of statement is only executed if the specified condition is true.
4
ii) If else statement: used when we have to check two conditions in the program
Syntax: if (condition)
{
Block of statements
}
else
{
Block of statements
}
The block of statement in the if part is only executed if the specified condition is true but if the
condition is false then the else part gets executed. As seen in the above syntax we haven’t
specified any condition in else. Hence if the condition in if part is false automatically else gets
executed without considering any of the conditions.
5
iii) If else ladder statement: used when we have to check more than two conditions in the
program.
Syntax: if (condition)
{
Block of statements
}
else if(condition)
{
Block of statements
}
else if(condition)
{
Block of statements
}
:
:
:
else
{
Block of statements
}
The block of statement in the if part is only executed if the specified condition is true but if the
condition is false then the next condition in the else if part is checked if it is true then it gets
executed otherwise it moves to next condition until the conditions are over.
6
iv) Nested if statement: One or more if statements if embedded within if statement then it is
called as Nested if. Which means you can use one if or else if statement inside
another if or else if statement(s). There is no specific syntax to be followed in Nested
if. But whenever we use if inside if be it of any type, it is said to be Nested if.
7
2. Switch case:
If you have a large number of decisions and all the decisions depend on the value of the same
variable, then it is better to use switch case statement instead of a series of if else ladder. The
switch statement tests a control expression (conditions), and the control is transferred to one of
the several alternatives i.e to a case constant which matches value of the expression. The value of
the expression may be of int or char type but it cannot be of float or double type. Also switch can
only test for equality.
Syntax: switch (control expression)
{
case constant1:Block of statements;
break;
case constant2:Block of statements;
break;
:
:
:
case constantn:Block of statements;
break;
}
8
Selection Statement Programs:
1. Program to Input a Number and check if it is Even or Odd
2. Program to Input age and height of a candidate and check if the candidate can get
selected for next round (age should be 18 or above and height should be 6 or above )
9
Sr No Switch If
1 The switch statement can only test for The if statement can evaluate logical or
equality i.e; only constant values are relational expressions
applicable
2 No two case statements have identical Same conditions may be repeated any
constants in the same switch number of times
3 Character constants are automatically Character constants are automatically
converted to integers converted to integers
4 In switch statement nested if can be In if statement switch can be used
used
Iteration Statements
They are also called as looping statements. C provides looping statements which allow a set of
instructions to be executed repeatedly until a certain condition is reached. This condition may be
predefined or open ended. Loops are used in programming to execute a block of code repeatedly
until a specified condition is met. There are three types of Iteration statements;
1. While loop
2. For loop
3. Do while loop
There are 3 steps to carry out a loop successfully
1. Variable Initialization
2. Condition
3. Increasing or Decreasing the Initial Value
1. While Loop
The while loop is an entry controlled loop. While loop is also known as a pre-tested loop. In
general, a while loop allows a part of the code to be executed multiple times depending upon a
given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is not known in advance.
Syntax: variable initialization
while(condition)
{
//code to be executed
Increase/decrease the value of the variable
}
10
Properties of while loop
1. A conditional expression is used to check the condition. The statements defined inside the
while loop will repeatedly execute until the given condition fails.
2. The condition will be true if it returns 0. The condition will be false if it returns any non-
zero number.
3. In while loop, the condition expression is compulsory.
4. Running a while loop without a body is possible.
5. We can have more than one conditional expression in while loop.
6. If the loop body contains only one statement, then the braces are optional.
11
While Loop Programs:
1. Program to print numbers from 1 to n
2. For Loop
The for loop in C language is used to iterate the statements or a part of the program
several times.
Syntax: for(variable_initialization;condition;increase/decrease the values)
{
//code to be executed
}
As you can see in while we require 3 different lines for 3 different steps of loops.
Whereas in for all the 3 steps are including in one line itself but by separating each other
by semicolon(;).
Properties of Expression 1 (Variable Initialization)
1. The expression represents the initialization of the loop variable.
2. We can initialize more than one variable in Expression 1.
3. Expression 1 is optional.
4. In C, we cannot declare the variables in Expression 1. However, It can be an exception in
some compilers.
12
Properties of Expression 2 (Condition)
1. Expression 2 is a conditional expression. It checks for a specific condition to be satisfied.
If it is not, the loop is terminated.
2. Expression 2 can have more than one condition. However, the loop will iterate until the
last condition becomes false. Other conditions will be treated as statements.
3. Expression 2 is optional.
4. Expression 2 can perform the task of expression 1 and expression 3. That is, we can
initialize the variable as well as update the loop variable in expression 2 itself.
5. We can pass zero or non-zero value in expression 2. However, in C, any non-zero value
is true, and zero is false by default.
Properties of Expression 3 (Increase/ Decrease value of the variable)
1. Expression 3 is used to update the loop variable.
2. We can update more than one variable at the same time.
3. Expression 3 is optional.
13
For Loop Programs:
1. Program to Calculate factorial of a given number
3. Do while Loop
Unlike for and while loops which test the loop conditions at the start of the loop, the do-
while loop checks its condition at the bottom of the [Link] means that the do while
loop executes atleast once before checking if the condition is true or false. As the
condition is checked at the bottom it is said to be an Exit controlled loop. Also the loop
terminates with semicolon as the condition is checked at the end.
Syntax: variable initialization
do
{
//code to be executed
Increase/Decrease the value of the variable
}while(condition);
14
Difference between While and Do while loop
15
Do while Loop Programs:
2. Program to print sum of 1 to 10 numbers
3. Program to Calculate Simple Interest until user inputs ‘n’
16
Unconditional Statements or Jump Statements
Jump Statement makes the control jump to another section of the program
unconditionally when encountered. It is usually used to terminate the loop or switch-case
instantly. It is also used to escape the execution of a section of the program. Jump
statements in C are used to interrupt the flow of the program or escape a particular
section of the program. There are many more operations they can perform within
the loops, switch statements, and functions. There are four types of jump statements.
1. Break
2. Continue
3. Goto
4. Return
1. Break statement
A break statement is used to terminate the execution of the rest of the block where it is
present and takes the control out of the block to the next statement. It is mostly used in
loops and switch-case to bypass the rest of the statement and take the control to the end
of the loop. Another point to be taken into consideration is that the break statement when
used in nested loops only terminates the inner loop where it is used and not any of the
outer loops.
2. Continue statement
The continue jump statement like any other jump statement interrupts or changes the flow
of control during the execution of a program. Continue is mostly used in loops. Rather
than terminating the loop it stops the execution of the statements underneath and takes
control to the next iteration. Similar to a break statement, in the case of a nested loop, the
17
continue passes the control to the next iteration of the inner loop where it is present and
not to any of the outer loops.
3. Goto statement
goto jump statement is used to transfer the flow of control to any part of the program
desired. The programmer needs to specify a label or identifier with the goto statement
in the following manner:
Syntax: goto label;
Here label indicates the location in the program where the control jumps to.
18
4. Return statement
Return jump statement is usually used at the end of a function to end or terminate it
with or without a value. It takes the control from the calling function back to the main
function (main function itself can also have a return).An important point to be taken
into consideration is that return can only be used in functions that is declared with
a return type such as int, float, double, char, etc. The function declared with void type
does not return any value. Also, the function returns the value that belongs to the
same data type as it is declared. Here is a simple example to show you how
the return statement works.
19