Professional Documents
Culture Documents
Lecture 03
if else
Control or decision making statement
• if statement
– The if statement evaluates the expression inside
the parenthesis.
– If the expression is true (nonzero), statements
inside the body of if is executed.
– If the test expression is false (0), statements inside
the body of if is skipped from execution.
if (Expression)
{
// statements
}
Flowchart of if statement
The if statement
#include <stdio.h>
main(){
int a,b;
printf("take two inputs:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf(“%d is greater“,a);
}
printf(“end of program”);
}
The if else statement
• if (Expression)
{
// codes inside the body of if
}
else {
// codes inside the body of else
}
• If expression is true, codes inside the body of if
statement is executed and, codes inside the body of
else statement is skipped.
• If expression is false, codes inside the body of else
statement is executed and, codes inside the body of
if statement is skipped.
Flowchart of if...else statement
The if else statement
#include <stdio.h>
main(){
int a,b;
printf("take two inputs:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf(“%d is greater“,a);
}
else
{
printf(“%d is greater“,b);
}
printf(“end of program”);
}
Nesting of if else statement
• When an if statement appears inside the other
it is called nesting of if statements.
• Nesting of if statements is very helpful when
you have something to do by following more
than one decision.
Nesting of if else statement
void main(){
int a,b,c;
printf("take three nmbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{printf("%d is greater",a);}
else
{printf("%d is greater",c);}
}
else
{
if(b>c)
{printf("%d is greater");}
else
{printf("%d is greater",c);}
}
}
The else if ladder