Beruflich Dokumente
Kultur Dokumente
• Control structure
Decision Statements
• If statement
• If-else statement
• Switch statement
– Selection structures
• if, if…else, switch
©LPU CSE101 C Programming
Condition Statements
– if statement
– if-else statement
©LPU CSE101 C Programming
Daily routine
Start
Go!!!
Where
Class To Movie
Go?
Stop Stop
©LPU CSE101 C Programming
if statement
• If statement
– It is decision making statement uses keyword if.
– It allows the computer to evaluate the expression
first
• and then, depending on whether the value is ‘true’
or ‘false’, i.e. non zero or zero it transfers the
control to a particular statement.
A decision can be made on any expression.
zero - false
nonzero - true
Example:
3 < 4 is true
©LPU CSE101 C Programming
if Statement
Syntax
if (expression)
statement;
or
if (expression)
{
block of statements;
}
yes no
Clouds?
No rain
Raining
if(v<10)
}
Enter the number: 6
Number is less than 10
Grab
something
to eat along
MESSA
GE
DISPLAY
Syntax
if ( condition ) {
block of statements;
}
else if ( condition ) {
block of statements; }
else {
block of statements;
}
if(v<10){
else if(v<100){
Enter the number: 1
Number is less than 10
printf(“number is less
or than 100”);
Enter
} the number: 56
Number
©LPU isC less
CSE101 than 100
Programming
Forms of if
The if statement can take any of the following forms:
if ( condition ) {
if ( condition )
do this ;
do this ;
and this ;
or
}
if ( condition ) {
else {
do this ;
do this ;
and this ;
and this ;
}
}
if ( condition )
do this ;
if ( condition ) else if ( condition )
do this ; do this ;
else else {
do this ; do this ;
and this ;
©LPU CSE101 C Programming }
#include<stdio.h>
Program to
void main()
print grades
{
of students
float marks;
marks.
scanf(“%f”, &marks);
if (marks>90){
printf(“Grade A”);
else if (marks>80) {
printf(“Grade B”);
66.70 }
Grade D
else if(marks>70){ or
78.00 printf(“Grade C”);
Grade
©LPU C C Programming
CSE101
Forms of if
Decision control Syntax Description
statements
if if (condition){ In these type of statements, if condition is
Statements;} true, then respective block of code is
executed.
if…else if (condition){ In these type of statements, group of
Statement1; statements are executed when condition is
Statement2;} true. If condition is false, then else part
else { statements are executed.
Statement3;
Statement4;}
Nested if if (condition1){ If condition 1 is false, then condition 2 is
Statement1;} checked and statements are executed if it
else is true. If condition 2 also gets failure, then
if(condition2){ else part is executed.
Statement2;}
else
©LPU CSE101 C ProgrammingStatement 3;
break statement
• break is a keyword.
• break allows the programmer to terminate
the loop.
• A break statement causes control to transfer
to the first statement after the loop or block.
• The break statement can be used in nested
loops. If we use break in the innermost loop
then the control of the program is terminated
only from the innermost loop.
©LPU CSE101 C Programming
switch Statement
Day= No Day=
Monday Sunday
Yes
int pt;
statement
printf(“Enter the number of nodes:”);
in geometry
scanf(“%d”, &pt);
switch(pt){
case 0:
printf(“\nNo Geometry”);
break;
case 1:
printf(“\nA point”);
Enterbreak;
the number of nodes: 2
A line
©LPU CSE101 C Programming
case 2:
#include<stdio.h>
• Program to
void main()
move a car
{
in car game
int key;
scanf(“%d”, &key);
switch(key){
case 1:
Press 1 to turn left.
printf(“\nTurn left”);
Press 2 to turn right.
Pressbreak;
3 to increase speed.
©LPU CSE101 C Programming
Next Class: Loop control and
Jump statements