2. The Decision Control Structure
âą Decision making statements are used to skip or to execute a group of
statements based on the result of some condition.
âą The decision making statements are,
â simple if statement
â ifâŠelse statement
â nested if
â else ⊠if ladder
â switch statement
â goto
âą These statements are also called branching statements
3. IF Statement
âą if statement executes a single statement or a block of statements if a
boolean expression evaluates to true.
Syntax:
If(condition)
{
Statements;
}
Here condition is a boolean expression
If condition is true, then the statement is executed
If condition is false, than the statement is bypassed
5. Simple if - Example
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
printf("Enter any two numbers ");
scanf("%d %d",&num1,&num2);
if(num1>num2)
printf("nNum1 is greater than Num2");
printf("nEnd of program");
getch();
}
Output1
Enter any two numbers 10 5
Num1 is greater than Num2
End of program
Output1
Enter any two numbers 10 50
End of program
6. IF Else Statement
âą An if statement can include an else clause that executes a statement or block
if the boolean expression is not true.
Syntax:
If (condition)
Statement1;
Else
Statement2;
âą If condition is true, then statement1 is executed.
âą Otherwise, statement2 is executed.
8. if â else Example
# include <stdio.h>
void main ()
{
int num;
printf ("Type a number:");
scanf ("%d", &num);
if (number < 0)
printf(âThe number is negativeâ);
else
printf(âThe number is positiveâ);
}
Output
Type a number 50
The number is positive
9. if â else Example
#include<stdio.h>
void main()
{
Int num;
printf ("Enter a number:");
scanf ("%d",&num);
if (num%2==0)
printf ("The number is
EVEN.n");
else
printf ("The number is
ODD.n");
}
Output
Enter a number 125
The number is ODD
10. if â else Example
#include<stdio.h>
#include<conio.h>
void main (void)
{
int percentage;
printf(âEnter your percentage: â);
scanf(â%dâ, &percentage);
if(percentage>=60)
printf(âYou are Passedâ);
else
printf(âYou are failedâ);
getch();
}
11. Else - if Ladder Statement
âą An if statement can be followed by an optional else
if...else statement, which is very useful to test various
conditions using single if...else if statement.
When using if , else if , else statements there are few points to
keep in mind:
âą An if can have zero or one else's and it must come after any
else if's.
âą An if can have zero to many else if's and they must come
before the else.
âą Once an else if succeeds, none of the remaining else if's or
else's will be tested.
12. Else - if Ladder Statement
Syntax
if (condition1)
statement block 1;
else if (condition2)
statement block 2;
else if (condition3)
statement block 3;
:
:
else if (condition)
statement block n;
else
default statement;
14. Else - if Ladder Example
#include<stdio.h>
#include<conio.h>
void main(void)
{
int num1,num2;
clrscr();
printf(âEnter integer numbers to check: â);
scanf(â%d %dâ,&num1,&num2);
if(num1>num2)
printf(ânum1 is greater than num2â);
else if(num1<num2)
printf(ânum1 is less than num2â);
else
printf(âboth numbers are equalâ);
getch();
}
15. Else - if Ladder Example
#include <stdio.h>
void main ()
{
float perc;
printf ("Enter Percentage:");
scanf ("%f", &perc);
if (perc <= 100 && perc >= 80)
printf ("n A-1 Grade");
else if (perc >= 70)
printf("n A Grade");
else if (perc >= 60)
printf ("n B Grade");
else
printf ("Fail");
}
Output
Enter Percentage: 75
A Grade