Professional Documents
Culture Documents
CP Notes chptr4
CP Notes chptr4
Control Structures in C
4.1 Introduction
As the name suggests the "Control Instruction" 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. There are 4 types of control
instructions in C. They are:
Sequence Control Instruction
Selection or Decision Control Instruction
Repetition or Loop Control Instruction
Case Control Instruction
The sequential structure consists of a sequence of program statements that are executed one after
another in order. The selective structure consists of a test for a condition followed by alternative path
that the program can follow. The repetitive structure consists of program statements that are
repeatedly executed while some condition hold true. In case control structure, among multiple cases,
only one condition will executed that hold the condition true.
i) if statement: The "if" statement is used to express conditional expressions. If the given
condition is true then it will execute the statements; otherwise it will execute optional
statements. The braces { and } are used to group declarations and statements into compound
statement or a block.
Syntax:
if(condition)
{
//set of stmt1
………………
………………
}
//set of stmt2
…………………
…………………
- If the condition is true set of statement stmt1 is executed and then after only statement set
stmt2 are executed otherwise statement set stmt1 are skipped and directly statement set stmt2
is executed.
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
int marks;
clrscr();
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 1
ii) 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.
Syntax:
if(condition)
{
//statement set1
}
else
{
//statement set2
}
Example: A program to read any two numbers from the keyboard and to display the larger
value.
#include <stdio.h>
#include <conio.h>
void main()
{
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 2
iii) Nested if- else statements: The if..else statement which is again inside if..else statements, is
called nested if..else statements which are as follows.
Syntax: 1
if (expression)
{
if(expression)
{
} Syntax: 2
else
{ if(expression)
if(expression)
} __________
} else
else __________
{ else
if(expression) _____________
{
}
else
{
}
}
getch();
}
iv) “if … else if” ladder: In else if ladder (or multi-way conditional statements), the condition
expression is evaluated in order. If any of these expressions is formed to be true, the statement
associated with it is executed and this terminates the whole chain. If none of the expression is
true then the statement associated with final else is executed.
Syntax:
if(condition)
{
}
else if
{
else if
{
}
else
{
//default statement
}
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
int marks;
clrscr();
printf("Enter marks of a student\n");
scanf("%d",&marks);
if(marks>60)
{
printf("Student is passed in first division:\n");
}
else if(marks>45)
{
printf("Student is passed in second division:\n");
}
else if(marks>32)
{
printf("Student is passed in third division:\n");
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 5
#include<stdio.h>
#include<conio.h>
#define meter_charge 50
void main()
{
int units;
float amt,amt_rupees=0;
clrscr();
printf("\n***Electricity Amount calaulating program***\n");
printf("How many units of electricity used?");
scanf("%d",&units);
if(units>300)
amt_rupees=(units-300)*60+200*50+100*40;
else if(units>100)
amt_rupees=(units-100)*50+100*40;
else
amt_rupees=units*40;
amt=amt_rupees+meter_charge;
printf("\nThe payable amount is %.2f",amt);
getch();
}
Assignments
Write a program to calculate the income tax and net salary having following condition.
If salary>=9000 income tax is 40% of salary
If salary>=7500 and salary<=8999 then income tax is 30% of salary
If salary<=7499 then income tax is 20% of salary
Note: WAP to find the largest and smallest number among 3 user entered numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, x, y;
clrscr();
printf("Enter any three numbers:");
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 6
y = a<b?(a<c?a:c):(b<c?b:c);
printf("The largest number is:%d”, y);
getch();
}
Syntax:-
switch(variable)
{
case 1:
//statement set1
……………..
break;
case 2:
//statement set2
……………..
break;
case n:
//statement set_N
……………………
break;
default:
//default statement set
…………..
break;
}
default:
printf("The number is odd");
}
getch();
return 0;
}
Example: WAP to find the day on entering a number less than or equal to 7.
#include<stdio.h>
#include<conio.h>
int main()
{
int day;
clrscr();
printf("Enter numeric day of the week:\n");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Day is Sunday\n");
break;
case 2:
printf("Day is Monday\n");
break;
case 3:
printf("Day is Tuesday\n");
break;
case 4:
printf("Day is Wednsday\n");
break;
case 5:
printf("Day is Thursday\n");
break;
case 6:
printf("Day is Friday\n");
break;
case 7:
printf("Day is Saturday\n");
break;
default:
printf("Your choice is wrong!!!!!!!!");
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 8
Assignment: Write a program using switch case to calculate area and circumference of a circle.
Suppose we need to display the natural number series 1 2 3 and suppose we do not know about loop
then probably we will draw a flowchart-1. Here to print 1 2 3 you used the statements “X=x+1 and
print value of x” three
times. If you would
have to print 1 2 3
4….100 then you
would have to write the
above statements 100
times. Do you think it
is a tedious task? Yes
of course it is a tedious
job. But don’t be afraid,
here Dennis Ritchie has
developed a tool called
loop to solve this
problem. C has
provided you a
mechanism to repeat
X=x+1 and print ‘x’ till
you want.
Take a look at the
flowchart-2.
a) for loop: The for loop is mostly used loop for repetitive structure and is an entry control loop.
The for loop consists of three expressions. The first expression issued to initialize the index
value, the second to check whether the loop is to be continued again and third to change the
index value for further repetition.
Example: WAP to count all the integers greater than 100 and less than 200 that are divisible by
7. Also find the sum of these numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, count=0, sum=0;
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 11
Nested Loops: If we define loop insides a loop, such loops are called nested loops. For example:
for(i=0;i<10;i++)
{
for(j=0;j<5;j++) //nested loop
{
…………
}
}
b) while loop statement: The second type of loop statement is while loop. While loop first checks
whether the initial condition is true or false and finding it to be true, it will enter the loop and
execute the statement. Thus, it is also an entry control loop (perform pre-test).
Syntax:
initialization;
while(test condition)
{
Statement 1;
…
Statement n;
increment or decrement;
}
OR
Q. Program to calculate factorial using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,n;
long float fact=1;
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 13
c) do…while loop statement: The do...while loop is another repetitive loop used in C programs.
Differ from the for loop and while loop, the do…while loop is an exit control (post-test) loop.
Thus, the while loop tests the condition before executing any of the statements inside the while
loop but the do…while loop test the condition after executing the statements. This means that
the do…while would execute its statements at least one even if the condition fails for first time
itself. The while loop on the other hand will not execute its statements if the condition fails for
the first times.
Syntax:
do
{
Statement 1;
...
Statement n;
}
while(condition);
A. Continue statement
Continue statement is used to bypass the execution of the statements inside the loop. In some
situations we want to jump to the next iterations of the loop the loop by ignoring the execution of
statements specified within the body of loop. The keyword “continue” allows us to do this. When
the keyword is encountered inside the loop, control automatically passes to the next iteration of
the loop. A continue specified using if () statement. As an example, consider the following
program:
Example:
main()
{
int i;
for (i =1; i<=5;i++)
{
If (i==3)
continue;
printf(“%d”,i);
}
getch();
}
Example:
main()
{
int n, sum=0;
printf(“enter a number and press 0 to stop:”);
do
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 15
The output of the program would be: enter a number and press 0 to stop: 1 2 – 3 4 -5 0
The sum of positive number is = 7. Note that the negative numbers are not added to the sum
because when the value of n is less than 0 (i.e. negative) the continue statement bypass the
execution of the sum = sum +n.
B. Break Statement
The break statement is used as a statement inside the body of loop. Generally the break statement
is associated with a condition specified with if () statement. As soon as the condition evaluates
within if (), the execution of break statement takes the control outside the loop and therefore
stops the further iterations of the loop. As an example, consider the following program:
Example:
void main() In this program, when i=3, the
{ execution of the break
int i; statement terminates the loop
for (i =1; i<=5;i++) and the output of the program
{ would be:
Printf(“%d”,i); 123
If (i==3)
Loop terminates here
break;
In the above program, the
}
printf(“%d”,i); is placed
printf(“\n loop terminates here”);
before break .
getch();
}
Do you think that placing printf(“%d”,i); statement after break statement will make a difference?
Here is the program:
This program invites the user to enter positive numbers. It also finds the summation of these
numbers. As soon as user enters a negative number, the loop is terminated by the break
statement. Let us take a look at the sample output: Enter numbers: 1 2 3 4 5 -1
Sum of positive numbers is = 15
Q. What is the similarity and difference between break and continue statements?
Similarity: Both break and continue are jump statements.
Difference: The break statement terminates the entire loop execution whereas continue
statement terminates single pass of the loop.
C. goto statement
The goto statement is used to alter the program execution sequence by transferring the control to
some other part of the program.
#include <stdio.h>
void main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
if(a>b)
goto output1;
else
goto output2;
output1:
printf("Largest value=%d",a);
goto stop;
output2:
printf("Largest value=%d",b);
goto stop;
stop:
;
}
Q. If two numbers n1 & n2 are input through the keyboard. WAP to find sum of those
numbers which is exactly divisible by 5 between n1 and n2.
void main()
{
int n1, n2, sum=0, i;
printf(“enter n1 & n2”);
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 18
Q. Write a program to check whether the given number is prime number or not.
Method-I:
#include <stdio.h> Method-II:
#include <conio.h> main()
void main() {
{ int n, i, c=0;
int num,i; printf(“enter a number”);
clrscr(); scanf(“%d”&n);
printf("Enter the number:"); i=2;
scanf("%d",&num); while(i<=n-1)
i=2; {
while(i<=num-1) if(n%i==0)
{ c++;
if (num%i==0) i++;
{ }
printf("Not a prime no"); if(c==0)
break; printf(“The number is prime”);
} else
i++; printf(“The number is
} composite”);
if(i==num) getch();
printf("Prime number"); }
getch();
}
Q. If a 4 digit number is input through the keyboard. WAP to check whether it is palindrome
or not. Hind: A number is palindrome if it is equal to its reverse. For example: 4224 is palindrome
but 4223 is not palindrome. To check whether a number is palindrome or not, first we should find the
reverse of a number and check whether it is equal to its reverse or not.
Method-II:
Method-I: main()
void main() {
{ int n, rev=0, y, d;
int n, rev=0, y, d; printf(“enter a no”);
printf(“enter a number”); scanf(“%d”,&n);
scanf(“%d”,&n); y=n;
y=n;
while(n!=0) while(1)
{ {
d=n%10; d=n%10;
rev=rev*10+d; rev=rev*10+d;
n=n/10; n=n/10;
} if(n==0)
break;
if(y==rev) }
printf(“the number is palindrome”);
else
printf(“the number is not palindrome”);
getch();
}
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 20
void main()
{
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
printf(“%d\t”,j);
printf(“\n”);
}
getch();
}
void main()
{
int i=1, b=0, x, v;
x=69; /*ASCII value of E */
while(i<=5) /*for five lines*/
{
j=65; /*ASCII value of A*/
v=x; /*assign x to v*/
while(j<=v)
{
printf(“%c”, j); /*print ASCII character of j*/
j++;
}
if(i==1)
v--;
k=1;
while(k<=b)
{
printf(“ “); /*for blank*/
k++;
}
b=2*i-1;
while(v>=65)
{
printf(“%c”, v);
v--;
}
printf(“\n”);
x--;
i++;
}
getch();
}
void main()
{
int p, q, m, n=5; /*n for no. of lines (5 lines)*/
for(p=1; p<=n; p++)
{
for(q=1; q<=n-p; q++) /*to print space*/
printf(“ “);
m=p;
for(q=1; q<=p; q++) /*to print numbers*/
{
printf(“%d”,m);
m=m+1;
}
m=m-2;
for(q=1; q<p; q++)
{
printf(“%d”,m);
m--;
}
}
1
2 3
4 5 6
7 8 9 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Exercise: WAP to read two integers n1 & n2 and display sum of even & odd numbers between the
two nos. separately.
Q. WAP to read an integer no. & add the individual digits contained in it until the final sum is
a single digit. For e.g. if the number is
n=35876, then
3+5+8+7+6=29
2+9=11
1+1=2
void main()
{
long int n;
int r, sum=0;
scanf(“%d”, &n);
do
{
sum=0;
do
{
r=n%10;
sum=sum+r;
n=n/10;
} while(n!=0);
n=sum;
} while(sum/10!=0);
printf(“%d”, sum);
getch();
}
Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 26
main()
{
int l, i;
float t;
l=1
while(i<=5)
{
t= pow(i, 2)+pow((i+1), 2)/(float)(i+2);
printf(“%f\t”, t);
l++;
}
getch();
}
Q. WAP to print Floyd’s triangle. A Floyd’s triangle is a right angled triangular array of
numbers arranged as follow:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
void main()
{
int i, j, n=1;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
printf(“%d”, n++);
printf(“\n”);
}
getch();
}