Академический Документы
Профессиональный Документы
Культура Документы
Control structure
Decision Statements
If statement
If-else statement
Switch statement
1. Wake up;
2. Get ready;
3. If you have enough time, then
eat breakfast;
4. Go to school.
LPU CSE101 C Programming
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
Grab
something to
eat along
MESSAG
E
DISPLAY
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>
void main() Program to
{
float marks;
scanf(%f, &marks);
print grades
if (marks>90){
printf(Grade A);
of students
}
else if (marks>80) {
marks.
printf(Grade B);
}
else if(marks>70){
printf(Grade C);
}
else if (marks >60) {
printf(Grade D);
}
}
66.70
Grade D
or
78.00
Grade C C Programming
LPU 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.
ifelse 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.
Day= No Day=
Monday Sunday
Yes