π§ Decision Making in C Programming
A Complete Beginner Guide with Examples and Output
π Part of the C Programming Beginner Series by Learning Growth Hub
✨ Introduction
In real life, we make decisions every day.
If it is raining → we take an umbrella.
If marks are above 40 → we pass the exam.
If option 1 is selected → we perform addition.
Similarly, in C programming, we use decision-making statements to control the flow of execution.
Decision making allows a program to execute specific blocks of code only when a given condition is true.
In this beginner-friendly guide, you will learn:
-
What is decision making in C
-
if statement
-
if-else statement
-
else-if ladder
-
Nested if
-
switch statement
-
Programs with line-by-line comments
-
Outputs for each program
-
Practice questions
Let’s start π
1️⃣ What is Decision Making in C?
π Description
Decision making allows a program to:
✔ Check a condition
✔ Decide whether it is true or false
✔ Execute specific code based on that result
It helps programs behave intelligently based on input or logic.
Conditions usually use:
-
Relational operators (
>,<,==,!=) -
Logical operators (
&&,||,!)
If condition is TRUE → block executes
If condition is FALSE → block is skipped
2️⃣ The if Statement
π Description
The if statement is the simplest decision-making statement in C.
It executes a block of code only when the given condition is true.
If the condition is false, the block is skipped.
It is commonly used to validate input or check simple conditions.
π Syntax
π§ͺ Example 1: Check if Number is Positive
π€ Output:
3️⃣ The if-else Statement
π Description
The if-else statement is used when we want two possible outcomes.
If the condition is true → one block executes.
If the condition is false → another block executes.
It is useful when checking things like even/odd, pass/fail, eligible/not eligible, etc.
π§ͺ Example 2: Check Even or Odd
π€ Output:
4️⃣ The else-if Ladder
π Description
The else-if ladder is used when we need to check multiple conditions.
C checks conditions from top to bottom.
As soon as one condition becomes true, its block executes and the remaining conditions are skipped.
It is commonly used in grading systems and menu-based programs.
π§ͺ Example 3: Grade Calculator
π€ Output:
5️⃣ Nested if Statement
π Description
A nested if means placing one if statement inside another if.
The second condition is checked only if the first condition is true.
This helps in creating more detailed decision logic.
π§ͺ Example 4: Check Positive and Even
π€ Output:
6️⃣ The switch Statement
π Description
The switch statement is used when we compare a single variable against multiple fixed values.
Instead of writing many if-else statements, switch makes the code cleaner and easier to read.
Each case represents a possible value.
π§ͺ Example 5: Simple Calculator Menu
π€ Output:
π Difference Between if-else and switch
| if-else | switch |
|---|---|
| Used for complex conditions | Used for fixed values |
| Supports logical operators (&&, ||, !) | Does not support complex logical conditions |
| More flexible | Cleaner for menu-based programs |
| Can compare ranges (e.g., marks >= 50) | Works only with specific constant values |
π§ͺ Practice Questions (Try Yourself)
-
Write a program to find the largest of 3 numbers.
-
Write a program to check whether a year is a leap year.
-
Create a menu-driven calculator using switch.
-
Write a program to check whether a character is a vowel or consonant.
-
Write a program to check voting eligibility (age ≥ 18).
π‘ Try solving before checking answers online. Practice builds confidence.
π― Why Decision Making is Important
Without decision-making statements:
-
Programs cannot react
-
Programs cannot validate input
-
Programs cannot create real-world logic
Every real-world software uses decision making.
π Quick Revision Summary
Before we conclude, here is a quick summary table to help you revise all decision-making statements at a glance.
This summary table helps you quickly revise when to use each decision-making statement in C programming. You can screenshot this table for quick exam revision.
✅ Conclusion
In this blog, you learned:
✔ What decision making is in C
✔ if statement
✔ if-else statement
✔ else-if ladder
✔ Nested if
✔ switch statement
✔ Programs with output
✔ Practice problems
If you practice these programs today, you will feel confident tomorrow.
π± Reflection
Decision making is what gives intelligence to a program.
When you use if, else, or switch, you are not just writing code —
you are teaching your program how to think and respond.
Mastering conditions is the first real step toward becoming a confident programmer.
π What’s Next?
π In the next blog, we will learn Loops in C (for, while, do-while) — which allow us to repeat tasks efficiently.
Stay tuned to Learning Growth Hub π±
✨ Written by Krishna Popat
π± Founder, Learning Growth Hub
Comments
Post a Comment