UNIT 5control Statement Structure by Sir
UNIT 5control Statement Structure by Sir
Control Statement
The statements which alter flow of execution of a program are known as control
statements.
There are two types of control statements: Decision Making & Repeating construct
5. switch statement
if(test_ expression)
{
statements-block;
}
Example 1
Write a program to read a number from user and test whether the number is negative
[Show message “The number is negative ” if it is negative number otherwise show
nothing].
#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter a number to be tested:");
scanf("%d", &num);
if(num<0)
printf("The number is negative");
getch();
return 0;
}
Output
Enter a number to be tested:-6
The number is negative
Nested if Statement
When one if statement is written within body of another if statement, it is called nested
if statement if statement. The several if statements shall be declared as
nested if statements.
Syntax:
if(expression)
{
if(expression)
{
//body
}
}
Example
Write a program to read percentage of marks obtained by a student in SLC and +2
level, Display message “Congratulation!! You have first division in both SLC and
+2” if both levels have percentage greater than or equal to 60.
#include<Stdio.h>
#include<conio.h>
int main()
if(SLC_ per>=60)
if(plus2_per>=60)
print f("Congratulation!!");
print f("\n You have first division in both SLC and +2.");
getch () ;
return 0;
Output
Congratulation!!!!!
if(test_ expression)
{
true-block statement (s);
}
else
{
false-block statement (s);
}
Example
Write a program to read a number from user and determine whether the number
is even or odd.
#include<stdio.h>
#include<conio.h>
int main()
remainder = num % 2;
// Modulo division
if (remainder == 0)
else
getch();
return 0;
Output
Enter a number : 90
Enter a number: 17
if(condition)
{
if(condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(condition3)
{
statement-3;
}
else
{
statement-4;
}
}
Example
Write a program to read three number from user and determine the largest
number among them.
int main()
if (n1>n2)
if(n1>n3)
else
else
if (n2>n3)
else
getch();
return 0;
Output
Largest = 100
Largest = 100
if(condition)
{
statement-1;
}
else if(condition2)
{
statement-2;
}
else if(condition3)
{
statement-3;
}
else if(condition n)
{
statement-n;
}
else
{
default-statement;
}
statement-x;
Example
Write a program that reads total marks of a student in seven subjects. Then
calculate the percentage and determine the division. Use following conditions:
#include<stdio.h>
#include<conio.h>
int main()
if(percent>= 80)
print f("Distinction");
else
print f("Fail");
getch();
return 0;
Output:
First Division
Fail
Types of Loop
i. for loop
ii. while loop
iii. do-while loop
i. for Loop
for(counter_ initialization; test_ condition; increment or decrement)
{
statements; or body of loop
}
Example
#include<stdio.h>
#include<conio.h>
int main()
int num, i;
long fact=1;
for(i=1;i<=num;i++)
fact*= i;
getch();
return 0;
Output
While Loop
While(test_ condition)
{
body of loop
}
Example
#include<stdio.h>
#include<conio.h>
int main()
long fact = 1;
while(i<=num)
fact*=i;
i++;
getch();
return 0;
do-while Loop
Do
{
//statements or body of loop;
}while(test_condition);
Example
Write a program to read a number from keyword until a zero or a negative number
is keyed in. Finally, calculate sum and average of entered numbers.
#include<stdio.h>
#include<conio.h>
int main()
do
sum+=num;
count++;
}while(num>0);
sum=sum-num;
avg=(sum)/(count-1)
getch();
return 0;
Output
Enter number: 10
Enter number: 20
Enter number: 15
Enter number: 5
Enter number: 0
break Statement
The break statement terminates the execution of the loop and the control is transferred
to the statement immediately following the loop.
break;
Example
#include<stdio.h>
#include<conio.h>
int main()
int i;
if (i==5)
break;
getch();
Output
1 2 3 4 5
continue Statement
The continue statement is used when we want to continue running the loop.
continue;
Example
Write a program that asks for a number n from user and then display only even
numbers from 2 to n .
#include<stdio.h>
#include<conio.h>
int main()
int i, num;
if(i%2!=0)
continue;
getch();
return 0;
Output
Enter a number: 20
2 4 6 8 10 12 14 16 18 20
goto STATEMENT
The goto statement is used to after the normal sequence of program execution by
unconditionally transferring control to some part of the program.
goto label;
label: statements;
Example
int main()
if(num<0)
goto negative;
if(num2<0)
goto negative;
getch();
return;
negative :
getch();
return 0;
Output
switch STATEMENT
When there are a number of options available and one of them is to be selected on the
basis of some criteria, switch statement is used.
switch(variable or expression)
{
case caseConstant1:
statements;
break;
case caseConstant2:
statements;
break;
default:
statements;
}
Important Questions
b. Conditional Operator
How do you swap the values of two integers without using the third temporary variable?
Justify with the example.
Asked on 2079 Exam
Write a program to demonstrate the following menu-driven program. The user will
provide an integer and alphabet for making choice and the corresponding task has to be
performed according as follow:
D. Exit
The choice will be displayed until the user will give “D” as a choice.
Asked on 2079 Exam
Why do we need a break and continue statement? Define formal argument and actual
argument in function with examples. Identify and list the errors in the following code.
int main(){
int a,b,c
scanf("%d%d%d, &a, &b, &c);
sum(a, b, c);
return -1;
}
Write a program to print largest among three numbers entered by the user.
What do you mean by jump statement? Explain each jump statement with example.
Write a program to check whether a number entered is prime or not.
Write a program that computes the sum of digits of a given integer number
What is break statement? Discuss with example. How the break statement is different
from continue statement?
What is looping statement? Discuss different looping statements with suitable example
of each.
What do you mean by looping? Explain while loop with suitable example. Compare
while loop with do-while loop. Write a program to find sum and average of first n natural
numbers.