C-Lecture-4 Control Statement
C-Lecture-4 Control Statement
Decision Making
In practice, users may have a number of
situations where they may have to change the
order of statements based on certain
conditions. This is decision making.
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
return 0;
}
Nested if else statement
When a series of decisions are involved, we
may have to use more than one if else
statement in nested form as shown below:
Nested if else statement (con’d)
#include <stdio.h>
int main( )
{
char letter;
printf(“Enter a character ?”);
scanf(“%c”, &letter);
if (letter >= ‘A’)
{
if (letter <= ‘Z’)
printf(“The character is within A to Z\n”);
}
else
printf(“The character is not Alphabet\n”)
return 0;
}
Nested if else statement (con’d)
#include <stdio.h>
void main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>b)
{
if(a>c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
The else if ladder
When a multipath decision is involved then we use else if ladder. A
multipath decision is a chain of ifs in which the statement associated
with each else in an if. It takes the following general form:
The else if Ladder (con’d)
#include<stdio.h>
void main(){
int marks;
printf("Give the value of percentages\n");
scanf("%d", &marks);
if(marks > 80)
printf("Grade : A");
else if(marks > 60
printf("Grade : B");
else if(marks > 50)
printf("Grade : C");
else if(marks > 40)
printf("Grade : D");
else
printf("Grade : Fail");
}
#include<stdio.h>
int main()
{
float m1,m2,m3,m4,m5,avg,per;
printf("Enter 5 subject marks out of 100:\n");
scanf("%f %f %f %f %f",&m1,&m2,&m3,&m4,&m5);
per = (m1+m2+m3+m4+m5)*100/500;
printf("Marks is %0.2f percentage. \n So your result is ",per);
if(per>=60)
printf("1st Division");
else if(per>=50 && per<=59)
printf("2nd Division");
else if(per>=40 && per<=49)
printf("3rd Division");
else if(per<40)
printf("Fail");
return 0;
}
Switch Statement
switch (expression)
{
case value 1:
program statement;
case value 2:
program statement;
........
break;
case value n:
program statement;
.............
break;
default:
............
break;
}
Switch Statement
void main ()
{ char grade ;
printf("\nEnter your grade to check [Input must be A,B,C,D or F]\n\n");
scanf("%c",&grade);
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" ); }
#include<stdio.h>
int main()
{
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is: %d",big);
return 0;
}
goto statement
When compiler encounters goto statement in a C program,
the control
jumps to the corresponding label mentioned along with goto.
goto statement (con’d)
#include<stdio.h>
int main()
{
int n=0;
a: n=n+1; /* 'a:' is the label */
printf("\t %d",n);
if(n>=100)
{
goto b;
}
goto a; /* Will go to the 'a' label
*/
b: printf("\n End of the program"); /* 'b' label */
}
Write a program that will read the value of x and evaluate the following function
y= { 1 for x >0
0 for x=0 (a) nested if statements.
-1 for x<0 (b) conditional operator ?
Solution:
/*…………evaluate the (b)conditional operator:
equation………..*/
#include<stdio.h>
(a)nested if statements:
int main()
#include<stdio.h>
int main() {
{ float y, x;
float x,y; printf("Input x\n");
printf("Input x\n"); scanf("%f",&x);
scanf("%f",&x); y=(x!=0)? ((x>0)?1:-
if(x!=0) 1) :0;
{
printf("%d",y);
if(x>0)
printf("y=1"); return 0;
if(x<0) }
printf("y=-1");
}
if(x==0)
printf("y=0");
return 0;
#include <stdio.h>
int main()
{
int year;
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
#include <stdio.h>
int main() {
int year;
year = 2016;
return 0;
}