If Statements (All Types) in C – BKNMU Junagadh | BCA Sem 1 Unit 2 Notes

BCA Sem 1  |  Unit 2  |  Computer Concepts & C Programming

If Statements (All Types) in C — BKNMU Junagadh

Simple If, If-Else, Nested If, and Else-If Ladder Decision Control Structures

BKNMU Junagadh NEP 2020 Unit 2 Decision Control If-Else Else-If Ladder
In simple words: Welcome to Unit 2! Decision Control Statements allow a C program to alter its execution path based on specified logical conditions. The if family of statements evaluates a condition as True (non-zero) or False (zero) to decide whether to execute or skip a block of code.
📖 Four Major Types of If Statements in C

C provides four primary structural variations of the if statement to handle different levels of decision-making complexity:

if (condition)

1. Simple If

Executes a single block of code only when the given condition evaluates to True. Has no alternative branch.

if ... else

2. If-Else

Provides two execution paths: one block executes if True, and the alternative block executes if False.

if ... else if

3. Else-If Ladder

Evaluates multiple conditions sequentially in a top-to-bottom chain until a True condition is encountered.

if inside if

4. Nested If

Places an if or if-else statement inside another if block for multi-level condition verification.

1️⃣ Master Table of If Statement Variations

Here is a detailed comparative summary of all four types of if statements used in C programming:

Statement TypeSyntax & Structural FormatWhen to Use in Programs
Simple Ifif (condition) {
  // code block
}
When an action is required ONLY if a condition is satisfied, and nothing needs to happen if it fails.
If-Elseif (condition) {
  // True block
} else {
  // False block
}
When you have exactly two mutually exclusive outcomes (e.g., checking if a number is even or odd).
Else-If Ladderif (cond1) { ... }
else if (cond2) { ... }
else { ... }
When choosing among three or more distinct options or checking value ranges (e.g., student grading schemes).
Nested Ifif (cond1) {
  if (cond2) {
    // inner block
  }
}
When a second condition must be evaluated only AFTER a primary condition has already passed True.
ℹ️ Non-Zero Rule in C: In C, any non-zero numeric expression inside an if statement (like if(5) or if(-10)) evaluates as True! Only an explicit zero (0) evaluates as False.
💻 Code Examples & Practical Applications

1. Simple If & If-Else Example:

#include <stdio.h>

int main() {
    int age = 18;

    // Simple If
    if (age >= 18) {
        printf("You are eligible to vote.\n");
    }

    // If-Else Statement
    int num = 7;
    if (num % 2 == 0) {
        printf("%d is Even\n", num);
    } else {
        printf("%d is Odd\n", num);
    }

    return 0;
}

2. Else-If Ladder Example (Grading Scheme):

#include <stdio.h>

int main() {
    int marks = 85;

    if (marks >= 90) {
        printf("Grade: Distinction\n");
    } else if (marks >= 75) {
        printf("Grade: First Class\n");
    } else if (marks >= 50) {
        printf("Grade: Second Class\n");
    } else {
        printf("Grade: Fail\n");
    }

    return 0;
}

3. Nested If Example (Find Largest of Three Numbers):

#include <stdio.h>

int main() {
    int a = 12, b = 25, c = 9;

    if (a >= b) {
        if (a >= c)
            printf("%d is the largest\n", a);
        else
            printf("%d is the largest\n", c);
    } else {
        if (b >= c)
            printf("%d is the largest\n", b);
        else
            printf("%d is the largest\n", c);
    }

    return 0;
}
🔄 Rules for Writing If Statements
1

Enclose Conditions in Parentheses

The logical condition inside if(...) MUST always be placed within parentheses.

2

Avoid Semicolons After If

Never put a semicolon directly after the condition line (e.g., writing if(a > b); ends the statement prematurely!).

3

Use Braces `{}` for Multi-line Blocks

If a block contains more than one statement, you MUST enclose the block within curly braces {}.

💡 Exam Tip on Dangling Else: An else clause always pairs with the **nearest un-paired if** in the same scope level unless forced otherwise using curly braces {}.
⚠️ Common Syntax Error (Assignment vs Equality): Writing if (x = 5) instead of if (x == 5) assigns 5 to x, which evaluates to non-zero (True) every time! Always use double equal signs == for comparison.
📝 Quick Revision — Key Exam Points
  • Control Type: Conditional branching decision statement.
  • 4 Variations: Simple If, If-Else, Else-If Ladder, Nested If.
  • True / False Values: Non-zero = True; Zero (0) = False.
  • Relational / Logical Operators: Used within conditions (==, !=, >, <, &&, ||).
  • Dangling Else: Matched to the nearest un-paired if.

Post a Comment

Thanks for comment.

Previous Post Next Post