Home Tutorials C Tutorial If Statement
If Statement

If Statement


The if Statement

What is an if Statement?

The if statement is the most fundamental decision-making structure in C. It is used to decide whether a certain statement or block of code will be executed based on a Boolean condition.

  • If the condition evaluates to True (non-zero), the code inside the braces runs.
  • If the condition evaluates to False (zero), the code inside the braces is skipped entirely.

Syntax

if (condition) {
    // Statements to execute if condition is true
}

Practical Examples

Example 1: Basic Comparison

int temperature = 35;

if (temperature > 30) {
    printf("It is a hot day.\n");
}

Example 2: User Authentication Logic

int entered_pin = 1234;
int correct_pin = 1234;

if (entered_pin == correct_pin) {
    printf("Access Granted. Welcome!\n");
}
Pro-Tip: In C, you don't actually need the curly braces { } if you only have one line of code inside your if statement. However, using them is considered a "best practice" because it makes your code much easier to read and maintain!

🏋️ Test Yourself With Exercises

Take our quiz on If Statement to test your knowledge.

Browse Quizzes »