UNIT 3 Control structure in C
PREPARED BY
PROF. VISHVA UPADHYAY
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
Simple statements
● The simplest kind of statement in C is an expression (followed by a semicolon, the
terminator for all simple statements). Its value is computed and discarded.
1 x = 2; /* an assignment statement */
2 x = 2+3; /* another assignment statement */
3 2+3; /* has no effect---will be discarded by smart compilers */
4 puts("hi"); /* a statement containing a function call */
5 root2 = sqrt(2); /* an assignment statement with a function call */
Most statements in a typical C program are simple statements of this form.
● Other examples of simple statements are the jump statements return, break, continue, and
goto.
● A return statement specifies the return value for a function (if there is one), and when
executed it causes the function to exit immediately.
● The break and continue statements jump immediately to the end of a loop.
● The goto statement jumps to another location in the same function, and exists for the rare
occasions when it is needed.
Decision making statements
● Decision making statements help you to make a decision based on certain conditions.
These conditions are specified by a set of conditional statements having boolean
expressions which are evaluated to a boolean value true or false. There are following
types of conditional statements in C.
● If statement
● If-Else statement
● Nested If-else statement
● If-Else If ladder
● Switch statement
If Statement
● The single if statement in C language is used to execute the code if a condition is true.It
is also called one-way selection statement.
Syntax
if(expression)
{
//code to be executed
}
Example of If Statement
#include<stdio.h>
#include<conio.h>
2
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
if(n%2==0)
{
printf("%d number in even",num);
}
getch();
}
If Else Statement
● The if-else statement in C language is used to execute the code if the condition is
true or false. It is also called a two-way selection statement.
Syntax
if(expression)
{
//Statements
}
else
{
//Statements
}
● If the expression is evaluated to nonzero (true) then if block statement(s) are
executed.
● If the expression is evaluated to zero (false) then else block statement(s) are executed.
Example of If Else Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int num=0;
printf("enter the number");
scanf("%d",&num);
3
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
if(n%2==0)
{
printf("%d number in even", num);
}
else
{
printf("%d number in odd",num);
}
getch();
}
Nested If – Else Statement
● The nested if...else statement is used when a program requires more than one test
expression. It is also called a multi-way selection statement. When a series of the decision
are involved in a statement, we use the if else statement in nested form.
Syntax
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
Example of Nested If – Else Statement
#include<stdio.h>
#include<conio.h>
void main( )
{
4
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
int a,b,c;
clrscr();
printf("Please Enter 3 number);
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greatest");
}
else
{
printf("c is greatest");
}
}
else
{
if(b>c)
{
printf("b is greatest");
}
else
{
printf("c is greatest");
}
}
getch();
}
If..else If ladder
● The if-else-if statement is used to execute one code from multiple conditions. It is also
5
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
called a multipath decision statement. It is a chain of if..else statements in which each if
statement is associated with another if statement and last would be an else statement.
Syntax
if(condition1)
{
//statements
}
else if(condition2)
{
//statements
}
else if(condition3)
{
//statements
}
else
{
//statements
}
Example of if…else if ladder
#include <stdio.h>
#include<conio.h>
void main( )
{
int a;
printf("enter a number");
scanf("%d",&a);
if( a%5==0 && a%8==0)
{
printf("divisible by both 5 and 8");
}
else if( a%8==0 )
{
printf("divisible by 8");
}
6
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
else if(a%5==0)
{
printf("divisible by 5");
}
else
{
printf("divisible by none");
}
getch();
}
Switch Statement
● Switch statement acts as a substitute for a long if-else-if ladder that is used to test a list
of cases. A switch statement contains one or more case labels which are tested against
the switch expression. When the expression matches a case then the associated
statements with that case would be executed.
Syntax
Switch (expression)
{
case value1:
//Statements
break;
case value 2:
//Statements
break;
case value 3:
//Statements
case value n:
//Statements
break;
Default:
//Statements
}
Example of Switch Statement
#include<stdio.h>
#include<conio.h>
void main()
7
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
{
int no;
printf("\n Enter Day no between 1-7 :");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n Sunday");
break;
case 2:
printf("\n Monday");
break;
case 3:
printf("\n Tuesday");
break;
case 4:
printf("\n Wednesday");
break;
case 5:
printf("\n Thursday");
break;
case 6:
printf("\n Friday");
break;
case 7:
printf("\n Saturday");
break;
default:
8
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
printf("\n Please Enter Proper Input");
break;
}
getch();
}
Looping Statements
● Looping Statements in C execute the sequence of statements many times until the stated
condition becomes false.
● A loop in C consists of two parts, a body of a loop and a control statement.
● The control statement is a combination of some conditions that direct the body of the
loop to execute until the specified condition becomes false. The purpose of the C loop is
to repeat the same code a number of times.
Types of Loops in C
● Depending upon the position of a control statement in a program, looping statement in C
is classified into two types:
● 1. Entry controlled loop
● 2. Exit controlled loop
● In an entry control loop in C, a condition is checked before executing the body of a loop.
It is also called a pre-checking loop.
● In an exit controlled loop, a condition is checked after executing the body of a loop. It is
also called a post-checking loop.
● The control conditions must be well defined and specified otherwise the loop will execute
an infinite number of times. The loop that does not stop executing and processes the
9
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
statements number of times is called an infinite loop. An infinite loop is also called an
“Endless loop.”
Characteristics of an infinite loop:
● No termination condition is specified.
● The specified conditions never meet.
● The specified condition determines whether to execute the loop body or not.
There are three types of loop constructs:
1. The while loop
2. The do-while loop
3. The for loop
While Loop :
● In a while loop, a condition is evaluated before processing a body of the loop. If a
condition is true then and only then the body of a loop is executed.
Do-While Loop :
● In a do…while loop, the condition is always executed after the body of a loop. It is also
called an exit-controlled loop.
For Loop :
● In a for loop, the initial value is performed only once, then the condition tests and
compares the counter to a fixed value after each iteration, stopping the for loop when
false is returned.
While Loop in C
● A while loop is the most straightforward looping structure.
Syntax of While Loop in C:
while (condition)
{
statements;
}
● It is an entry-controlled loop. In a while loop, a condition is evaluated before processing a
body of the loop. If a condition is true then and only then the body of a loop is executed.
After the body of a loop is executed then control again goes back at the beginning, and
the condition is checked if it is true, the same process is executed until the condition
becomes false. Once the condition becomes false, the control goes out of the loop.
● After exiting the loop, the control goes to the statements which are immediately after the
loop. The body of a loop can contain more than one statement. If it contains only one
statement, then the curly braces are not compulsory. It is a good practice though to use
the curly braces even if we have a single statement in the body.
● In a while loop, if the condition is not true, then the body of a loop will not be executed,
not even once.
10
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Do-While loop in C
● A do…while loop in C is similar to the while loop except that the condition is always
executed after the body of a loop. It is also called an exit-controlled loop.
Syntax of Do-While Loop in C:
do {
statements
} while (expression);
● As we saw in a while loop, the body is executed if and only if the condition is true. In
some cases, we have to execute a body of the loop at least once even if the condition is
false. This type of operation can be achieved by using a do-while loop.
● In the do-while loop, the body of a loop is always executed at least once. After the body
is executed, then it checks the condition. If the condition is true, then it will again execute
the body of a loop otherwise control is transferred out of the loop.
● Similar to the while loop, once the control goes out of the loop the statements which are
immediately after the loop is executed.
11
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
● The critical difference between the while and do-while loop is that in the while loop the
while is written at the beginning. In a do-while loop, the while condition is written at the
end and terminates with a semicolon (;)
Example :
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
2
4
6
8
10
12
14
16
18
20
For Loop in C
● A for loop is a more efficient loop structure in ‘C’ programming.
● Syntax of For Loop in C:
for (initial value; condition; incrementation or decrementation )
{
statements;
}
● The initial value of the for loop is performed only once.
● The condition is a Boolean expression that tests and compares the counter to a fixed
value after each iteration, stopping the for loop when false is returned.
● The incrementation/decrementation increases (or decreases) the counter by a set value.
#include<stdio.h>
int main()
12
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Break Statement in C
● The break statement is used mainly in the switch statement. It is also useful for
immediately stopping a loop.
Example of Break
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<5;++i)
{
if(i==3)
break;
printf(“%d “,i);
}
getch();
}
Output:
012
13
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
Continue in C
● When you want to skip to the next iteration but remain in the loop, you should use the
continue statement.
Example
#include<stdio.h>
int main()
{
int nb = 7;
while (nb > 0)
{
nb--;
if (nb == 5)
continue;
printf("%d\n", nb);
}
}
Output:
6
4
3
2
1
So, the value 5 is skipped.
Difference Between Break and Continue
Break Continue
A break statement is used in switches and continue statement is used in loops only.
loops.
When break is encountered the switch or When continue is encountered, the
loop execution is immediately stopped. statements after it are skipped and the loop
control jumps to the next iteration.
Goto Statement
● The goto statement is known as jump statement in C. As the name suggests, goto is used
to transfer the program control to a predefined label.
14
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(3110003)
● The goto statement can be used to repeat some part of the code for a particular condition.
It can also be used to break the multiple loops which can't be done by using a single
break statement
● it makes the program less readable and complicated.
● Syntax of goto statement in C
goto label_name;
..
..
label_name: C-statements
● Example of goto statement
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++){
sum = sum+i;
if(i==5){
goto addition;
}
}
addition:
printf("%d", sum);
return 0;
}
● Output:
15
● Explanation: In this example, we have a label addition and when the value of i (inside
loop) is equal to 5 then we are jumping to this label using goto. This is the reason the sum
is displaying the sum of numbers till 5 even though the loop is set to run from 0 to 10.
15