OnlineCBT Logo OnlineCBT
C Tutorial
C · LESSON 7

Conditional Branching: if, else if, switch

By Ratan Updated: 15 Oct 2026

Implement decision logic using nested if-else statements and switch-case structures.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Choose one path through a program

A condition is false when its value compares equal to zero; otherwise it is true. Use if for decisions, else if for alternatives, and switch when one integer-valued expression selects among discrete cases. The order of the tests matters.

Worked example: grade classification

#include 
int main(void) {
    int score = 76;
    if (score < 0 || score > 100) {
        puts("Invalid score");
    } else if (score >= 80) {
        puts("Distinction");
    } else if (score >= 60) {
        puts("First division");
    } else if (score >= 40) {
        puts("Pass");
    } else {
        puts("Needs improvement");
    }
    return 0;
}

Expected output is First division. Tests run from top to bottom and stop at the first true branch. Testing score greater than or equal to 40 first would wrongly capture scores such as 76. Validate the domain before classifying it.

When switch is clearer

switch (choice) {
    case 1:
        puts("Add student");
        break;
    case 2:
        puts("List students");
        break;
    case 0:
        puts("Exit");
        break;
    default:
        puts("Unknown choice");
}

This fragment assumes choice is an initialized integer and stdio.h is included. Case labels must be integer constant expressions. A standard C switch does not directly match strings or ranges such as 60 through 79. Without break, execution falls through to subsequent labels. Use fall-through only intentionally and document it.

Braces prevent maintenance errors

An unbraced if controls only one statement. Indenting a second statement does not put it under the condition. Always using braces makes later edits safer. A stray semicolon immediately after if (condition) creates an empty controlled statement. Also remember that an else associates with the nearest unmatched if; explicit braces make the intended association visible.

Boundary testing

For the grade example, test -1, 0, 39, 40, 59, 60, 79, 80, 100 and 101. These cases expose gaps and overlaps more effectively than testing only 76. Write down the expected result before running each case.

Practice with solution outline

  1. Classify a number as negative, zero or positive. Use mutually exclusive comparisons against zero.
  2. Create a menu for area of a square or rectangle. Use switch for the menu and an if to reject non-positive dimensions.
  3. Why is if (x = 5) suspicious? It assigns 5 and then tests a nonzero value; equality requires ==.

हिन्दी संकेत: पहले invalid input रोकें, फिर boundaries के क्रम में conditions जांचें। हर branch का अपना test case रखें।

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat