explain if and if else statement .how if and if else are differ from each other .explain with the help of suitable example. (length 2-3 pages )
Answers
Explanation:
if and else are use to check the condition before executing any code
if you have ONLY one condition to check u can use only IF as ELSE is not mandatory
but to use ELSE , IF statement is compulsory.
eg
without else
int a= 10;
if ( a> 5)
{
cout<<" hello world";
}
in this hello world will get print a the condition inside the bracket are true
eg with else statement
int a= 20;
if (a< 10)
{
cout<<" this is if statement";
}
else
{
cout<<" this is else statement";
}
in this else statement will execute bcz the condition in IF statement is not true bcz a is greater than 10 so the other part will execute.
u can use multiple if else condition like
if( a%3==0)
{
cout<<" statement 1";
}
else if ( a%5==0)
{ cout<< " statement 2";
}
else if ( a%7==0)
cout<<" statement 3";
else
cout<< "statement 4";
in these kind if any of the condition is true it will execute that block of code and will skip all other blocks
like in this eg it will print statement 2 and will skip all other block of statement
and if non of the condition is true and u have ELSE block then it will execute that .