CHAPTER 07
Beginner
Conditional Statements
Updated: May 17, 2026
5 min read
# CHAPTER 7
Conditional Statements
1. Introduction
Programs need to make decisions. Should a user be granted access? Is the player's health zero? Conditional statements allow a C program to choose different paths of execution based on specific conditions, creating intelligent software.2. Learning Objectives
By the end of this chapter, you will be able to:-
Use the
ifstatement to run code conditionally.
-
Use
if-elsefor two-way branching.
-
Chain conditions using
else ifladders.
-
Use
switch-casefor multi-way branching.
- Write concise conditions using the Ternary Operator.
3. The if Statement
Executes a block of code ONLY if the condition evaluates to true (non-zero).
c
4. The if-else Statement
Provides an alternative block of code to run if the condition is false (zero).
c
5. The else if Ladder
Used to check multiple conditions in sequence. As soon as one condition is true, its block executes, and the rest are skipped.
c
6. Nested if Statements
Placing an if statement inside another if statement.
c
7. The switch-case Statement
The switch statement is a cleaner alternative to a long else if ladder when comparing a single variable against exact values.
c
Important: Without the break keyword, execution "falls through" and runs all subsequent cases!
8. The Ternary Operator (?:)
A shorthand for a simple if-else statement.
Syntax: condition ? valueiftrue : valueiffalse;
c
9. Mini Project: Grade Calculator
c
10. Memory-Level Explanation
At the assembly level, anif statement compiles into a "Compare" (CMP) instruction followed by a "Jump" (JMP, JE, JNE) instruction. If the comparison fails, the CPU alters the Instruction Pointer (IP) register to skip over the code block entirely.
11. Common Mistakes
-
Using
=instead of==:
if (x = 5) assigns 5 to x, which evaluates to true (non-zero). This is a massive logic bug. It must be if (x == 5).
-
Missing
breakin switch: This causes fall-through, executing unintended code.
-
Floating point in switch:
switchonly works with integers and characters. You cannot switch on a float.
12. Exercises
- 1. Write a program to check if a given year is a leap year.
-
2.
Build a simple calculator using
switch-casethat asks the user for an operator (+,-,*,/) and two numbers.
- 3. Use the ternary operator to find the maximum of two numbers.
13. MCQ Quiz with Answers
Question 1
What happens if an if condition evaluates to 0?
Question 2
Which keyword handles the "catch-all" scenario in a switch statement?
Question 3
What is the output of if (5 = 5) { printf("Yes"); }?
Question 5
What prevents "fall-through" in a switch case?
Question 6
What does (x > y) ? x : y; do?
else mandatory after an if?
a) Yes b) No
Answer: b) No
Question 8
Which operator is used to compare two values for equality?
Question 9
Can if statements be nested inside other if statements indefinitely?
Question 10
In C, what value represents 'true'?
14. Interview Questions
-
Q: Explain the difference between
if-else ifladders andswitchstatements. Which is faster?
- Q: What is the dangling else problem?
-
Q: Why is it considered dangerous to use
if (a = b)?
15. Summary
Conditional statements give your program decision-making power.if, else if, and else provide flexible logic evaluation. switch-case offers a cleaner syntax for matching exact integers or characters. The ternary operator is a concise shortcut for simple binary decisions.
16. Next Chapter Recommendation
In Chapter 8: Loops in C, we will learn how to repeat actions automatically usingfor, while, and do-while loops.