0% found this document useful (0 votes)
25 views18 pages

CP Unit Iii

Unit III discusses control statements in C programming, which alter the flow of execution based on conditions. It covers conditional statements like simple if, if-else, nested if-else, and switch statements, as well as iterative statements including while, do-while, and for loops. The document provides examples and explanations for each type of control statement to illustrate their usage.

Uploaded by

smce.ramu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views18 pages

CP Unit Iii

Unit III discusses control statements in C programming, which alter the flow of execution based on conditions. It covers conditional statements like simple if, if-else, nested if-else, and switch statements, as well as iterative statements including while, do-while, and for loops. The document provides examples and explanations for each type of control statement to illustrate their usage.

Uploaded by

smce.ramu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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

Conditional (Selection) statements:


Conditional statements are used to execute a statement or a group of statement based on
certain conditions.
1.Simple if statement: The general form of simple if statement is

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

C Programming Notes Page 1


Unit - III

/*program to find absolute value of an integer using simple if statement*/


#include<stdio.h>
#include<conio.h>
main( )
{
int n;
clrscr( );
printf(“enter a integer\n”);
scanf(“%d”,&n);
if(n<0)
{
n = - n;
}
printf(“the absolute value of number is %d\n”,n);
}
2.if…else statement: The general form of if …else statement is
if (expression)
{
Statement block1;
}
else
{
Statement block2;
}
Statement-x;
Here, the expression can be any valid C expression. Statement block1 and statement block2 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 block1 gets executed and then control transfers to the next immediate statement of
if..else statement i.e., statement-x by skipping the statements in the statement block2.
If the expression is false (or zero) then the statements in the statement block2 gets executed and
then control transfers to the next immediate statement of if..else statement i.e., statement-x by
skipping the statements in the statement block1.This is illustrated in the following flowchart.

False expression? True

Statement block2 Statement block1

Statement -x

C Programming Notes Page 2


Unit - III

/*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);
}

3.Nested if ..else statement:


When an if..else statement is included within another if..else statement, it is known as
nested if ..else statement.
The general form of nested if..else statement is
if (expression1)
{
if (expression2)
{
Statement block1;
}
else
{
Statement block2;
}
}
else
{
Statement block3;
}
Statement-x;
Here, the expression1 and expression2 can be any valid C expression. Statement block1,stement
block2 and statement block3 may contain one statement (or) more statements.
First expression1 will be evaluated. If the expression1 is true(or nonzero) then the expression2
will be evaluated. If expression2 is true(or nonzero) then statements in the statement block1
gets executed and then control transfers to statement-x . If expression2 is false(or zero) then
statements in the statement block2 gets executed and then control transfers to statement-x .

C Programming Notes Page 3


Unit - III

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 block3 Statement block2 Statement block1

Statement-x

/*program to find biggest of three numbers using nested if..else statement*/


#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,c;
clrscr( );
printf(“enter three numbers\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“maximum of three numbers is %d\n”,a);
else
printf(“maximum of three numbers is %d\n”,c);
}

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);
}
}

C Programming Notes Page 4


Unit - III

else if ladder:

The general form of else if ladder is

This is illustrated in the following flowchart.

C Programming Notes Page 5


Unit - III

/*program to find student grade of a student */


#include<stdio.h>
#include<conio.h>
main( )
{
int m1,m2,m3,avg;
clrscr( );
printf(“enter marks in three subjects\n”);
scanf(“%d%d%d”,&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf(“average of the student=%d\n”,avg);
if(avg>=75)
printf(“student grade is A%d\n”);
else if(avg<75&&avg>=60)
printf(“student grade is B%d\n”);
else if(avg<60&&avg>=50)
printf(“student grade is C%d\n”);
else if(avg<50&&avg>=40)
printf(“student grade is D%d\n”);
else if(avg<40&&avg>=35)
printf(“student grade is E%d\n”);
else
printf(“student failed\n”);
}

The switch statement:

The general form of switch statement is

C Programming Notes Page 6


Unit - III

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.

/*program to implement simple calculator using switch statement*/


#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,choice;
clrscr( );
printf(“enter two numbers\n”);
scanf(“%d%d”,&a,&b);
printf(“enter your choice\n”);
printf(“1.addition\n2.substraction\n3.multiplication\4.division\n”);

C Programming Notes Page 7


Unit - III

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.

C Programming Notes Page 8


Unit - III

Post-test loop(or) exit controlled loop:

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.

C language provides three types of loops. They are


1. while loop
2. do…while loop
3. for loop

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

C Programming Notes Page 9


Unit - III

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);
}

C Programming Notes Page 11


Unit - III

for loop:
The for loop is entry controlled loop. The general form of the for loop is

for(initialization; test condition; increment)


{
Statement block;
} Statement-x;

The execution of the for loop is as follows:


Initialization of control variables is done first using assignment statements such as
i=1 and sum=0. Then the value of the control variable is tested using the test condition. If the
condition is true (or nonzero), the body of the loop gets executed and control transfers to for loop
and now the control variable is incremented. The new value of the control variable is again tested
using the test condition. If the condition is true (or nonzero), again the body of the loop gets
executed and control transfers to for loop and the control variable is incremented. This process
continues till the test condition becomes false (or zero) and then control transfers to the next
immediate statement of for loop i.e., statement-x;

Additional features of for loop:

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);
}
}

C Programming Notes Page 13


Unit - III

Counter and Sentinel Controlled Loops:


Based on the nature of control variable and the kind of value assigned to it for testing the control
expression, the loop may be classified into two general categories:
1. Counter Controlled Loops
2. Sentinel Controlled Loops

1. Counter Controlled Loops:

Example : A while loop is an example of counter controlled loop.


========
sum = 0;
n = 1;
while (n <= 10)
{
sum = sum + n*n;
n = n+ 1;
}
This is a typical example of counter controlled loop. Here, the loop will be executed exactly 10
times for n = 1,2,3,......,10.

2. Sentinel Controlled Loops

C Programming Notes Page 14


Unit - III

Example : The following do....while loop is an example of sentinel controlled loop.

======
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.

C Programming Notes Page 15


Unit - III

/*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;

C Programming Notes Page 16


Unit - III

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.

C Programming Notes Page 17


Unit - III

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

C Programming Notes Page 18

You might also like