C Programming
Control Statements
Control Statements in C
Control statements enable us to specify the flow of
program control — that is,
The order in which the instructions in a program
must be executed.
They make it possible to make decisions, to
perform tasks repeatedly
To jump from one section of code to another
Types of Control Statements
There are four types of control statements in C:
1.Decision making statements (if, if-else)
2.Selection statements (switch-case)
3.Iteration statements (for, while, do-while)
4.Jump statements (break, continue, goto)
Decision Making Statement: the if-else Statement
The if-else statement is used to carry out a logical test and then take one of two
possible actions depending on the outcome of the test (ie, whether the outcome
is true or false).
if (condition)
{
// statements
}
else
{
// statements
}
#include<stdio.h>
int main( ) {
int a;
printf("n Enter a number:");
scanf("%d", &a);
if(a>0)
{
printf( "n The number %d is positive.",a);
}
else
{
printf("n The number %d is negative.",a);
}
return 0;
}
Nested if and if-else Statements It is also possible to embed or to nest if-else statements
one within the other. Nesting is useful in situations where
one of several different courses of action need to be
selected.
if(condition1)
{
// statement(s);
}
else if(condition2)
{
//statement(s);
}……………………
else if (conditionN)
{
//statement(s);
}
else
{
//statement(s);
}
The following program makes use of the nested if-else statement to find the greatest of
three numbers.
#include<stdio.h>
int main( ) {
int a, b,c;
a=6,b= 5, c=10;
if(a>b) else if(b>c) //outermost if-else block
{ {
if(b>c) printf(“\nGreatest is: " , b);
{ }
printf(“\nGreatest is: " , a); else
} {
else if(c>a) printf( “\nGreatest is: " , c);
{ }
printf(“\nGreatest is: ", c); return 0;
} }
}
Selection Statement: A switch statement is used for multiple way selections that will
the switch-case Statement branch into different code segments based on the value of a
Example: a program to print the day variable or expression. This expression or variable must be of
of the week integer data type
#include<stdio.h> case 4:
printf("Wednesday");
int main( ) { break;
int day; case 5:
printf("nEnter the number of the day:"); printf("Thursday");
scanf("%d",&day); break;
switch(day) case 6:
{ printf("Friday");
case 1: break;
printf("Sunday"); case 7:
break; printf("Saturday");
case 2: break;
printf("Monday"); default:
break; printf("Invalid choice");
case 3: }
printf("Tuesday"); return 0;
break; }
Iteration Statements Iteration statements are used to execute a particular set of
instructions repeatedly until a particular condition is met or for
a fixed number of iterations
1. The for statement
The for statement or the for loop repeatedly executes the set of instructions that comprise the body of
the for loop until a particular condition is satisfied.
for(initialization; termination; increment/decrement)
{
//statements to be executed
}
#include<stdio.h>
int main() {
int i,n, a, b, sum=0;
printf("Enter the number of terms:");
scanf("%d",&n);
a=0; b=1;
printf("%d \n %d",a ,b);
for(i=2; I < n; i++)
{
sum=a+b;
printf(“\n%d",sum);
a=b;
b=sum;
}
return 0;
}
while loop
The syntax of the while loop is:
while (testExpression)
{
// the body of the loop
}
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
++i;
}
return 0;
}
do...while loop The do..while loop is similar to the while loop with one
important difference. The body of do...while loop is executed at
least once. Only then, the test expression is evaluated
Do
{
// the body of the loop
} while (testExpression);
/ Program to add numbers until the user enters zero
#include <stdio.h>
void main()
{
int number, sum = 0;
do // the body of the loop is executed at least once
{
printf("Enter a number: ");
scanf("%d", &number);
sum += number;
} while(number != 0);
printf("Sum = %d",sum);
}
Break
You have already seen the break statement used in an earlier chapter . It was used to "jump out" of a
switch statement.
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when i is equal to 4:
int i;
for (i = 0; i < 10; i++)
{ Output : 0 ,1,2,3
if (i == 4)
{
break;
}
printf("%d\n", i);
}
Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.
This example skips the value of 4:
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
} Output : 0 ,1,2,3,5,6,7,8,9
printf("%d\n", i);
}
C goto Statement The goto statement allows us to transfer control of
the program to the specified label.
goto label;
... .. ...
... .. ...
label:
statement;