Wednesday, October 29, 2025

Decision Making in C (if - else)

 

Decision Making in C (if - else)

 

 

The if statement is powerful decision making statement in C. It is used to control the flow of execution of execution.

Types of if Statements in C

  1.   Simple if Statement
  2.   if...else  Statement
  3.   Nested if...else  Statement
  4.   if-else-if Ladder

 

Simple if Statements

The syntax of a Simple if Statement is

       if(condition)

            statement1;

       statements; 

  if condition is true then executed statement1 and followed statements. if condition is false executed statements.

 

The statement-block.

       if(condition)

      {  

            statement1;

            statement2;

            ---------

            statement n;

      }

      statements;

 

if condition is true then executed Block statements and followed statements. if condition is false executed statements.

 

if...else  Statement

 

        if(condition)

       {

            statement 1;

            statement 2;

            ----------

            statement n;

       } 

       else

       {

            statement 1;

            statement 2;

            ----------

            statement n;

       } 

       statements;

 

if condition is true then executed true Block statements and followed statements. if condition is false then executed false block statements and followed statements.

 

Nested if...else  Statement

Nested Condition: Condition in Condition is called Nested Condition.

 

         if(condition)
            if(condition)
               statement 1;
            else
               statement 2;
          else

            statement 3;

 

if condition is true then executed next if condition and it is true then excited statement1. if second if false  and executed statmenet2.if condition is false executed statements3.

 

if-else-if Ladder

 

The if-else-if Ladder is multipath decision.

   if(condition1)

      statement 1;

  else if(condition2)

      statement 2;

  else if(condition3)

      statement 3;

  -------

  else if(condition n)

      statement n;

 

 

No comments:

Post a Comment