0% found this document useful (0 votes)
87 views26 pages

L4-Control Statement in C -Part 1.ppt

C lecture pdf 5

Uploaded by

Ishan
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)
87 views26 pages

L4-Control Statement in C -Part 1.ppt

C lecture pdf 5

Uploaded by

Ishan
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/ 26

C Programming

Module-4: Control Statements in C

IMT and IMG 1st Year

Instructor : Dr. Santosh Singh Rathore


ABV-IIITM Gwalior
Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content for the slides have been taken from the various sources and
from the reference books.

Students need to follow reference books to get the thorough details


of the concepts discussed in slides
Control Statements
• Control statements enable us to specify the order in which the various
instructions in the program are to be executed.
• This determines the flow of control.
• Control statements define how the control is transferred to other parts of
the program.
• C language supports four types of control- statements, which are as-
• if...else
• goto
• switch
• loop
• while
• do...while
• for
Compound Statement or Block
if...else
• This is a bi-directional conditional control statement.
• This statement is used to test a condition and take one of the two possible
actions.
• If the condition is true then a single statement or a block of statements is
executed (one part of the program),
• otherwise another single statement or a block of statements is executed
(other part of the program).
• Recall that in C, any nonzero value is regarded as true while zero is
regarded as false.
if...else
if...else
if...else
if...else
if...else
if...else
• We can have another if. .. else statement in the if block or the else block.
• This is called nesting of if... else statements.
• Here is an example of nesting where we have if...else inside both if block and else
block.
if (condition 1)
{
if (condition 2) • While nesting if...else statements,
statementA1; sometimes confusion may arise in
else associating else part with appropriate
statementA2; if part.
}
else
{
if(condition 3)
statementB1;
else
statement B2
}
if...else
if...else
#include<stdio.h> • The program finds whether a given year is
main( leap or not. A centennial (divisible by 100)
{ year is leap if it is divisible by 400, and a
int year; non centennial year is leap if it is divisible
printf (“Enter year:”); by 4.
scanf (“%d”, &year) ;
if (year%100==0)
{
if (year%400==0)
printf ("Leap year\n");
else
printf ("Not leap\n");
}
else
{
if (year%4==0)
printf ("Leap year\n");
else
printf("Not leap\n");
}
else if Ladder
else if Ladder
if...else
• Program to find out the grade of a student
else if (per>=40)
when the marks of 4 subjects are given.
grade= 'D' ;
else
#include<stdio.h>
grade='E';
main ( )
printf (“Percentage is %f\n Grade is
{
%c\n”, per, grade) ;
float ml, m2, ni3, m4, total, per;
}
char grade;
printf(“Enter marks of 4 subjects”);
scanf(“%f%f%f%f”,&ml,&m2,&m3,&m4) ;
total=ml+m2+m3+m4;
per=total/4;
if (per>=85)
grade= 'A' ;
else if(per>=70)
grade='B';
else if (per>=55)
grade= 'C' ;
if...else
Use of Logical Operators
#include<stdio.h>
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ("Enter marks in five subjects") ;
scanf ("%d %d %d %d %d", &m1, &m2,
&m3, &m4, &m5) ;
per = (m1 + m2 + m3 + m4 + m5) / 5 ;
if (per >= 60)
printf ("First division") ;
if ((per >= 50) && (per < 60))
printf ("Second division") ;
if ((per >= 40) && (per < 50))
printf ("Third division") ;
if (per < 40)
printf ("Fail") ;
}
THE SWITCH STATEMENT
• C has a built-in multiway decision statement known as a switch.
• The switch statement tests the value of a given variable (or expression) against a list of
values and when a match is found, a block of statements associated with that is executed.
The general form of the switch statement is as shown below:
THE SWITCH STATEMENT
• The expression is an integer expression or characters. Value-1, value-2 ..... are constants
or constant expressions (evaluable to an integral constant) and are known as case labels.
• Each of these values should be unique within a switch statement. .... are statement lists
and may contain zero or more statements.
• There is no need to put braces around these blocks.
• Note that labels end with a colon (:).
• When the switch is executed, the value of the expression is successfully compared
against the 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 statement at the end of each block signals the end of a particular case and causes an
exit from the switch statement, transferring the control to the following the switch.
• The 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.
THE SWITCH STATEMENT
Rules for Switch Statement
• The switch expression must be an integral type.
• Case labels must be constants or constant expressions.
• Case labels must be unique. No two labels can have the same value.
• Case labels must end with colon.
• The break statement transfers the control out of the switch statement.
• The break statement is optional. That is, two or more case labels may belong to the
same statements.
• The default label is optional. If present, it will be executed when the expression does not
find a matching case label.
• There can be at most one default label.
• The default may be placed anywhere but usually placed at the end.
• It is permitted to nest switch statements.
Rules for Switch Statement
Rules for Switch Statement
You can in fact put the cases in any order you please. Here is an example of scrambled case
order:
main( )
{
int i = 22 ; The output of this program would be:
switch ( i ) I am in case 22
{
case 121 :
printf ( "I am in case 121 \n" ) ;
break ;
case 7 :
printf ( "I am in case 7 \n" ) ;
break ;
case 22 :
printf ( "I am in case 22 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
Rules for Switch Statement
You are also allowed to use char values in case and switch as shown in the following
program:

main( )
{ The output of this program would be:
char c = 'x' ; I am in case x
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 'a' :
printf ( "I am in case a \n" ) ;
break ;
case 'x' :
printf ( "I am in case x \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}}
Thank you for your attention!

< Questions?

You might also like