C Programming IT Programming Tutorials

Decision Control Statements

In a program, certain logical tests are carried out only when certain conditions are satisfied. The logical test gives either True or False as output, based on the result we execute different sets of statements and hence referred to as Decision Control Statements.


Types of Decision Control Statements:

1. if the statement
    2. an if-else statement
    3. Nested if-else statement
    4. switch case statement
    5. Nested switch case statement

if Statement

 If a process needs to be done only during certain conditions then if statement is used.

Syntax:   

if(condition)
{
      statements
    }

Example 1:

<script src="https://gist.github.com/GeethaaSrinivasan/f37c840fbf9497799d40ad66f838cc5f.js"></script>

Explanation:

If the entered a and b is equal, the condition becomes true and hence executes the statement inside the if.

if-else Statement

The if-else executes the statements following the ‘if’ when True, and the else part when false.

Syntax:

  if(condition)
    {
       Statements
    }
  else
    {
       Statements
    }

Example 2:

 <script src="https://gist.github.com/GeethaaSrinivasan/2259425475c25ebc38ddef37e33f4f2f.js"></script>

Explanation:    

The values 20 and 30 are not equal and hence the statement following if is not executed instead executes the else part.

Nested if-else Statement

To check multiple conditions, we can use the nested if-else statements.

Syntax:

   if(condition)
     {
       statement
     }
    else 
      { 
          if(condition)
           {
             statement
           }
        else
         {
          statement
         }
     }

Example 3:

<script src="https://gist.github.com/GeethaaSrinivasan/1a1c8a21912605ebab46d6759eaaff3a.js"></script>

Explanation:

  1. In the first ‘if’ case we compare the ASCII value of the entered letter with the ASCII value of range between 65 to 90 then execute the 1st printf statement.
  2. If the entered character ASCII value lies between 97 to 122 then executes the 2nd printf statement.
  3. If both the cases are not satisfied then it will execute the final else part.

Switch Case Statement

  1.  If we have multiple cases with a different block of code to execute during each case, then we can use switch case statements instead of multiple if-else statements.
  2. The Switch expression must be an integer or character type.
  3. The case value is used inside the switch with integer or character constants.
  4. Each case can have a break statement, it is optional.
  5. If no break at the end of each case then it will execute successive case statements one by one until it finds a break or default statement.
  6.  Default is used at the end of switch cases, if no other cases match the condition then default statements will be executed.

Syntax:
switch(expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}

Example 4:

 <script src="https://gist.github.com/GeethaaSrinivasan/9bb0382106e71383b81b38b1dcc75568.js"></script>

Explanation: 

  1. The first printf displays a list of all books with prices and asks to select the book then we get user input, here it is 3 which matches with case 3, which represents data structures & algorithms book and its price is 300.
  2. Entered n value is 2 which means the value of two data structures book is 2*300=600.
  3. The total price of the Data Structures and Algorithm book is 600.

Nested Switch Case

 If we use a switch case inside the case statement of another switch case then we call that a nested switch case.

Syntax:

  switch(expressions)
    {
    case value1:
    statements;
    break;
    case value2:
    statement;
    switch(expression2)
     {
      case value1:
      statements;
      break;
      case value2:
      statements;
      break;
      } 
   break;
   default:
   statements;
   }

Example 5:

<script src="https://gist.github.com/GeethaaSrinivasan/a8fa7dedb9de666b1ca0e32cfb8c4acf.js"></script>

Explanation:

The first statement lists the choice to enter the color and executes case 1 when the user selects 1. Then it lists choices to enter the primary colors red, green, and blue.

Here the c value entered is 2, which corresponds to the green color and hence prints green.

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *