Since decision making statements control the flow of
execution, they also fall under the category of control statements. Following
are decision making statements:
1. if statements
2. if....else statements
3. else if statement
4. Nested if...else statement
5. switch statement
if statement:
The if statement is a powerful decision making statement and is used to
control the flow of execution of statements. This is a bi-directional condition
control statements. This statement is used to test a condition and take one of
two possible actions, If the condition is true then a single statement or a
block of statements is executed (one part of the program), other wise another
single statement or a block of statements is executed (other part of the program).
In C, any non-zero value is regarded as true while zero is regarded as false.
syntax :
if (condition)
if (condition)
statement1 ; {
statement1 ;
- - - - -
statement n ;
}
Here if the condition is true (non-zero) then
statement1 is executed, and if it is false(zero), then the next statement which
is immediately after the if control statement is executed.
For eg:
|* ### Program to check whether the number is –ve or
+ve ### *|
#include<stdio.h>
main()
{
int num
;
printf
(“Enter a number to be tested:” ) ;
scanf
(“%d”, &num) ;
if
(num<0)
printf(“The
number is negative”) ;
printf
(“value of num is : %d|n”, num) ;
getch()
;
}
Output :
1st run
Enter a
number to be tested : -6 ↵
The
number is negative
Value of
num is : -6
2nd run
Enter a
number to be tested : 6 ↵
Value of
num is : 6
if...else
statement:
The if..else statement is an
extension of the simple if statement. It is used when there are two possible
actions – one when a condition is true and the other when it is false.
The syntax is :
if (condition)
if (condition)
statement1 ; {
else statement
;
statement2; } -
- - -
else
{
statement ;
- - - -
}
Here if the condition is true
then statement1 is executed and if it is false then statement2 is executed.
After this the control transfers to the next statement which is immediately
after the if...else control statement.
|* Program to check whether the number is even or odd
*|
#include<stdio.h>
main()
{
int num,
remainder ;
printf
(“Enter a number:”) ;
scanf
(“%d”, &num) ;
remainder
= num%2 ; |* modular division *|
if
(remainder = = 0) |* test for even *|
printf
(“Number is even|n”) ;
else
printf
(“ Number is odd|n”) ;
}
Output:
Enter a
number : 15 ↵
Number
is odd.
Nested if
...else statement:
We can have another if... else statement in the if
block or the else block. This is called nested if..else statement.
For example
if(condition1)
{
if(condition2)
statementA1;
else
statement A2;
//Here, we have if...else inside both if block and else block
}
else
{
if(condition3)
statementB1;
else
statementB2;
}
|* Program to
find whether a year is leap or not *|
#include<stdio.h>
main()
{
int
year,
printf
(“Enter year: ”) ;
scanf
(“%d”, &year) ;
if(year%100
= = 0)
{
if(year%400
= = 0)
printf
(“Leap year \n”) ;
else
printf
(“Not leap year\n”) ;
}
This can also be written in place of nested if else
as
if ((year%4 = =
0 && year %100!=0) | | year%400 = = 0)
printf (“%d is
a leap year\n”, year) ;
else
printf (“%d is
not a leap year\n”, year) ;
|* Program to
find largest number from three given number *|
#include<stdio.h>
main()
{
int a,
b, c, large ;
printf
(“Enter three numbers : ”) ;
scanf
(“%d%d%d”, &a, &b, &c) ;
if
(a>b)
{
if
(a>c)
large =
a ;
else
large –
c ;
}
else
{
if
(b>c)
large =
b ;
else
large =
c ;
}
printf
(“Largest number is %d\n”, large) ;
} /* End
of main() */
Output:
Enter
the numbers: 3 4 5 ↵
Largest
num is 5
else if
statement:
This is a type of nesting in
which there is an if...else statement in every else part except the last else
part. This type of nesting is frequently used in programs and is also k/a else
if ladder.
if(condition1) if(condition1)
statementA ; statementA ;
else elseif(condition2)
if(condition2) statementB ;
statementB ; elseif(condition3)
else statementC ;
if (condition3) else
statementC ; statement D ;
else
statement D ;
|* Program to
find out the grade of a student when the marks of 4 subjects are given. The
method of
assuming grade is as
per>=80
grade = A
per<80 and
per>=60 grade = B
per<60 and
per>=50 grade = C
per<50 and
per>=40 grade = D
per<40
grade = F
Here Per is
percentage *|
#include<stdio.h>
main()
{
float
m1, m2, m3, m4, total, per ;
char
grade ;
printf
(“Enter marks of 4 subjects : ”) ;
scanf
(“%f%f%f%f”,&m1, &m2, &m3, &m4) ;
total =
m1+m2+m3+m4 ;
per =
total /4 ;
if(per>=80)
grade =
‘A’ ;
elseif(per>=60)
grade =
‘B’ ;
elseif(per>=50)
grade =
‘C’ ;
elseif(per>=40)
grade =
‘D’ ;
else
grade =
‘F’ ;
printf(“Percentage
is %f\n Grade is %c\n”, per, grade) ;
}
Equivalent code
in simple if statement:
if(per>=80)
grade =
‘A’ ;
if(per<80
&& per>=60)
grade =
‘B’ ;
if(per<60
&& per>=50)
grade =
‘D’ ;
if(per<40)
grade =
‘F’ ;
In else_if ladder whenever a condition is found true
other conditions are not checked, while in if statement all the conditions will
always be checked wasting a lot of time and moreover the conditions here are
more lengthy.
Comments
Post a Comment