C Decision Making

What is Decision Making in C?

Decision Making in C is used to execute different blocks of code based on a condition.

It allows the program to decide which statement should be executed depending on whether the condition is true or false.

Decision-making statements make programs more interactive, efficient, and intelligent.

Examples of decision-making statements are if, if…else, nested if, else if ladder, switch, and conditional (?:) operator.

Why Decision Making is Important?

Decision Making is important because it:

  • Executes code based on conditions.
  • Makes programs interactive.
  • Helps solve real-world problems.
  • Improves program efficiency.
  • Reduces unnecessary code execution.

Types of Decision Making in C

C provides different decision-making statements for different situations.

The main types of decision-making statements are:

  • if Statement
  • if…else Statement
  • Nested if Statement
  • else if Ladder
  • switch Statement
  • Conditional (Ternary) Operator

if Statement

The if statement executes a block of code only when the given condition is true.

If the condition is false, the statements inside the if block are skipped. 

Syntax:

if(condition)
{
// statements
}

Example Program

Program

#include<stdio.h>

int main()
{
int age = 20;

if(age >= 18)
{
printf(“Eligible to Vote”);
}

return 0;
}

Output

Eligible to Vote

Explanation

  • age = 20 stores the age.
  • The condition age >= 18 is checked.
  • Since the condition is true, the message Eligible to Vote is displayed.
  • If the condition were false, nothing would be printed.

if...else Statement

The if…else statement is used when there are two possible outcomes.

If the condition is true, the if block is executed.

If the condition is false, the else block is executed.

Syntax:

if(condition)
{
// statements if condition is true
}
else
{
// statements if condition is false
}

Example Program

Program

#include<stdio.h>

int main()
{
int marks = 32;

if(marks >= 35)
{
printf(“Pass”);
}
else
{
printf(“Fail”);
}

return 0;
}

Output

Fail

Explanation

  • marks = 32 stores the student’s marks.
  • The condition marks >= 35 is checked.
  • Since the condition is false, the else block is executed.
  • Therefore, the output is Fail.

Nested if Statement

A Nested if statement means placing one if statement inside another if statement.

It is used when multiple conditions must be checked one after another.

Syntax:

if(condition1)
{
if(condition2)
{
// statements
}
}

Example Program

Program

#include<stdio.h>

int main()
{
int age = 22;
int citizen = 1;

if(age >= 18)
{
if(citizen == 1)
{
printf(“Eligible to Vote”);
}
}

return 0;
}

Output

Eligible to Vote

Explanation

  • The first condition checks whether the age is 18 or above.
  • The second condition checks whether the person is a citizen.
  • Both conditions are true, so the message Eligible to Vote is displayed.
  • If any condition were false, the output would not be displayed.

else if Ladder

The else if ladder is used when multiple conditions need to be checked.

The program checks each condition from top to bottom and executes the first condition that is true.

If none of the conditions are true, the else block is executed.

Syntax:

if(condition1)
{
// statements
}
else if(condition2)
{
// statements
}
else if(condition3)
{
// statements
}
else
{
// statements
}

Example Program

Program

#include<stdio.h>

int main()
{
int marks = 78;

if(marks >= 90)
printf(“Grade A”);
else if(marks >= 75)
printf(“Grade B”);
else if(marks >= 50)
printf(“Grade C”);
else
printf(“Fail”);

return 0;
}

Output

Grade B

Explanation

  • The program checks conditions one by one.
  • marks >= 90 is false.
  • marks >= 75 is true.
  • Therefore, Grade B is displayed.
  • The remaining conditions are skipped.

switch Statement

The switch statement is used to select one block of code from multiple options.

It compares the value of a variable with different case values. When a matching case is found, that block of code is executed.

The break statement is used to stop the execution after a matching case.

Syntax:

switch(expression)
{
case value1:
// statements
break;

case value2:
// statements
break;

default:
// statements
}

 

Example Program

Program

#include<stdio.h>

int main()
{
int day = 3;

switch(day)
{
case 1:
printf(“Monday”);
break;

case 2:
printf(“Tuesday”);
break;

case 3:
printf(“Wednesday”);
break;

default:
printf(“Invalid Day”);
}

return 0;
}

Output

Wednesday

Explanation

  • day = 3.
  • The program checks each case.
  • case 3 matches the value.
  • Therefore, Wednesday is displayed.
  • The break statement stops further execution.

Conditional (?:) Operator

The Conditional (Ternary) Operator is a short form of the if…else statement.

It evaluates a condition and returns one value if the condition is true and another value if the condition is false.

Syntax:

#include<stdio.h>

int main()
{
int age = 18;

(age >= 18) ? printf(“Eligible to Vote”) : printf(“Not Eligible”);

return 0;
}

 

Example Program

Program

#include<stdio.h>

int main()
{
int age = 18;

(age >= 18) ? printf(“Eligible to Vote”) : printf(“Not Eligible”);

return 0;
}

Output

Eligible to Vote

Explanation

  • age = 18.
  • The condition age >= 18 is checked.
  • Since the condition is true, Eligible to Vote is displayed.
  • If the condition were false, Not Eligible would be displayed.

Key Points

  • Decision Making controls the flow of a program using conditions.
  • The if statement executes code only when the condition is true.
  • if…else provides two execution paths.
  • Nested if is used when one condition depends on another.
  • else if ladder checks multiple conditions one by one.
  • switch is useful for selecting one option from multiple choices.
  • Conditional (?:) Operator is the shorthand form of if…else.
  • Decision-making statements make C programs more flexible and efficient.