C Decision Making Statements

Sometimes, we need to change the order of execution of statements depending on some specific conditions. This is possible with the help of C Decision Making Statements.

C conditional statements allow us to make a decision, based upon the result of a condition. C language assumes any non-zero and non-null value as true while zero and null value treated as false.

C language supports following Decision Making statements:

  1. if statement
  2. switch statement
  3. conditional operator statement

  1. If Statement

  2. With the help of If statement we can test a condition if it is true or false. If the condition is true then the code inside the if statement will be executed otherwise that code will not be executed.

    Here are the different variations available in if statement.

    1. If statement
    2. If-else statement
    3. If else-if ladder statement
    4. Nested if statement

    1. If Statement

    2. The Simple if statement in C language is used to perform an operation on the basis on one condition (also called an expression). If the expression is true then the statement gets executed otherwise these statements are skipped.

      Syntax of If Statement

      if (expression)
      {
      // code which is to be executed
      }

      Simple program using If Statement


    3. If-else Statement

    4. In the previous example, only if the condition is true then the code will be executed. Now, the question arises that if the condition is false then what will be executed?
      So, with the help of the if-else statement, we can perform the operation on the basis of if the condition is false. If the condition is true, then the code inside if statement will execute. And if the condition is false then code inside the else statement executes.

      Syntax of If-else Statement

      if(condition)
      {
      // code to be executed when the condition is true
      }
      else
      {
      // code to be executed when the condition is false
      }

      Simple program using if-else Statement:

      Here, with the help of if-else, we will check if a student is pass or not.


    5. If else-if ladder Statement

    6. By using if-else-if ladder statement in C programming language, we can test the sequence of conditions.

      Syntax of If else-if ladder Statement

      if(condition-1)
      {
      // this code will execute when condition-1 is true
      }
      else if(condition-2)
      {
      // this code will execute when condition-2 is true
      }
      else if(condition-3)
      {
      // this code will execute when condition-3 is true
      }

      else
      {
      // this code will execute only if all the above conditions are false
      }
      This is how If else-if ladder Statement works. Next if condition is executed when all previous if conditions in the if-else ladder are false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits from the whole if-else ladder.

      Example of If else-if ladder Statement


    7. Nested if Statement in C

    With the help of Nested if Statement in C, we can use one if or else if statement inside another if or else if statement. Nested if statements mean an if statement inside another if statement. Now the question arises that why we use nested if statement?.
    Sometimes, a choice has to be made from more than 2 possibilities. The nested if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions. A nested if is an if statement that is the target of another if statement.

    Syntax of Nested if Statement

    if( expression 1)
    {
    /* Executes when the expression 1 is true */
    if(expression 2)
    {
    /* Executes when the expression 2 is true */
    }
    }

    Example of Nested if-else


  3. Switch statement in C

  4. Learn about Switch statement in this tutorial.


  5. Conditional operator statement

    1. Conditional operator statement is based on functioning of Conditional Operator/Ternary Operator. Now the question arises that What is ternary Operator?
      Ternary Operator returns first value if the condition is true and if the condition is false then return another value. This operator is used on three operands. Ternary Operator is represented by ? : ..

      Syntax of Conditional operator statement

      (Condition? true_value: false_value);

      Example of Ternary Operator


      Congratulations! Chapter Finished. Do you want to practice more?
      Exercises & Assignments
    2. No Content Found.
    3. Interview FAQs
    4. No Content Found.