C Decision Making with Code Examples

In this tutorial, we will learn the decision making in C which is the structures must have and the programmer must define one or more statement to figure out. On the other hand it will be tested by the program. Within a statement to be executed when the condition is single minded to be true. Besides, the other statement will be executed when the condition is resolute to be false.

In the diagram below is the common form of a typical decision making structure. Which is found in most programming languages.

Flowchart Diagram for C Decision Making

In C program, we assume that any non-zero and non-null values as true, and if it is zero or null, then we assumed a false value.

Five Types of Decision Making in C

In the table below are the five types of decision making in C:

Sr.No.Statement TypeDescription
1if statementThe if statement contains a Boolean expression and followed by one or more statements.
2if…else statementThe if statement will be followed by an optional else statement, through executions if the Boolean expression is false.
3nested if statementsWe can use single if or else if statement inside in another if or else if statement(s).
4switch statementThe switch statement allows a variable to be checked for equality against a list of values.
5nested switch statementsWe can use single switch statement inside another switch statement(s).

If Statement in C with Example

In C language, the if statement contains a Boolean expression and followed by one or more statements.

Syntax:

if(statement) {
   /* The statement(s) will execute if the statement is true */
}

When the Boolean statement is true, then the code inside in the “if statement” will be executed. When the Boolean statement is false, then the first set of a code and after at the end of an “if statement” or after the closing of a curly brace will be executed.

Below is the illustration of a flowchart for the if statement.

Let’s have a look at the example of if statement below.

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int x = 45;
 
   /* check the boolean condition using if statement */
	
   if( x < 60 ) {
      /* if condition is true then print the following */
      printf("X is less than 60\n" );
   }
   
   printf("The value of x is : %d\n", x);
 
   return 0;
}

Output:

X is less than 60
The value of x is : 45

In the code example above, the condition of if statement is true because the value of X is less than 60 which is 45. So the if statement is executed.

The If.. Else Statement in C with Example

The if statement will be followed by an optional else statement, through executions if the Boolean expression is false.

Syntax:

Here is the syntax of if.. else statement in C.

if(statement) {
   /* The statement(s) will execute if the statement is true */
} else {
   /*The statement(s) will execute if the statement is false */
}

In C language, the Boolean statement evaluates in true condition, then the if block condition will be executed, Besides, the else block will be executed.

In addition, we assume any non-zero and non-null values are true, and if it is zero or null, then it is assumed as false.

The illustration below is the flowchart diagram of if.. else statement in C

flowchart diagram of if.. else statement in C

Let’s have a look at the example below.

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int x = 50;
 
   /* check the boolean condition */
   if( x < 10 ) {
      /* if condition expression is true then print the following statement */
      printf("x is less than 10\n" );
   } else {
      /* if condition expression is false then print the following statement */
      printf("x is not less than 10\n" );
   }
   
   printf("value of x is : %d\n", x);
 
   return 0;
}

Nested If Statements in C with Example

We can use single if or else if statement inside of another if or else if statement(s).

Syntax:

Here is the syntax of nested if statement below.

if( statement1) {

   /*It will Executes if the statement 1 is true */
   if(statement2) {
      /* It will Executes when the statement 2 is true */
   

In C program, we can nest else if…else in the same way as you have nested if statements.

For example:

#include <stdio.h>
 
int main () {

   /* Here is the local variable definition */
   int x = 10;
   int y = 20;
 
   /* Here is to check the boolean condition */
   if( x == 10 ) {
   
      /* Here if condition is true then check the following */
      if( y == 20 ) {
         /* Here if condition is true then print the following */
         printf("Value of x is 10 and y is 20\n" );
      }
   }
   
   printf("The exact value of x is : %d\n", x );
   printf("The exact value of y is : %d\n", y );
 
   return 0;
}

Output:

Value of x is 10 and y is 20
The exact value of x is : 10
The exact value of y is : 20

Switch Statement in C with Example

In C program, the switch statement allows a variable to be checked for equality against a list of values. In every value is called a case. Furthermore, the variable being switched on is tested in every switch case.

The Syntax of switch statement in C:

witch(statement) {

   case constant-statement:
      expression(s);
      break; /* optional */
	
   case constant-statement:
      expression(s);
      break; /* optional */
  
   /* We can have any number of case statements */
   default : /* Optional */
   expression(s);
}

In addition, here are the following rules that must be applied to a switch statement.

  • The expression in using a switch statement must be an integral or enumerated type. On the other hand, it should be class type in which the class has only one conversion function to an integral or enumerated type.
  • Within a switch, we should have a number of case statements. In every case it is followed by the value to be compared to and with a colon at the end.
  • The constant-expression in a case it should be the same data type as the variable in the switch, and it should be a constant or a literal.
  • If the variable is being switched on it is equal to a case. Then, the statements of the following case will execute as far as a break statement is reached the scope.
  • If the break statement is achieved, then, the switch ends, For the flow of control it will jump to the next line in the following switch statement.
  • Not in each case needs to consist of a break. When no break appears, the flow of control will decline to the following cases until a break is achieved.
  • The switch statement has default case option, It should appear at the end of the switch. The default case will be used to perform a task when none of the cases is true. There is no break is required in the default case.

For example:

#include <stdio.h>
 
int main () {

   /* local variable definition */
   char mark = 'A';

   switch(mark) {
      case 'A' :
         printf("Excellent!\n" );
         break;
      case 'B' :
      case 'C' :
         printf("Well done\n" );
         break;
      case 'D' :
         printf("You passed\n" );
         break;
      case 'F' :
         printf("Better try again\n" );
         break;
      default :
         printf("Invalid mark\n" );
   }
   
   printf("Your mark is  %c\n", mark );
 
   return 0;
}

Output:

Excellent!
Your mark is A

Nested Switch Statements in C with Examples

We can use single switch statement inside another switch statement(s).

Syntax:

switch(statement1) {

   case 'X': 
      printf("This A is part of outer switch" );
		
      switch(statement2) {
         case 'X':
            printf("This A is part of inner switch" );
            break;
         case 'Y': /* case code */
      }
	  
      break;
   case 'Y': /* case code */
}

For example:

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int x = 10;
   int y = 20;
 
   switch(x) {
   
      case 10: 
         printf("This is part of outer switch\n", x );
      
         switch(y) {
            case 20:
               printf("This is part of inner switch\n", x );
         }
   }
   
   printf("Exact value of x is : %d\n", x );
   printf("Exact value of y is : %d\n", y );
 
   return 0;
}

Output:

This is the outer switch statement
This is the inner switch Statement
The exact value of x is : 10
The exact value of y is : 20

Conclusion

In this tutorial on C Decision Making, we have discussed and learned almost all the decision making in C with explanations of examples. The tutorials begins with the different types of decision making in C, which are the following such as if statement, if…else statement, nested if statements, switch statement, and nested switch statements.

Leave a Comment