CONTROL STATEMENTS
3.1Control Statements:
In C program, statements are normally executed sequentially in the order in
which they appear. But sometimes, we have to change the order of execution of
statements based on certain conditions, or repeat a group of statements until certain
specified conditions are satisfied.
For this, C language provides Control statements (or decision making
statements) which alter the flow of execution and provide better control to the
programmer on the flow of execution. They are two types. They are
1. Conditional statements
2. Iterative statements
3.2Conditional (Selection) statements:
Conditional statements are used to execute a statement or a group of
statement based on certain conditions.
3.2.1.Simple if statement:
The general form of simple if statement is
if(expression)
{
Statement block;
}
Statement-x;
Here, the expression can be any valid C expression. Statement block may
contain one statement (or) more statements.
First expression will be evaluated. If the expression is true(or
nonzero) then the statements in the statement block gets executed and then
control transfers to the next immediate statement of simple if i.e., statement-
x.
If the expression is false(or zero) then control transfers to execute
the next immediate statement of simple if i.e., statement-x by skipping the
statements in the statement block.This is illustrated in the following
flowchart.
Page 1
, Unit - II
Expression? true
Statement block
False
Statement -x
/*program to find absolute value of an integer using simple if statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
clrscr( );
printf(“enter a integer\n”);
scanf(“%d”,&n);
if(n<0)
{
n = - n;
}
printf(“the absolute value of number is %d\n”,n);
}
Page 2
, Unit - II
3.2.2if…else statement:
The general form of if …else statement is
if (expression)
{
Statement block1;
}
else
{
Statement block2;
}
Statement-x;
Here, the expression can be any valid C expression. Statement block1 and
statement block2 may contain one statement (or) more statements.
First expression will be evaluated. If the expression is true(or
nonzero) then the statements in the statement block1 gets executed and then
control transfers to the next immediate statement of if..else statement i.e.,
statement-x by skipping the statements in the statement block2.
If the expression is false(or zero) then the statements in the
statement block2 gets executed and then control transfers to the next
immediate statement of if..else statement i.e., statement-x by skipping the
statements in the statement block1.This is illustrated in the following
flowchart.
False true
expression?
Statement block2 Statement block1
Statement -x
Page 3