Q What do you mean by decision making in C? Which are the statements in C that provides the facilities of decision making and branching? Explain nested if else statement with suitable Example?
Decision Making is related to choosing one out of two statement on the basis of some condition like suppose we want to take decision that is Ram eligible for Job? then in this situation we are going to check for age of Ram. If the age of Ram is greater than 18 then yes otherwise no. Such a situation in c is handled using Conditional Statement. Conditional statements in C are
used for decision making. Based on the result of a set of conditions, decisions
are made. One or more conditions are required to be evaluated by the program,
having statement(s) to be executed if the condition evaluates to true and statement(s) to be executed if the condition
evaluates to false.
Statements which provide the
facility of decision making are:
·
if
statements
·
if else
statements
·
nested
if else statements
·
if-else-if
ladder
· switch statements
Statements which provide the
facility of branching are:
·
break
·
continue
·
return
·
goto
Nested if else statement- Using a if else statement inside another if else statement is called a nested if…else statement. It is basically used to test when there are multiple conditions.
Syntax:
If(test condition)
{
if(test
condition)
{
statements;
}
else
{
statements;
}
}
else
{
statement;
}
Example: Program to
find the greatest of three numbers entered by the user.
In this example we have to think about our self suppose I have three number in order to find greatest of three numbers we perform following steps
let A, B, C are three variable which are going to hold the value of three number we have to compare.
Step 1: we first compare any two number suppose A and B
Step 2: in this step we are going to compare biggest number out of A and B with 3rd number.
Step 3: print the greatest number out of A, B, C.
#include<stdio.h>
void main()
{
int x, y, z;
printf(“Input
three numbers:\n ”);
scanf(“%d%d%d”,
&x, &y, &z);
if(x>y)
{
if(x>z)
{
printf(“%d
is the greatest number”, x);
}
else
{
printf(“%d
is the greatest number”, z);
}
}
else
{
if(y>z)
{
printf(“%d
is the greatest number”, y);
}
else
{
printf(“%d
is the greatest number”, z);
}
}
}
No comments:
Post a Comment