If Else in 1 Video | C Programming | Lecture 2 | Complete C Course

Master If Else in C to implement decision making in C control structures. This comprehensive guide covers syntax, practical code examples, multi-condition ladders, and nesting techniques to help you write smart, dynamic programs using essential conditional statements in C.
authorImageVarun Saharawat23 Jun, 2026
If Else in 1 Video | C Programming | Lecture 2 | Complete C Course

Writing computer programs requires a way to guide the execution path based on different inputs. This is where decision-making in C plays an important role. Many beginners struggle to understand how code chooses between multiple paths, resulting in logical errors.

This comprehensive C programming tutorial simplifies the concept of If Else in C, helping you understand exactly how conditional statements in C manage code execution. You will master standard syntax rules, multi-condition handling, and error prevention strategies.

What is If Else in C?

The block known as 'If Else' in C is a control statement used to execute specific parts of code based on whether a given condition evaluates to true or false. In standard programming, operations run sequentially from top to bottom. Introducing conditional statements in C changes this linear flow, letting code adapt based on user values or calculation outputs.

When a program evaluates a condition, it produces a Boolean output: either non-zero (true) or zero (false). The system evaluates these Boolean results to determine which code segment to process next.

How to Use Standard If Else in C?

The standard block evaluates a single boolean expression. If the statement evaluates to a true value, the program runs the instructions wrapped inside the initial curly brackets. When the expression evaluates to false, the system skips that portion and runs the code inside the alternate section.

Syntax for standard usage

C

if (condition) {
    // Code runs if the condition evaluates to true
} else {
    // Code runs if the condition evaluates to false
}

Relational operators used in conditions

  • Less than (<): Checks if the left value is smaller than the right value.

  • Less than or equal to (<=): Verifies if the left value is smaller than or equal to the right value.

  • Greater than (>): Checks if the left value is larger than the right value.

  • Greater than or equal to (>=): Verifies if the left value is larger than or equal to the right value.

  • Equal to (==): Checks if both values match exactly.

  • Not equal to (!=): Verifies if two values are different.

Practical code example

The program below evaluates user input to determine whether an integer is even or odd using a basic check:

C

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    // True if the remainder is 0
    if (number % 2 == 0) {
        printf("%d is an even integer.\n", number);
    } else {
        printf("%d is an odd integer.\n", number);
    }
    return 0;
}

What is the Multi-Condition If Else in C ladder?

When your software needs to check more than two distinct options, a basic two-way split is insufficient. The multi-condition ladder allows your application to test several sequential expressions until one evaluates to true.

The system processes this chain from top to bottom. As soon as a true condition is met, the code inside that specific block executes, and the rest of the ladder is completely skipped.

Syntax for the ladder structure

C

if (condition_1) {
    // Runs if condition_1 is true
} else if (condition_2) {
    // Runs if condition_2 is true
} else if (condition_3) {
    // Runs if condition_3 is true
} else {
    // Runs if all previous conditions are false
}

Numerical relationship program example

The code below shows how to compare two input numbers using three evaluation steps:

C

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    // Checks if the two integers are equal
    if (number1 == number2) {
        printf("Result: %d = %d\n", number1, number2);
    }
    // Checks if number1 is greater than number2
    else if (number1 > number2) {
        printf("Result: %d > %d\n", number1, number2);
    }
    // Runs if both expressions above are false
    else {
        printf("Result: %d < %d\n", number1, number2);
    }
    return 0;
}

How to Use Nested If Else in C statements?

Nesting means placing an inner conditional structure inside the body of an outer condition block. This technique helps break down complex, multi-layered validation requirements into manageable steps.

Structure of nested conditions

C

if (outer_condition) {
    if (inner_condition) {
        // Runs if both outer and inner conditions match true
    } else {
        // Runs if outer is true but inner is false
    }
} else {
    // Runs if the outer condition evaluates to false
}

Nested comparison program example

This example implements the same two-number relationship logic from the previous section, using a nested setup instead of a ladder:

C

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
        if (number1 == number2) {
            printf("Result: %d = %d\n", number1, number2);
        } else {
            printf("Result: %d > %d\n", number1, number2);
        }
    } else {
        printf("Result: %d < %d\n", number1, number2);
    }
    return 0;
}

Best Practices for If Else in C

Using clean formatting and proper syntax ensures your code is safe, predictable, and readable. Pay attention to structural rules to avoid introducing subtle runtime bugs.

Always use explicit block curly braces

If the code body following a condition contains only one line, the compiler does not strictly require opening and closing curly braces {}. For instance, the two examples below perform identically:

C

// Method without explicit braces
if (balance > minimum)
    printf("Account active\n");

// Recommended method with clear braces
if (balance > minimum) {
    printf("Account active\n");
}

Omitting braces is risky. If you add a second statement to the body later, the compiler will treat that new line as independent code outside the condition, breaking your application's logic.

Managing boolean flags cleanly

You can pass boolean variables directly into the evaluation parentheses instead of writing an explicit relational comparison. Remember to include the <stdbool.h> header file when using standard boolean definitions.

C

#include <stdio.h>
#include <stdbool.h>

int main() {
    int x = 20;
    int y = 18;
    bool isGreater = x > y;
    if (isGreater) {
        printf("x is successfully verified as greater than y\n");
    }
    return 0;
}

Key Differences Between Decision Structures in If Else in C

Choosing the correct control tool clarifies your design goals. The table below compares the functional properties of different decision making in C formats:

Control Structure Format

Best Use Cases

Maximum Target Expressions

Single If Block

Performing isolated, single-path optional operations.

Validates exactly 1 expression path.

Standard If Else

Handling mutually exclusive two-way logical paths.

Validates exactly 2 distinct paths.

Else If Ladder

Evaluating variable ranges or complex conditions.

Validates unlimited expression paths sequentially.

Nested Structure

Performing multi-layered dependent validation checks.

Validates deep, hierarchical expression paths.

Common Errors to Avoid with If-Else in C

Learn about the common mistakes programmers make while using if-else statements in C.

  • Using the Assignment Operator (=) Instead of Equality (==): Writing if (x = 5) assigns the value 5 to the variable x. Since 5 is non-zero, the condition always evaluates to true. Use if (x == 5) to compare values.

  • Adding Errant Semicolons: Placing a semicolon directly after the condition line, such as if (x > y);, terminates the statement immediately. The block following it will then execute every time, regardless of the condition.

  • Incorrect Ladder Ordering: When writing an array range evaluation ladder, check the most restrictive boundaries first. Ordering conditions incorrectly can cause broader conditions to capture values early, preventing lower blocks from ever executing.

FAQs

How do conditional statements in C evaluate true or false values?

The system treats 0 as false. Any non-zero numerical value, whether positive or negative, evaluates to true during processing.

Can I write a nested expression structure without using an else block?

Yes. The secondary code blocks are optional. You can wrap a clean inner block inside an outer block without providing fallback paths.

Why does my code run the assignment instead of checking values?

This happens when you accidentally use the single equal sign assignment operator instead of the double equal sign equality comparison operator inside the evaluation parenthesis.

What happens if multiple expressions in an else if ladder evaluate to true?

Only the first true block in the sequence executes. The program processes the ladder from top to bottom and exits the structure as soon as it handles the first matching block.

Is this format the only method for decision making in C?

No, this C programming tutorial highlights basic blocks, but you can also manage choices using switch statements or ternary conditional operators.
Popup Close ImagePopup Open Image
Talk to a counsellorHave doubts? Our support team will be happy to assist you!
Popup Image
avatar

Get Free Counselling Today

and Clear up all your Doubts

Talk to Our Counsellor just by filling out the form.
Student Name
Phone Number
IN
+91
OTP
Email Id
Join 15 Million students on the app today!
Point IconLive & recorded classes available at ease
Point IconDashboard for progress tracking
Point IconLakhs of practice questions
Download ButtonDownload Button
Banner Image
Banner Image