decision making Statements:
decision Making Statements are used to make a choice among a no. of options eg. to check weather the number is +ve or -ve, too find the division of Student ie. Ist, 2nd or Fail.
Types:
a)If condition only: It execute the statement if given condition evaluate true.
Syntax:
if(TRUE)
{
Statements
}
b)
else-if: if condition evaluate is true then execute if block and if condition is false then execute else block. we can also say that it is used to make choice between 2 options only. eg. to check student is pass or fail.
Syntax:
if(TRUE)
{
Statements;
}
else
{
Statements;
}
in this we not need to give any condition in else because else means it is opposite to the condition given in if.
c)
Ladder If-else: Used to make choice among more than two options.
if(condition)
{
statement;
}
else if(condition)
{
statement;
}
else if(condition)
{
statement;
}
else
{
statement;
}
d)
Nested If else: It is also used to make choice among more than 2 options. in this if-else statements are nested in if and else blocks.
Syntax:
if(condition)
{
if(TRUE)
{
Statements;
}
else
{
Statements;
}
}
else
{
if(TRUE)
{
Statements;
}
else
{
Statements;
}
}
program to check given number is positive or negative:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<"Enter any number\n";
cin>>n;
if(n<0)
{
cout<<"Given number is negative";
}
else
{
cout<<"Given number is positive";
}
getch();
}
No comments:
Post a Comment