C
Branching Statements
Lecture 9: Control Flow
Control Flow Fundamentals

Making Decisions in C

By default, C statements execute sequentially from top to bottom. Branching statements allow your code to choose different execution paths based on runtime conditions.

In C, what numerical value represents a true condition in an if statement?
branching_overview.cDecision Logic
#include <stdio.h> int main(void) { int score = 85; // Condition evaluated: (score >= 50) if (score >= 50) { printf("Result: PASSED!\n"); } else { printf("Result: FAILED!\n"); } return 0; }
One-Way Branching

The Simple if Statement

An if statement executes a block of code only if the specified condition evaluates to true (non-zero). If false, the block is completely skipped.

Syntax & Mechanics

if (condition) { // Code executes if condition is TRUE }

If there is only a single statement inside the if block, curly braces {} are optional, but using them is best practice to prevent bugs.

Enter Program
Condition?
TRUE → [ Execute Block ] FALSE → [ Skip ]

Interactive Lab: Temperature Alert

Test how a single if statement responds to input values.

if (temp > 35) { printf("HEAT WAVE WARNING!"); } printf("Have a nice day.");
Two-Way Branching

The if-else Statement

Use if-else when you have two mutually exclusive choices: execute Action A if the condition is true, or Action B if the condition is false.

Interactive Code Tracer: Voting Eligibility

Click evaluate to run the tracer.
voting_check.cExecution Trace
int age = 19; if (age >= 18) { printf("Eligible to vote!"); } else { printf("Not eligible yet."); }
Hierarchical Logic

Nested if-else & The Dangling Else

An if or else block can contain another complete if-else structure. Beware of the Dangling Else ambiguity!

⚠️ The Dangling Else Trap

In C, an else clause is always paired with the nearest preceding if that does not already have an else, regardless of indentation!

// Misleading Indentation! if (a > 0) if (b > 0) printf("Both positive"); else printf("Is this for (a>0)?"); // NO! It pairs with (b>0)!
Fix: Always wrap blocks in curly braces {} to enforce clear scope!

Interactive Admission Test

Adjust scores and click evaluate.
Multi-Way Branching

The if-else Ladder

When testing a single variable or expression against multiple sequential ranges, an if-else if-else ladder evaluates conditions top-to-bottom until a true match is found.

Interactive Grade Calculator

Condition: marks >= 90 Grade A+
Condition: marks >= 80 Grade A
Condition: marks >= 70 Grade B
Condition: marks >= 60 Grade C
Default else Grade F
grade_ladder.cSequential Evaluation
if (marks >= 90) { grade = 'A+'; } else if (marks >= 80) { grade = 'A'; <-- Stops here if 82 } else if (marks >= 70) { grade = 'B'; } else if (marks >= 60) { grade = 'C'; } else { grade = 'F'; }
Best Practices

Common Branching Pitfalls in C

Watch out for these frequent logical bugs that compile cleanly but produce wrong behavior!

🚨

Assignment vs Equality

Accidentally writing if (x = 5) instead of if (x == 5) assigns 5 to x, which evaluates as TRUE!

if (x = 5) { // BUG! Always true printf("Oops!"); }
🛑

Extra Semicolon Bug

Placing a semicolon right after if(...) ; creates an empty statement, causing the following block to ALWAYS run!

if (score > 90); { // BUG! ';' ends if printf("Passed!"); // Runs always! }
💡

Best Practice: Braces

Always use curly braces {} even for single-line blocks to improve code readability and maintainability.

if (isReady) { execute(); }
Knowledge Check

Test Your Branching Mastery

Question 1 of 5
Loading question...