Decision
If we want to execute one set of statements when the condition is satisfied
and another set of statements when the condition is not satisfied, we use
decision making statements. The different type of decision making
Statements are as follows:
1. The if-statement
2. The if-else statement
3. The Nested-if-else statement
1. The if statement:
The if statement executes a set of statements only if the condition is
satisfied. The general syntax for if statement is:
if (condition)
{Set of statements;
}
Example: Write a program that accepts a number from the keyboard if the number is less than10. The program should display a message.
Solution:
Logic --> here we input any one random number from user and test whether
that provided number is less than 10 and display the required result as
examined.
Step 1: Input any number.
Step 2: Test provided number with 10...whether it is less than 10.
Step 3: Display result as condition match otherwise do nothing.
Program-->
#include<stdio.h>
#include<conio.h>
void main()
{int x;
clrscr();
printf("Input any number");
scanf("%d",&x);
if (x<10)
printf("The number is less than 10");
getch();
}
Output: Input any number....5
The number is less than 10
2. The if-else statement:
The if-else statement executes one group of statements, if the condition is
satisfied and another group of statements if the condition is false. The
general syntax for if-else statement is:
if (condition)
{Block of statement;
}
else
{Block of statement;
}
Example: Write a program that accepts a two number from the keyboard and check which one largest among them.
Solution:
Logic--> Here we take any two numbers from user and check which one
largest among them using condition. Condition here test true and false both. If
one statement is true than it take that as a result else go for next option of
statement.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{int a,b;
clrscr();
printf("Input any two number");
scanf("%d%d",&a,&b);
if (a>b)
printf("Largest number is=",a);
else
printf("Largest number is=",b);
getch();
}
Output: Input any two number...10 20
Largest number is 20
3. The Nested-if-else statement:
If an if-else statement written within the body of another if statement or
the body of an else statement, this is called nested-if-else statement. The
general syntax for nested-if-else statement is:
if (condition)
{if (condition)
{Block of statement;
}
else
{Block of statement;
}
}
else {
if (condition)
{block of statement;
}
else
{Block of statement;
}
}
Example: Three numbers are input through keyboard. Write a program to find the largest of them?
Solution:
Logic--> Here we take three numbers from user and check each of them
using nested-if-else condition. Let we have a,b and c as three number.
Step 1: Test "a" and "b" number first. If "a"
is greater than "b" then we go for check with "c" and if
satisfied, display "a" is largest
Step 2: If "a" is not largest then display "c" is
largest as "a" is greater than "b" is already satisfied.
Step 3: If Step 1 fail, we come in this step. As we get now, "b"
is largest than "a", so we go for check with "c" and if
satisfied, display "b" is largest otherwise "c" is largest
automatically.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{int a,b,c;
clrscr();
printf("Input any three numbers");
scanf("%d%d%d",&a,&b,&c):
if (a>b)
{
if (a>c)
printf("Largest number is=",a);
else
printf("Largest number is=",c);
}
else
{if (b>c)
printf("Largest number is=",b);
else
printf("Largest number is=",c);
}
getch();
}
Output: Input any three numbers....10 25 100
Largest number is 100
No comments:
Post a Comment