Unit-5 Control Statements
Unit-5 Control Statements
Unit-5
Control Statements
Introduction
The statements which alter the flow of execution of the program are known as control
statements.
Sometimes we have to do certain calculations/tasks depending on whether a condition or test
is true or false. Similarly, it is necessary to perform repeated actions or skip some statements.
For these operations, control statements are needed.
main()
{
int n;
printf("Enter a number to be tested:");
scanf("%d",&n);
if(n<0)
printf("The number is negative");
getch();
return 0;
}
If…else statement
The if-else statement is used when there are only two possible actions-one happens when a
test condition is true, and other when it is false.
Syntax:
if(condition)
{
//Statement block
}
else
{
//Statement block
}
E.g.
Program to find whether a number is odd or even:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
if (a%2==0)
{
printf("%d is even",a);
}
else
{
printf("%d is odd",a);
}
getch();
}
if(condition2)
{
statement/statements;
}
else
{
statement/statements;
}
}
else
{
if(condition3)
{
statement/statements;
}
else
{
statement/statements;
}
}
E.g.
#include<stdio.h>
main()
{
int num,rem;
printf("Enter an integer Number:");
scanf("%d",&num);
if(num>=0)
{
rem=num%2;
if(rem==0)
{
printf("%d is positive even number",num);
}
else
{
printf("%d is positive odd number",num);
}
}
else
{
rem=num%2;
if(rem==0)
{
printf("%d is negative even number",num);
}
else
{
printf("%d is negative odd number",num);
}
College Note Prepared By: Jayanta Poudel
4 C Programming Reference Note
}
return 0;
}
Q. Write a program to find whether given an integer is divisible by 3 and 5 but not by 10.
#include <stdio.h>
#include<conio.h>
main( )
{
int n;
printf("Enter an integer:");
scanf("%d", &n);
if(n%3 == 0 && n%5 == 0)
{
if(n%10!=0)
{
printf("%d is divisible by 3 & 5 and not by 10", n);
}
else
{
printf("%d is divisible by 3, 5 & 10", n);
}
}
}
Q. Write a program to read three numbers from user and determine the largest number
among them.
#include <stdio.h>
#include<conio.h>
int main( )
{
int n1, n2, n3;
printf("Enter 3 numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
printf("largest=%d", n1);
else
printf("largest=%d", n3);
}
else
{
if(n2>n3)
printf("largest=%d", n2);
else
printf("largest=%d", n3);
}
getch();
return 0;
}
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
The break is used to break out of the case statements. break is a keyword that breaks out of
the code block. The break prevents the program from testing the next case statement also.
E.g.
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
Q. Write a program to find whether a input char is vowel or constant or other characters
using switch statement.
#include <stdio.h>
int main() {
char c;
printf("Enter a character:");
scanf("%c", &c);
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
{
switch (c) {
case'a':
case'e':
case'i':
case'o':
case'u':
case'A':
case'E':
case'I':
case'O':
case'U':
printf("It is vowel");
break;
default:
printf("It is consonant");
}
}
else
printf("It is other character");
return 0;
}
Syntax:
for(initilization; condition expression; increment/decrement)
{
//Body of loop
}
E.g.
#include<stdio.h>
void main( )
{
int i;
for(i = 1; i <= 10; i++)
{
printf("%d\t", i);
}
}
While Loop
Syntax:
initialization;
while(condition_expression)
{
//Body of loop
//update statement(increment/decrement)
}
First the condition is evaluated; if it is true then the statements in the body of loop are
executed. After the execution, again the condition is checked and if it is found to be true then
again the statements in the body of loop are executed. This means that these statements are
executed continuously till the condition is true and when it becomes false, the loop terminates
and the control comes out of the loop.
E.g.
#include<stdio.h>
void main( )
{
int i;
i = 1;
while(i <= 10)
{
printf("%d\t", i);
i++;
}
}
Q. Write a program to calculate factorial of a number using while loop.
#include<stdio.h>
#include<conio.h>
int main( )
{
int n, i;
long int fact;
printf("Enter the integer:");
scanf("%d", &n);
fact=1, i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
printf("Factorial of %d is %ld", n, fact);
getch();
return 0;
}
Do-while Loop
Syntax:
initialization;
do{
//Body of loop
//update statement
}while(condition_expression);
Here firstly the segments inside the loop body are executed and then the condition is
evaluated. If the condition is true, then again the loop body is executed and this process
continues until the condition becomes false.
E.g.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;
do
{
printf("\n%d",i);
i++;
}while(i<5);
getch();
}
Q. Write a program to print the even number upto n using do-while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
printf("Enter a value of n:");
scanf("%d",&n);
i=1;
do
{
if(i%2==0)
printf("%d\t",i);
i++;
}while(i<=n);
getch();
}
Q. Write a program to compute and print the sum of given numbers of squares.
[12+22+32+….+n2]
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum=0;
printf("Enter a value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i*i;
}
printf("\nThe sum is:%d", sum);
getch();
}
Nesting of Loops
When a loop is written inside the body of another loop, then it is known as nesting of loops.
Any type of loop can be nested inside any other type of loop. For example, a for loop may be
nested inside another for loop or inside a while or do...while loop. Similarly, while and do
while loops can be nested.
E.g.
#include<stdio.h>
void main( )
{
int i, j;
/* first for loop */
for(i = 1; i < 5; i++)
{
printf("\n");
/* second for loop inside the first */
for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
}
Break Statement
The break statement is used to terminate loops or exit from a switch. It can be used within a
for, while, do –while, or switch statement. If a break statement is included in a while, do –
while or for loop, then control will immediately be transferred out of the loop when the break
statement is encountered.
E.g.
#include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for (i = 1; i <= 10; ++i)
{
printf("Enter a n%d: ", i);
scanf("%lf", &number);
College Note Prepared By: Jayanta Poudel
13 C Programming Reference Note
Continue statement
The continue statement is used to bypass the reminder of the current pass through a loop. The
loop does not terminate when a continue statement is encountered. Rather, the remaining loop
statements are skipped and the computation proceeds directly to the next pass through the
loop.
E.g
Program to print sum of odd numbers between 0 and 10
#include <stdio.h>
int main ()
{
int a,sum = 0;
for (a = 0; a < 10; a++)
{
if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("sum = %d",sum);
return 0;
}
Goto Statement
The goto statement is used to alter the normal sequence of program execution by transferring
control to some other part of the program.
Syntax:
goto label;
Where label is an identifier that is used to label the target statement to which control will be
transferred. The target statement must be labeled, and the label must be followed by a colon.
Thus, the target statement will appear as
label: statement
E.g.
#include<stdio.h>
#include<conio.h>
int main( )
{
int n ;
printf("Enter the number :") ;
scanf ("%d", &n) ;
if (n%2 == 0)
goto even ;
else
goto odd;
even :
printf ("Number is even") ;
goto end ;
odd :
printf ("Number is odd") ;
end :
printf ("\n") ;
getch( );
return 0;
}
#include <stdio.h>
#include<conio.h>
int main( )
{
int n, i, j, a;
printf("How many rows:");
scanf("%d", &n);
a=1;
for(i=1; i<=n; i++)
{
for(j=1; j<=i;j++)
{
printf("%d\t", a++);
}
printf("\n");
}
getch();
return 0;
}
College Note Prepared By: Jayanta Poudel
16 C Programming Reference Note