C# & .NET Framework Control Flow — Decisions!
💡
Exercise 11

If/Else — The Architect's Decision Tree 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

🏛️ Every Great Building Starts with a Decision

Picture this: You're the Chief Architect of a booming digital city. Every day, building proposals land on your desk. Some are tiny cafés, others are towering skyscrapers. Your job? Evaluate each proposal and stamp it: APPROVED or REJECTED.

In C#, the if, else if, and else statements are your decision stamps. They let your program choose different paths based on conditions — just like an architect choosing different blueprints based on the terrain.

🔍 The Basic If Statement

The simplest decision: "Is this condition true? If so, do something."

int floors = 25; if (floors > 10) { Console.WriteLine("This is a tall building!"); }

🔀 Adding Else — The Fallback Plan

What if the condition is false? The else block catches everything that didn't match:

int floors = 3; if (floors > 10) { Console.WriteLine("Tall building — needs structural review"); } else { Console.WriteLine("Standard building — auto-approved"); }

🪜 The Full Decision Chain: else if

Real architectural reviews aren't just "tall or not." There are grades, tiers, and categories. That's where else if comes in — it lets you check multiple conditions in sequence:

int floors = 25; if (floors >= 50) { Console.WriteLine("Category: Megatower"); } else if (floors >= 20) { Console.WriteLine("Category: Skyscraper"); } else if (floors >= 5) { Console.WriteLine("Category: Mid-Rise"); } else { Console.WriteLine("Category: Low-Rise"); }

⚖️ Comparison Operators — Your Evaluation Tools

Every if statement needs a condition. Here are the comparison operators the architect uses to evaluate proposals:

  • == — Equal to: floors == 10 → Are there exactly 10 floors?
  • != — Not equal to: material != "Wood" → Is it NOT made of wood?
  • < — Less than: budget < 1000000 → Is the budget under a million?
  • > — Greater than: floors > 50 → More than 50 floors?
  • <= — Less than or equal: height <= 200 → At most 200 meters?
  • >= — Greater than or equal: floors >= 20 → At least 20 floors?

🧩 Nesting Decisions

Sometimes one decision leads to another — like checking both height AND material strength. You can nest if statements inside each other:

int floors = 30; string material = "Steel"; if (floors > 20) { if (material == "Steel") { Console.WriteLine("Approved: Steel skyscraper"); } else { Console.WriteLine("Rejected: Tall buildings require steel"); } }

⚠️ Common Pitfalls

  • Using = (assignment) instead of == (comparison) — the compiler will catch this for non-boolean types, but watch out!
  • Forgetting curly braces {} — without them, only the next single line belongs to the if block. Always use braces!
  • Order matters in else if chains — put the most specific conditions first. If you check floors > 5 before floors > 50, the 50-floor building matches the first condition and never reaches the second.

🏗️ Your Mission

The city council needs a building classification system. Given a building with a specific number of floors, your program must output three things: its approval status, the floor count, and the building category. Time to build that decision tree, Architect!

📋 Instructions
**The Architect's Building Classifier** You have a variable `floors` set to `25`. Write an if/else if/else chain that does the following: 1. **Print the building status**: If floors are greater than 0, print `"Building Status: APPROVED"`. Otherwise print `"Building Status: REJECTED"`. 2. **Print the floor count**: Print `"Floors: 25"` (use the `floors` variable). 3. **Classify the building category** using this logic: - 50 or more floors → `"Category: Megatower"` - 20 to 49 floors → `"Category: Skyscraper"` - 5 to 19 floors → `"Category: Mid-Rise"` - Less than 5 floors → `"Category: Low-Rise"` Make sure the output matches **exactly** (including capitalization).
Start with the approval check: if (floors > 0). Then print the floor count using Console.WriteLine("Floors: " + floors). Finally, write an else-if chain starting from the HIGHEST threshold (>= 50) down to the lowest. Remember: order matters — check the biggest number first!
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.