Decision making is about deciding the order of execution depending upon certain conditions.
Decision making with ‘if’ statement:
The ‘if’ statement can be implemented in a different form depending upon the complexity of the conditions to be tested. The forms are as follows:
- Simple ‘if’ statement
- if … else statement
- Nested if .. else statement
- Using else if statement
Simple ‘if’ statement:
The general syntax for this form is:
if (expression)
{
Inside statement
}
Outside Statement
This checks for particular expressions and the control passes to the “if” block only when the expression happens to be true else the statements insides the ‘if’ block are skipped.
Example:
#include <stdio.h>
int main ()
{
int num1 = 15;
int num2 = 10;
if (num1 > num2)
{
printf ("\n The first number is greater\n");
}
return 0;
}
Output:
The first number is greater
if .. else statement:
The general syntax for this form is:
if (condition)
{
Statement Block1
}
else
{
Statement Block2
}
If the expression happens to be true, the statement block1 is executed and the statement block2 is skipped. If the expression happens to be false statement block1 is skipped and the statement block2 is executed.
Example:
#include <stdio.h>
int main ()
{
int num1 = 5;
int num2 = 10;
if (num1 > num2)
{
printf ("\n The first number is greater\n");
}
else
{
printf ("\n The second number is greater\n");
}
return 0;
}
Output:
The second number is greater
Nested if .. else statement
The general syntax for this form is:
if (expression)
{
if (expression2)
{
statement1;
}
else
{
statement2;
}
}
else
{
if (expression3)
{
statement3;
}
else
{
statement4;
}
}
If the expression happens to be true, expression 2 will be evaluated and if it happens to be false expression 3 will be evaluated and either statement3 or statement4 will be executed.
#include <stdio.h>
int main ()
{
int num1 = 10;
int num2 = 7;
int num3 = 21;
if (num1 %2 == 0)
{
printf ("\n The first number is even\n");
if (num1 > num2)
{
printf ("\n The first number is greater\n");
}
else
{
printf ("\n The second number is greater\n");
}
}
else
{
printf ("\n The first number is odd\n");
if (num1 > num3)
{
printf ("\n The first number is greater\n");
}
else
{
printf ("\n The third number is greater\n");
}
}
return 0;
}
else if ladder
The general syntax of this form is:
if (expression)
{
statement block1;
}
else if (expression2)
{
statement block2
}
else if (expression3)
{
statement blcok3
}
else
{
statement 4
}
Examples:
#include <stdio.h>
int main ()
{
int num1 = 5;
int num2 = 6;
int num3 = 10;
if (num1 > num2)
{
printf("\n The first number is greater\n");
}
else if (num2 > num3)
{
printf("\n The third number is greater\n");
}
else
{
printf("\n The third number is greater\n");
}
return 0;
}
Note:
- If there is a single statement within the if block, curly braces( { } ) can be avoided.
int num = 5;
if (a >10)
printf("\n Number is greater than 10\n");
- == must be used for comparison in the expression of “if” statement. The = will always be evaluated to be true.
- Other than 0, all other values are considered as true
if (100)
printf ("\n Hello world\n");
In the above example, Hello world is always printed.
Relevant Posts:
Categories: C Language
Leave a Reply