Decision-Making Statements in C

Decision Making Statements in C

Let’s have a glance at the decision making statements in the C programming language: its types & syntax.

What is Decision Making Statements in C Programming?

Suppose, you are looking to find whether a given number is even or odd, then what will you do? You will divide that number by 2 and if the remainder will be zero, the number will be called as even number, otherwise an odd number. Likewise, in the programming language, decision-making statements execute a statement on the basis of conditions.

Example:

if(number%2==0
{
printf("number is even");
}
else
{
printf("number is odd");
}

Decision-making statements are also known as conditional statements. 

Types of Decision Making Statements in C

In c programming language there are following decision-making statements:

  1.  if statements
  2.  if-else statements
  3. nested if statements
  4. switch statement
  5. nested switch statements

Let’s have a look at all the decision making statements along with its syntax one by one.

if statement

It executes the statement within its body if the stated boolean condition is true. In if statement, a single statement can be added without inserting curly braces { }

Syntax of if statement

if( expression )
{
 statement 1;//statement to be executed when the condition is true
}
 statement 2;

Example of if statement in C programming language

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    if (number < 0) {
        printf("You entered %d.\n", number);
    }
    printf("The if statement is easy.");
    return 0;
}

if-else statement

It executes the statement(s) followed by if -statement if the boolean condition is true otherwise it will execute the statement(s) followed by the else statement.

Syntax of if-else statement

if( expression )
{
 statement 1;//statement to be executed when the condition is true
}
else
{
 statement 2;//statement to be executed when the condition is false
}

Example of the if-else statement in C programming language

#include <stdio.h>
int main() {
  int number;
  printf("Enter an integer: ");
  scanf("%d", &number);
   if  (number%2 == 0) {
       printf("%d is an even integer.",number);
   }
   else {
       printf("%d is an odd integer.",number);
   }
 return 0;
}

Nested if statements

It allows the use of a conditional statement(s) inside another conditional statement.

Syntax of nested if statement

 if( expression )
{
  if( expression1 )
   {
     statement 1;//statement to be executed when the condition is true 
   }
  else 
   {
     statement 2; //statement to be executed when the condition is false
   }
}
else
{
 statement 3; //statement to be executed when the condition is false
}

Example of nested if statement in C programming language

#include <stdio.h>
int main() {
   int number1, number2;
   printf("Enter two integers: ");
   scanf("%d %d", &number1, &number2);
   if (number1 >= number2) {
     if (number1 == number2) {
       printf("Result: %d = %d",number1,number2);
     }
     else {
       printf("Result: %d > %d", number1, number2);
     }
   }
   else {
       printf("Result: %d < %d",number1, number2);
   }
   return 0;
}

Switch statement

It allows you to test the variable for equality to the value list.

Syntax of the switch statement

switch(n) // n is any variable
{ 
case 1: 
statement(s);
break; 
case n: 
statement(s);
break; 
default: 
statement(s);
break; 
}

Example of the switch statement in C programming language

#include<stdio.h>
void main( )
{
    int a, b, c, choice;
    while(choice != 3)
    {
        printf("\n 1. Press 1 for addition");
        printf("\n 2. Press 2 for subtraction");
        printf("\n Enter your choice");
        scanf("%d", &choice);
        
        switch(choice)
        {
            case 1:
                printf("Enter 2 numbers");
                scanf("%d%d", &a, &b);
                c = a + b;
                printf("%d", c);
                break;
            case 2:
                printf("Enter 2 numbers");
                scanf("%d%d", &a, &b);
                c = a - b;
                printf("%d", c);
                break;
            default:
                printf("you have passed a wrong key");
                printf("\n press any key to continue");
        }
    } 
}

Nested switch statements

it has one switch statement inside another switch statement.

Syntax of the nested switch statement

switch(n) 
{
case 1:  
statement(s);
switch(ch2) 
{
case 1:
statement(s);
break;
case n:
statement(s);
}
break;
case 2:
statement(s);
}

Example of the nested switch statement in C programming language

#include<stdio.h>
int main()
{
  int a,b;
  printf("1.School of Computer Science\n");
  printf("2.School of Business\n");
  printf("3.School of Engineering\n");
  printf("make your selection\n");
  scanf("%d",&a);
  switch (a)
  {
    case 1: 
      break;
    case 2: 
      printf("Available Departments\n"
      printf("1.Department of commerce\n");
      printf("2.Department of purchasing\n");
      printf("Make your selection.\n");
      scanf("%d",&b);
 
      switch(b) 
      { 
        case 1:
        printf("You chose Department of commerce\n" );
        break;
        case 2: 
        printf("You chose Department of purchasing" );
        break;
      }   
      break;
  }
}