C programming tutorial

                 PREVIOUS                                                                                   NEXT CHAPTER

If else Statement in C

If statement will execute a block of code when the condition inside if comes to be true.It dont do anything when condition inside if evaluates to false.If else statement will execute a group of statements if condition inside if comes to be true and an another group if condition evaluates to be false.

 

syntax

if(condition)
{
//statements;
}
else
{
//statements;
}

I will explain it using following C program.

int i=2;
if(i>3)
printf(" if else c ");
else
printf(" IF ELSE C ");

  1. In above program i was initialized to 2 in first step.
  2. condition inside if evaluates to false since i<3.
  3. Now only statement inside else is executed.statement inside if is ignored.
  4. Hence output will be IF ELSE C.

 

Note: remember ,In an if else statement,only the code associated with if or the code associated with else else executes,never both.

 



                 PREVIOUS                                                                                   NEXT CHAPTER