CP Unit Iii
CP Unit Iii
Control Statements: In C program, statements are normally executed sequentially in the order
in which they appear. But sometimes, we have to change the order of execution of statements
based on certain conditions, or repeat a group of statements until certain specified conditions are
satisfied.
For this, C language provides Control statements (or decision making statements) which
alter the flow of execution and provide better control to the programmer on the flow of
execution. They are two types. They are
1. Conditional statements
2. Iterative statements
if(expression)
{
Statement block;
}
Statement-x;
Here, the expression can be any valid C expression. Statement block may contain one statement
(or) more statements.
First expression will be evaluated. If the expression is true(or nonzero) then the statements in
the statement block gets executed and then control transfers to the next immediate statement of
simple if i.e., statement-x.
If the expression is false(or zero) then control transfers to execute the next immediate
statement of simple if i.e., statement-x by skipping the statements in the statement block.This is
illustrated in the following flowchart.
True
Expression?
False true
Statement block
Statement -x
Statement -x
/*program to find whether an integer is a even number or not using if..else statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
clrscr( );
printf(“enter a number\n”);
scanf(“%d”,&n);
if(n%2 = =0)
printf(“the number %d is even number\n”,n);
else
printf(“the number %d is odd number\n”,n);
}
If the expression1 is false (or zero) then the statements in the statement block3 gets executed
and then control transfers to the statement-x. This is illustrated in the following flowchart.
Expression1?
Expression2?
Statement-x
else
{
if(b>c)
printf(“the maximum of three numbers is %d\n”,b);
else
printf(“the maximum of three numbers is %d\n”,c);
}
}
else if ladder:
Here expression is any valid expression that evaluates to an integer. value-1, value-2,
…………are Constant expressions(evaluable to an integer constant) and are known as case
labels. Each of these values should be unique within a switch statement.
When the switch is executed, the value of the expression is successfully compared with
values value-1,value-2,………..If a case is found whose value matches with the value of the
expression, then the block of statements that follows the case are executed. The break statement
at the end of each block signal the end of a particular case and causes an exit from the switch
statement, transferring control to the statement-x following the switch.
The default is an optional case. When present, it will be executed if the value of the
expression does not match with any of the case values. If not present, no action takes place if all
matches fail and the control goes to the statement-x. This is illustrated in the following
flowchart.
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“%d+%d=%d\n”,a,b,a+b);
break;
case 2:
printf(“%d-%d=%d\n”,a,b,a-b);
break;
case 3:
printf(“%d*%d=%d\n”,a,b,a*b);
break;
case 4:
printf(“%d/%d=%d\n”,a,b,a/b);
break;
default:
printf(“you have entered wrong choice\n”);
break;
}
}
Iterative statements (or) Loops: In looping, sequences of statements are executed until
some conditions are satisfied for the termination of loop. Depending on where we check the
condition for the termination of the loop, loops are divided into two types. They are
1.Pre-test loop (or) entry controlled loop
2.Post –test loop(or) exit controlled loop
Pre-test loop (or) entry controlled loop: In these loops, in each iteration the condition is tested
first. If it is true, the loop continues; otherwise, the loop is terminated. This is illustrated in the
following flowchart.
In the exit controlled loops, in each iteration, the loop continues. Then the control
expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates.
while loop:
while loop is an entry controlled loop. The general form of while loop is
while(expression)
{
Statement block;
}
Statement-x;
Here expression is any valid C expression. Statement block may contain one statement
(or) more than one statement.
First expression is evaluated. If the expression is true(or nonzero), then the statements in
the body of the loop gets executed. After execution of the body of the loop, control again goes to
evaluate the expression and if it is true(or nonzero), the body of the loop is executed once again.
This process of repeated execution of the body of the loop continues until the expression
becomes false(or zero).If the expression is false(or zero) control transfers to the next immediate
statement of while loop i.e., statement-x.
example program 1:
/*program to calculate sum of the digits of an integer using while statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int n, number, digit, sum=0;
clrscr( );
printf(“enter the number\n”);
scanf(“%d”,&number);
n=number;
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf(“sum of digits of the number %d is %d\n”,number,sum);
}
example program2:
/*program to find the reverse of a positive integer using while statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int n, number, digit, revn=0;
clrscr( );
printf(“enter the number\n”);
scanf(“%d”,&number);
n=number;
while(n!=0)
{
digit=n%10;
revn=revn *10 +digit;
n=n/10;
}
printf(“the reverse of the number %d is %d\n”,number,revn);
}
C Programming Notes Page 10
Unit - III
do…while loop:
do…. while loop is an exit controlled loop. The general form of do…while loop is
do
{
Statement block;
} while(expression);
Statement-x;
Here expression is any valid C expression. Statement block may contain one statement
(or) more than one statement.
First body of the loop gets executed and control goes to evaluate the expression. If the
expression is true(or nonzero), then the statements in the body of the loop gets executed once
again. After execution of the body of the loop, control again goes to evaluate the expression and
if it is true(or nonzero), the body of the loop is executed once again. This process of repeated
execution of the body of the loop continues until the expression becomes false(or zero).If the
expression is false(or zero) control transfers to the next immediate statement of do…while loop
i.e., statement-x.
Example program:
/*program to calculate sum of the first n natural numbers using do…. while statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i=1,n, sum=0;
clrscr( );
printf(“enter the number\n”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i=i+1;
}while(i<=n);
printf(“sum of first %d natural numbers is %d\n”,n,sum);
}
for loop:
The for loop is entry controlled loop. The general form of the for loop is
note1: more than one variable can be initialized at a time in the initialization section of for
statement but they must be separated by comma. Similarly the increment section may also have
more than one part which must be separated by comma.
Example program1:
/*program to calculate sum of the first n natural numbers using for statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i,n,sum;
clrscr( );
printf(“enter the number\n”);
scanf(“%d”,&n);
for(i=1,sum=0;i<=n;i++)
{
sum=sum+i;
}
printf(“sum of first %d natural numbers is %d\n”,n,sum);
}
C Programming Notes Page 12
Unit - III
Example program2:
/*program to calculate factorial of a positive integer using for statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i,n,fact;
clrscr( );
printf(“enter the positive integer\n”);
scanf(“%d”,&n);
for(i=1,fact=1;i<=n;i++)
fact=fact*i;
printf(“factorial of %d is %d\n”,n,fact);
}
Note2: we can omit one or more sections in the for loop, if necessary.
Nesting of for loops:
Nesting of for loops is allowed in C i.e., we can write one for statement within another
for statement. The nesting may continue upto any desired level.
Example program1:
/*program to print all prime numbers between 1 and n using nested for loops*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i,j,n,nf;
clrscr( );
printf(“enter the number\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
nf=0;
for(j=1;j<=i;j++)
{
if(i%j= =0) nf++;
}
if(nf= = 2)
printf(“%d\t”,i);
}
}
======
do
{
printf(“Input a number.\n”);
scanf("%d", &num);
}
while(num>0);
In the above example, the loop will be executed till the entered value of the variable num is not 0
or less then 0. This is a sentinel controlled loop and here the variable num is a sentinel variable.
Jumping in loops:
continue statement: continue statement is a jump statement. The continue statement can be
used only inside for loop, while loop and do-while loop. The general form of continue is
continue;
Execution of continue statement does not cause an exit from the loop but it suspend the
execution of the loop for that iteration and transfer control back to the loop for the next iteration.
/*program to find sum of first 20 natural numbers except numbers which are divisible by 5 using
continue statement*/
#include<stdio.h>
#include<conio.h>
main( )
{
int n,sum=0;
clrscr( );
for(n=1;n<=20;n++)
{
if(n%5==0)
continue;
sum=sum+n;
}
printf(“sum=%d\n”,sum);
}
break statement: The break statement is a jump instruction and can be used inside a switch (or)
in loops. When executed, the control transfers out of switch (or) from the loop which contains
the break statement and program execution continues with the next immediate statement of the
switch (or) loop. The general form of break statement is
break;
Example:
#include <stdio.h>
int main () {
int a = 10;
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
if( a > 15) {
/* terminate the loop using break statement */
break;
}
}
return 0;
}
goto statement: C supports the goto statement to branch unconditionally from one
statement to another in the program. The goto requires a label in order to identify the statement
where the branch is to be made. A label is any valid identifier, and must be followed by a colon.
The label is placed immediately before the statement where the control is to be transferred.
The general form of goto statement is
goto label; label:
------------- -------------
-------------- -------------
-------------- --------------
label: goto label;
------------- -------------
------------- -------------
-------------- --------------
Forward jump backward jump
The label can be anywhere in the program either or after the goto label statement. If the label is
placed after the goto statement, then it is called a forward jump and if it is placed before the goto
statement, then it is called backward jump.
Example:
#include<stdio.h>
#include<conio.h>
main( )
{
int number;
clrscr( );
goto x;
y:
printf(“VVIT”);
goto z;
x:
printf(“my college name is ”);
goto y;
z:
printf(“, Namburu”);
}
Output:
my college name is VVIT, Namburu