Tuesday 1 October 2013

if else in C

Operator                                                                                                                        Looping


if else:-  if else is used to provide condition . in the if() we insert the conditions.
it executes only one block either if or else.
condition true executes if block
condition false executes else block

Ex;-
int i ,j;
if(i>j)
{
printf("i is greater than j");
}
else
{
printf("i is less than j");
}

output:-
if we initialize i=4 and j =3
it executes the if block only
if we initialize i=3 and j =4
it executes the else block only.

Nested if else:- writing if else in the if else

ex:-
if(condition)
{
    if(condition)
      {
      }
    else
     {
     }
}
else
{
}

output:-first it check the condition if condition true it go into the if and in the if it again check the condition of nested if if condition true  printf if statement condition false prints nested else statement .
if condition of outer if false then it prints the statement of outer else.

 for providing multiple conditions we use else if

ex:-
int i , j;
if(i>j)
printf(" i greater than j);
else if(i<j)
printf("i less than j");
else if(i==j)
printf("i equal to j");





No comments :

Post a Comment