C++ Nested if-else statement
It is a conditional statement which is used when we want to check more than 1 conditions at a time in a same program. The conditions are executed from top to bottom checking each condition whether it meets the conditional criteria or not. If it found the condition is true then it executes the block of associated statements of true part else it goes to next condition to execute.
Syntax :
if(condition)
{
if(condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the braces and again checks the next condition. If it is true then it executes the block of statements associated with it else executes else part
Example:
#include <iostream.h>
#include <conio.h>
void main()
{
int n;
clrscr();
cout <<Enter Number :";
cin >> n;
if(n>0)
{
cout << "\n\n Number is greater than 0 !";
}
else
{
if(n==0)
{
cout << "\n\n It is 0 !";
}
else
{
cout << "Number is less than 0 !";
}
}
getch();
}
Output :
Description:
If user enter 0 then the if condition will become false and hence thelse part
will execute in which one more if condition is present and since it becomes true
the if inside else is executed.
3 comments:
When i go to run the program c++ says that i need an ; before the else. If i put an ; in front of the else c++ still says the same thing. What is wrong with the program?
Please provide me the code that is causing error, i will definitely help you out..!!!
C
C++
is
VERY
VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY BORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORINGBORING
BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING BORING
Post a Comment