Tuesday, 28 July 2026

Decision Making in C Programming .

🔀 Decision Making in C Programming

if, if-else and else-if Statements Explained

Teach Your Program How to Make Decisions 🚀


🚀 Introduction

In the previous lesson, we learned how programs communicate with users using printf() and scanf().

But real-world programs need to make decisions. For example:

  • If a student gets 40 marks → Pass
  • If password is correct → Login successful
  • If balance is sufficient → Allow payment

"Decision-making statements allow a program to execute different actions based on conditions."


1️⃣ if Statement

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

Syntax:


if(condition)
{
    // statements
}

Example:


#include <stdio.h>

int main()
{

int age = 18;

if(age >= 18)
{
    printf("You can vote");
}

return 0;

}


2️⃣ if-else Statement

The if-else statement is used when there are two possible choices.

Example:


if(marks >= 40)
{
    printf("Pass");
}

else
{
    printf("Fail");
}

If the condition is true, the first block runs. Otherwise, the else block runs.


3️⃣ else-if Ladder

When we have multiple conditions, we use else-if.


if(marks >= 90)
{
 printf("Grade A");
}

else if(marks >= 75)
{
 printf("Grade B");
}

else if(marks >= 40)
{
 printf("Grade C");
}

else
{
 printf("Fail");
}


📊 Decision Making Flowchart

⭕ Start
⬇️
▭ Enter Value
⬇️
◇ Condition Check?
⬇️
✅ True → Execute Code
❌ False → Alternative Code
⬇️
⭕ End

🌍 Real Life Examples

Situation Decision
ATM Withdrawal If balance is available → Give money
Online Login If password matches → Allow access
Exam Result If marks ≥ 40 → Pass

💻 Complete Example: Check Number Positive or Negative



#include <stdio.h>

int main()
{

int number;

printf("Enter a number: ");

scanf("%d",&number);


if(number >= 0)
{
    printf("Positive Number");
}

else
{
    printf("Negative Number");
}


return 0;

}



🖥️ Output

Enter a number: 25
Positive Number

🔗 Nested if Statement

An if statement inside another if statement is called nested if.


if(age >= 18)
{

   if(hasID == 1)
   {
      printf("Allowed");
   }

}


⭐ Why Decision Making is Important?

  • Creates intelligent programs
  • Controls program flow
  • Solves real-world problems
  • Builds games and applications
  • Used in Artificial Intelligence logic

🎯 Conclusion

Decision-making statements are the foundation of logical programming. They help computers choose the correct action based on conditions.

Next: Loops in C Programming 🔁🚀

No comments:

Post a Comment