if statements: simple if, if-else, if-else if-else, Nested if

if runs code only when a condition is true, if-else picks between two paths, the else-if ladder picks one of many (perfect for grades), and nested if asks a question inside a question.

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The program's first decision

Until now the marks program treats 92 and 12 identically: compute, print, done. No judgement.

But a real marksheet SAYS something: pass or fail, and which grade.

Today the program forks its road: if this, do that, else do the other. Decision-making is where programs stop being calculators and start being software, and in exams, Unit 2 usually opens with exactly this family.

Theory

The toll booth chain

Picture toll booths in a row, each with a question. Simple if: one booth, satisfy it and a gate opens, fail and you simply drive on. if-else: a fork, one question, two roads, you MUST take one. else-if ladder: booths in sequence, the FIRST one you satisfy routes you off, later booths never see you. Nested if: a booth inside the toll plaza's VIP lane, a question you face only after passing the first.

Theory

The four forms, formally

  • Simple if: if (marks >= 35) { printf("Pass"); } runs the block only when the condition is true (nonzero).
  • if-else: adds the other road: else { printf("Fail"); }
  • if-else if-else ladder: one of many roads, tested top to bottom, first true wins, the rest are skipped.
  • Nested if: an if inside an if's block: check pass first, and only inside that, check for distinction.

Braces { } mark each road's block. Use them even for one statement; future-you will be grateful.

Practical

The grade ladder

#include <stdio.h>

int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);

    if (marks >= 70) {
        printf("Grade A\n");
    } else if (marks >= 60) {
        printf("Grade B\n");
    } else if (marks >= 35) {
        printf("Grade C\n");
    } else {
        printf("Fail\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Walk the ladder

Trace marks = 65 through the code above, booth by booth. Which conditions are tested, which one wins, and what prints?

Show the answer

65 >= 70? No, drive on. 65 >= 60? Yes: print Grade B, and the ladder EXITS: 65 >= 35 is never even tested. First true wins, the rest are skipped.

That is also why the ladder checks the HIGHEST grade first: reverse the order (35 first) and every passing student becomes Grade C, a logic bug with zero compiler complaints.

Quiz

What does this print for marks = 20? if (marks >= 35); { printf("Pass\n"); }

  1. Pass, the semicolon ended the if, so the block always runs
  2. Nothing, since 20 fails the condition
  3. A compile error at the semicolon
  4. Fail
Show the answer

Pass, the semicolon ended the if, so the block always runs

That semicolon right after if(...) is a complete, empty statement: "if condition, do nothing". The { printf } block that follows is INDEPENDENT code that runs unconditionally, so even 20 prints Pass. It compiles cleanly, which is what makes it the most famous C trap after = vs ==. Scan every if for a stray semicolon.

Quiz

In the grade ladder, marks = 90 satisfies BOTH marks >= 70 and marks >= 60. What prints?

  1. Grade A only: the ladder stops at the first true condition
  2. Grade A and Grade B, both conditions are true
  3. Grade B, the later match overrides
  4. A compile error: overlapping conditions
Show the answer

Grade A only: the ladder stops at the first true condition

An else-if ladder is one decision, not a series of independent checks: top to bottom, FIRST true condition claims the execution and the remaining rungs are skipped entirely. Both-print answers treat the ladder as separate ifs, which is exactly the misunderstanding this question exists to catch.

Watch out

The three if killers

Semicolon after if(...): empty statement, block runs always (above). = instead of == in the condition: assigns and reads as true, the Unit 1 trap in its natural habitat. Wrong ladder order: check 35 before 70 and toppers get Grade C. Bonus subtlety for nested ifs: an else pairs with the NEAREST unmatched if, so use braces to say what you mean.

Theory

Selection unlocked

Remember structured programming's three shapes? You now own the second: selection. The marks program can judge. But an if-else ladder for a five-option MENU (view marks, percentage, grade, exit...) gets clumsy fast, and C carries a purpose-built tool for exact-match menus: switch-case, next lesson, break statements and all.

Summary

Key takeaways

  • Simple if: run a block when the condition is true; if-else: two exclusive roads.
  • else-if ladder: many roads, tested top to bottom, FIRST true wins, rest skipped.
  • Ladder order matters: check the highest grade first.
  • Nested if: a question inside a question (pass, then distinction).
  • Killer traps: semicolon after if(...), = instead of ==, wrong ladder order.
  • Memory hook: toll booths, first satisfied booth routes you off.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

if statements: simple if, if-else, if-else if-else, Nested if · Computer Programming and Programming Methodology (CPPM) · Gri-Learn