if condition

“if condition” is a very important part of programming in this we execute statements / take decisions based on the given condition. Knowledge of “if condition” is very important for successful programming.

Here we have given all syntax of if condition and examples of each syntax.

C MCQs    C Discussion Forum

1.
if the specified condition is true then given a single statement (A) will get executed and if the condition is false nothing will happen.

Syntax:
if (condition)
Statement;//A

2.
if the specified condition is true then given a single statement (A) will get executed and if the condition is false the given single statement (B) will get executed.

Syntax:
if(condition)
   statement;//A
else
   statement;//B

3.
if the specified condition is true and we want to execute a set of statements then the statements have to be given within { }.
In the above syntax if the condition is true then set of statements A will get executed.

Syntax:
if(condition)
{//A
   Statements;
   Statements;
}

4.
if the specified condition is true and we want to execute a set of statements then the statements have to be given within { }.
In the above syntax if the condition is true then the set of statements A will get executed. on the other hand, if the condition is false then the set of statements B will get executed.

Syntax:
if(condition)
{//A
   Statements;
   Statements;
}
else
{//B
   Statements;
   Statements;
}

5.
Nested if condition

This syntax is used when we have multiple conditions to check and based on the condition set of statements are executed. if we have to execute a single statement then using { } is optional but if we have to execute a set of statements then statements have to be given within { }.

Syntax:
if(condition)
{
  Statements;
}
else
   {
     if(condition)
      {
         Statements;
      }
      else
      {
          if(condition)
          {
              Statements;
          }
         else
         {
             Statements;
          }
     }
}

6
Ladder if condition

This syntax is used when we have multiple conditions to check and based on the condition a single statement is to be executed.

Syntax:
if(condition)
Statement;
else
   if(condition)
   Statement;
   else
      if(condition)
      Statement;
      else
      Statement;

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects

Number Of Visitors

0238206