Understanding Nested IF-ELSE Statements in C Programming

Hey there, future coding star!
Today, we’re going to learn about Nested IF-ELSE statements. This is like taking the basic IF-ELSE decision-making and stacking them inside each other—like building blocks to make your program smarter!


What is a Nested IF-ELSE Statement?

A Nested IF-ELSE statement is when you have one “IF” or “ELSE” statement inside another one. Imagine you’re making a decision, and based on the first decision, you need to make another decision.

Syntax:


Real-Life Example of Nested IF-ELSE

Let’s say you’re deciding what to wear based on the weather and the temperature:

  • IF it’s sunny, then check:
  • IF it’s hot, wear a T-shirt.
  • Else (if it’s not hot), wear a light jacket.
  • Else (if it’s not sunny), wear a raincoat.

Here’s how this would look in C programming:


Flowchart of Nested IF-ELSE Statement

Let’s look at how the decision-making works using a flowchart:

  1. Start
  2. Check the first condition (weather == sunny).
  3. If true, go to the next condition (temperature > 30).
  4. Based on the second condition, print a message (wear a T-shirt or jacket).
  5. If the first condition is false (not sunny), wear a raincoat.

Programming Example

Let’s write a program that checks if a student has passed based on their score and grade:

  • IF the score is greater than 50, check:
  • IF the grade is ‘A’, print “Excellent!”
  • Else, print “Good job!”
  • Else, print “You need to work harder.”

Explanation:

  • First, we check if the student’s score is greater than 50.
  • If true, we check the grade.
  • If the grade is ‘A’, it prints “Excellent!”
  • If the grade is not ‘A’, it prints “Good job!”
  • If the score is not greater than 50, it prints “You need to work harder.”

Output 1:

For score = 65 and grade = 'A', the output will be:

Excellent!

Output 2:

For score = 65 and grade = 'B', the output will be:

Good job!

Output 3:

For score = 45, the output will be:

You need to work harder.

Fun Example: Picking a Movie

Let’s make a fun example where the computer helps you pick a movie to watch:

  • IF it’s the weekend, check:
  • IF you have finished your homework, print “Watch a movie!”
  • Else, print “Finish your homework first!”
  • Else, print “It’s a weekday. Focus on schoolwork.”

Output 1:

If it’s the weekend (weekend = 1) and you’ve finished your homework (homeworkDone = 1), the output will be:

Watch a movie!

Output 2:

If it’s the weekend but you haven’t finished your homework (homeworkDone = 0), the output will be:

Finish your homework first!

Output 3:

If it’s a weekday (weekend = 0), the output will be:

It's a weekday. Focus on schoolwork.

Summary

  • Nested IF-ELSE statements let you make more detailed decisions.
  • First, you check one condition, and based on that, you can check another condition inside it.
  • It’s like asking, “If the weather is sunny, then is it also hot?” Each answer leads to a different outcome.

Using Nested IF-ELSE makes your programs smarter and able to handle more complex situations, just like you make decisions in real life!

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?