C Conditionals

if Statement

Executes a block of code only if a given condition is true.

The if statement in C is used to test a condition. If the condition evaluates to true (non-zero), the code inside the block runs. Otherwise, it is skipped.

Basic Structure

if (condition) {
          // code to execute if condition is true
      }
Loading...
Output:

else Statement

If the condition in if is false, else will run.

Loading...
Output:

else if Chain

Used to evaluate multiple conditions in order.

Loading...
Output:

Nested Conditionals

You can nest one if or else block inside another.

Loading...
Output:

Truthy Values

C uses integers for condition evaluation:

  • 0 → false
  • non-zero → true
Loading...
Output:

Common Mistakes

  • ❌ Using = instead of == in comparisons
  • ❌ Forgetting braces when multiple statements are grouped
Loading...
Output:

Fix: Use if (x == 10)

switch Statement

The switch statement compares a variable against several constant values. Each case should end in break to prevent fall-through.

Loading...
Output:

if vs switch

When deciding between if and switch:

if / else ifswitch
Works with ranges and complex conditionsOnly works with constant discrete values (e.g. int, char)
More flexibleMore efficient for multiple exact matches
Supports expressions like x > 5No support for expressions, just equality

Practice Question

What will this print?

A. A
B. B
C. C
Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic