Chapter 5
Control Statements
(8 hours)
5.1 Introduction to Simple and Compound Statement
5.2 Sequential Statement
5.3 Branching Statement
5.3.1 Simple if Statement
5.3.2 if-else Statement
5.3.3 Nested if-else Statement
5.3.4 else-if Ladder
5.3.5 switch Statement
5.3.6 go to statement
5.4 Looping Statement
5.4.1 for loop
5.4.2 while loop
5.4.3 do while
5.4.4 Nested loop
5.5 Loop Interruption
5.5.1 break
5.5.2 continue
Control Statements
• Control statement /structure enables us to specify the order in
which the various instructions in a program are to be executed by
the computer.
• In other words, the control instructions determine the "flow of
control" in a program i.e. the order in which the instructions in a
program must be executed.
• It is possible to make decisions, to perform tasks repeatedly or to
jump from one section of code to another.
• There are 3 types of control instructions in C. They are:
• Sequence Control Instruction
• Selection or Decision Control Instruction
• Repetition or Loop Control Instruction
Sequence Control Statement
• The program control flow from one command to the next is called
sequential control flow / statement.
Example: / * Program to illustrate sequential flow of execution */
Selection or Decision or Branching
Control statement
• The statements that make selection of statements to be executed are called
selection statement or branching statement.
• The selective structure consists of a test for a condition followed by alternative
path that the program can follow.
• It can be used to select a block for execution based on a condition
• The conditional expression is mainly used for decision making.
• There are various structures of the control statements.
a) Simple if statement/ if statement
b) if….else statement
c) Nested else if / Else if ladder
d) Nested if- else statements
e) Switch-Case statement / Case Control Instruction
Simple if statement / if statement
• The if statement is used to express conditional expressions.
• If the given condition is true then it will execute the statements
• The braces { } are used to group declarations and statements into
compound statement or a block.
Flowchart
Syntax:
if(condition)
{
//set of statementt1
}
If the condition is true set of stmt1 is executed ,otherwise set
of stmt1 are skipped and directly statement outside of { } will
be executed.
Example:
#include <stdio.h> ClassWork:
#include <conio.h> a) WAP to input a number and check even
void main()
or not.
{
int marks; b) WAP to input number and check
clrscr(); positive or not.
printf("Enter marks of a student\n");
scanf("%d",&marks);
if(marks>=32)
printf("Student is passed\n");
if(marks<32)
printf("Student is Failed:\n");
getch();
}
if….else statement
• In this case, either of the two statement is executed depending upon the
value of the expression.
• The first statement is executed if the expression is true; otherwise the
else statement is executed.
Flowchart
Syntax:
if(condition)
{
//statement 1
}
else
{
//statement 2
}
Q. WAP to find a number which is even or Q. A program to read any two numbers from the keyboard and to
odd. display the larger value.
#include <stdio.h>
#include <stdio.h>
#include<conio.h>
#include <conio.h> Q. WAP to input 3
void main( )
void main()
{
digit number and
{
int n;
float x,y; check whether it is
clrscr();
clrscr();
printf("Enter first number:"); palindrome or not
printf("Enter a number\n");
scanf("%d" ,&n);
scanf("%f",&x); Q. WAP to input 3
printf("Enter second number:");
if(n%2==0)
scanf("%f",&y); digit number and
{
printf("The number is even\n");
if(x>y) check whether it is
{
}
printf("Larger value is %f",x); Armstrong or not
else
}
{
else
printf("The number is odd\n");
{
}
printf("Larger value is %f",y);
getch();
}
}
getch();
}
if else if ladder
Syntax:
• In else if ladder (or multi- if(condition1)
Flowchart
way conditional {
statements), the condition Statement 1
}
expression is evaluated in else if (condition2)
order. {
Statement 2
• If any of these expressions is }
formed to be true, the
statement associated with it else if (condition3)
{
is executed and this Statement 3
terminates the whole chain. }
…
• If none of the expression is …
true then the statement … else
{
associated with final else is
executed. //default statement
}
#include <stdio.h>
#include <conio.h> else if(marks>=32)
void main() {
{ printf("Student is passed in third division:\
int marks; clrscr(); n");
printf("Enter marks of a student\n"); }
scanf("%d", &marks); else
if(marks>=80) {
{ printf("Student is failed:\n");
printf("Student is passed in distinction:\n"); }
} getch();
else if(marks>=60) }
{
printf("Student is passed in first division:\n");
}
else if(marks>=45)
{
printf("Student is passed in second division:\
n");
}
Q. WAP to input any number and check whether positive, negative or zero number.
Q. WAP to input any three number and display the largest number using nested else …if.
Q. WAP to input a character and check whether it is uppercase, lowercase or not.
Q. WAP to check whether a character is vowel or consonant.
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is Consonant.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}
return 0; }
Q. Write a program to check whether a triangle is valid or not, when the three sides of the triangle are entered through the
keyboard and determine the triangle is equilateral, isosceles, equilateral, scalene or right-angled triangle.
#include<stdio.h> Else if(a==b||a==c||b==c)
int main() printf("\n It is a Isosceles Triangle.");
{ Else if ((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+
float a,b,c; (c*c)||(c*c)==(a*a)+(b*b))
printf("\n Enter value for Side-1 : "); printf("\n It is a Right-angleTriangle.");
scanf("%f",&a); Else if
printf("\n Enter value for Side-2 : "); (a!=b&&a!=c&&b!=c)
scanf("%f",&b); printf("\n It is a Scalene Triangle.");
printf("\n Enter value for Side-3 : "); }
scanf("%f",&c); Else
printf("\n RESULT: This Triangle is NOT
if(a<(b+c)&&b<(a+c)&&c<(a+b))
possible.");
{
getch ();
printf("\n RESULT: It is a Triangle.");
return 0;
if(a==b&&a==c&&b==c)
printf("\n It is a Equilateral Triangle.");
}
Nested if…else statement
• The if...else statement which is again inside if…else statements, is called
nested if…else statements which are as follows.
Syntax Flowchart
Class work
1. WAP to calculate all the roots of quadratic equation.
2. WAP to input selling price and cost price and check whether profit or
loss.
3. WAP to check whether a number is divisible by 5 and 11 or not
[Hints:----if( n %5==0 && n %11==0)---print” divisible by 5 and 11
4. WAP to input marks in any three subjects and check whether he/she is
pass or fail. Assume Pass marks for each subject is 32.
5. WAP to input age and nationality of a person and check whether
he/she can cast a vote or not. Assume age must be more than 18 and
nationality is nepali.
6. WAP to check alphabet, digit or special character.
7. WAP to input a number and find largest number among three number
6.
#include <stdio.h> else if(ch>='0' && ch<='9')
int main() /* whether it is digit */
{ {
char ch; printf("This is a digit.\n");
printf("Input a character: "); }
scanf(“%c”, &ch); else /* Else special character */
/* Checks whether it is an alphabet {
*/
printf("This is a special
if((ch>='a' && ch<='z') || (ch>='A'
&& ch<='Z')) character.\n");
{ }
printf("This is an alphabet.\n"); }
}
• A bank has introduced on incentive policy. A bonus of 2% of the balance is
given to everyone, irrespectively of their balance and 5% is given to female
account holder. If their balance is more than Rs. 5000. WAP to represent
this policy and calculate balance after bonus.
• A set of two linear equations with two unknown variables x1 and x2 are
given as:
ax1+bx2=m
cx1+dx2=n
The set has a unique solution
x1=(md-bn)/(ad-cb)
x2=(na-mc)/ (ad-cb)
Provided ad-cd is not equal to zero.
WAP to read the values of coefficients and constants a,b,c,d,m,n and
compute the value of x1 and x2. An approximate message should be printed
if ad-bc=0.
switch case statement
• The control statement that allows us to make a decision from the
number of choices is called a switch statement.
• It chooses a particular group of statements from several available
groups.
• It matches the variable with the case value.
• The set of statements inside the case that matches the variable are
executed.
• If none of the case value matches the expression then the default
statement is executed.
• The case value may be either integer or character.
• Case labels always end with a colon ( : ). Each of these cases is
associated with a block.
Syntax Flowchart
Example 1: // Following is a program to demonstrate syntax of Example 2
switch.
#include <stdio.h>
int main()
{
int x = 2;
switch (x)
{
case 1:
printf("Choice is 1");
break;
case 2:
printf("Choice is 2");
break;
case 3:
printf("Choice is 3");
break;
default:
printf(“you entered Choice other than 1, 2
and 3");
break;
}
return 0;
}
Q. WAP to calculate volume ,TSA, CSA of
cylinder. Enter the choice character as A,B
and C.