Unit-4 Control Structures
Unit-4 Control Structures
Control Structures
INTRODUCTION
Generally, C program contains many statements
that are normally executed in sequence or we can
say in a specific order as per written in a program.
if (test expression)
{
statement 1;
..............
statement n;
}
statement x;
1. SIMPLE IF STATEMENT
First the test expression is evaluated. If the test
expression is true, the statement of if block
(statement 1 to n) are executed otherwise these
statements will be skipped and the execution will
jump to statement x.
1. SIMPLE IF STATEMENT
In some cases, we want to execute the statement
only if the condition becomes true. If condition is
wrong the we just want to move on next
statement.
SYNTAX OF IF ELSE
STATEMENT
if (test expression)
{
statement_block 1;
}
else
{
statement_block 2;
}
statement x;
2. IF ELSE STATEMENT
In the if-else construct, first the test expression is
evaluated. If the expression is true, statement block
1 is executed and statement block 2 is skipped.
if ( test expression 1)
{
statement block 1;
}
else if ( test expression 2)
{
statement block 2;
}
...........................
else if (test expression N)
{
statement block N;
}
else
{
Statement Block X;
}
Statement Y;
3. IF ELSE IF STATEMENT
In the else if ladder construct, first the test
expression is evaluated.
If the expression1 is true, statement block 1 is
executed , if it is false then it will check expression2
and if it is true then statement block 2 is executed
but if it is false then again it will check for next
condition.
if ( test expression 1)
{
statement block 1;
if(test expression 2)
{
statement block 2;
}
else
{
statement block 3;
}
}
else
{
Statement Block X;
}
Statement Y;
4. NESTED IF ELSE
In the nested if else statement, first the test
expression1 is evaluated.
If the expression1 is true, statement block 1 is
executed and it will check again for next test
expression2 and if it is true then statement block 2
is executed but if it is false then statement block 3
is executed. But if Main test expression1 is false
then it is directly move to the else part and execute
remaining statement.
4. NESTED IF ELSE EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n Enter Value 1 : “);
scanf(“%d”,&a);
printf(“\n Enter Value 2: “);
scanf(“%d”,&b);
printf(“\n Enter Value 3: “);
scanf(“%d”,&c);
if(a>b)
{
if(a>c)
{
printf(“\n A is Maximum”);
}
else
{
printf(“\n C is Maximum”);
}
}
4. NESTED IF ELSE EXAMPLE
else
{
if(b>c)
{
printf(“\n B is Maximum”);
}
else
{
printf(“\n C is Maximum”);
}
}
getch();
}
SWITCH STATEMENT
Instead of using if-else-if ladder, the switch construct can be
used to handle multiple choices, such as menu options. Using
this statement we can develop a menu base program in C
language.
default :
statement block;
[break];
}
SWITCH STATEMENT
The switch construct starts with the switch keyword
followed by a different blocks which contain the
different cases with constant value. Switch evaluates
the control variable and compare with the first case
if its value is equal to constant-1; then it executes the
block of statements until it reaches the break
keyword.
While loop
Do-while loop
For loop
ITERATIVE STATEMENTS
Actually there are two types of loop in C
language Entry controlled loop and Exit controlled
loop.
for (initialization;condition;increment/decrement)
{
statement block;
}
Statement Y;
FOR LOOP
For loop is also called entry controlled loop or pre test
loop because it first check the condition and if
condition becomes true then only execute its block.
for(i=1;i<=10;i++)
{
printf(“\n %d”,i);
}
}
DO WHILE LOOP
The do-while loop is similar to the while loop. The only
difference is that in a do-while loop, the test condition is
tested at the end of the loop.
Statement x;
Initialization of counter variable
do
{
statement_block;
increment/decrement
} while (condition);
statement y;
DO WHILE LOOP
Since the body of the loop is executed first and then
the loop condition is checked in do while loop so it
executes its statement at least once.
break;
for(i=1;i<=10;i++)
{
if(i==5)
break;
printf(“\n %d”,i);
}
}
Continue statement
The continue statement can only appear in the body of a loop.
continue;
for(i=1;i<=10;i++)
{
if(i==5)
continue;
printf(“\n %d”,i);
}
}
goto statement
The goto statement is used to transfer control to a specified
label.