Decision Making Statements in C

Decision Making Statements

In this page, you will find detailed explanation about the various decision making statements and control flow statements in C.

decision making statements in c example

What is Decision Making Statements in C?

In C programming, decision making statements are used to execute different blocks of code based on the results of a condition or expression. The main decision making statements in C are

  • the if statement
  • the switch statement.

If Statement

The if statement allows you to execute a block of code if a certain condition is true. The general syntax for an if statement in C is:

if (condition) {
// code to execute if condition is true
}

You can also include an else clause in an if statement to execute a different block of code if the condition is false:

if (condition) {
   // code to execute if condition is true
} else {
   // code to execute if condition is false
}

You can also include additional else if clauses to test multiple conditions:

if (condition1) {
   // code to execute if condition1 is true
} else if (condition2) {
   // code to execute if condition1 is false and condition2 is true
} else {
   // code to execute if condition1 and condition2 are false
}

Switch Statement

The switch statement is another decision making statement in C that allows you to execute different blocks of code based on the value of an expression. The syntax for a switch statement is:

switch (expression) {
   case value1:
      // code to execute if expression is equal to value1
      break;
   case value2:
      // code to execute if expression is equal to value2
      break;
   ...
   default:
      // code to execute if expression does not match any of the cases
      break;
}

Sample programs demonstrating Decision Making Statements in C

Program to Check if x is greater than 5

Run
#include <stdio.h>

int main() {
   int x = 10;

   // Check if x is greater than 5
   if (x > 5) {
      printf("x is greater than 5\n");
   }

   return 0;
}

Output

x is greater than 5

Program to Check if x is greater than 5

Run

#include <stdio.h>

int main() {
   int x = 2;

   // Switch on the value of x
   switch (x) {
      case 1:
         printf("x is 1\n");
         break;
      case 2:
         printf("x is 2\n");
         break;
      case 3:
         printf("x is 3\n");
         break;
      default:
         printf("x is not 1, 2, or 3\n");
         break;
   }

   return 0;
}

Output

x is 2

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription