Lecture 6Part1Decision Control or Conditional Control Structures
Lecture 6Part1Decision Control or Conditional Control Structures
1. Wake up;
2. Get ready;
3. If you have enough time, then
eat breakfast;
©LPU CSE101 C Programming
4. Go to school.
Control Statements
• The C language programs until now follows a
sequential form of execution of statements.
• C language provides statements that can alter the
flow of a sequence of instructions. These statements
are called control statements.
• These statements help to jump from one part of the
program to another. The control transfer may be
conditional or unconditional.
Go!!!
Where
Class To Movie
Go?
Stop Stop
©LPU CSE101 C Programming
if statement
if (expression)
statement;
or
if (expression)
{
block of statements;
}
yes no
Clouds?
No rain
Raining
MESSAGE
DISPLAY
}
A. Nothing will be printed
B. Compile time error
C. Hello
D. Logical error
}
A. Nothing will be printed
B. Compile time error
C. Hello
D. Logical error
66.70
Grade D
or
78.00
Grade
©LPU C C Programming
CSE101
Q1
#include <stdio.h>
int main() A. inside if
{ B. inside elseif
int x = 1; C. inside if
if (x > 0) inside elseif
printf("inside if\n");
D. Compile time error
else if (x > 0)
printf("inside elseif\n");
}
// Nested - if statement
// Will only be executed if statement above is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Day= No Day=
Monday Sunday
Yes
case 2.5:
printf(“A line”); Floating point number
break; cannot be used
case 3 + 7.7:
printf(“A triangle”);
case 3 + 7.7: Floating point number
printf(“A triangle”); cannot be used and
break; same expression cannot
case count+5: be used
printf(“A pentagon”);
break;
} constant expression
should be used
©LPU CSE101 C Programming
#include<stdio.h>
int main()
{
int pt;
printf("Enter the number of nodes:");
Program to
scanf("%d", &pt);
switch(pt){ show switch
case 0:
printf("\nNo Geometry");
break;
statement in
case 1:
printf("\nA point");
break;
geometry
case 2:
printf("\nA line");
break;
case 3:
printf("\nA triangle");
break;
case 4:
printf("\nA rectangle");
break;
case 5:
printf("\nA pentagon");
break;
default:
printf("Invalid input");
break;
}
return 0;
}