C Booleans & If...Else

Make decisions in C.

✍️ 2 exercises in this lesson

C uses integers for truth values: 0 is false, anything else is true. Include <stdbool.h> to use bool, true and false.

  • if (cond) { } else if (cond2) { } else { }
  • Ternary: max = (a > b) ? a : b;

Key Takeaways: C Booleans & If...Else

Mastering C Booleans & If...Else helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C Booleans & If...Else Code Example

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

int main() {
    int temp = 31;
    bool isSunny = true;

    if (temp > 35) {
        printf("Very hot\n");
    } else if (temp > 25) {
        printf("Warm\n");
    } else {
        printf("Cool\n");
    }

    if (isSunny) printf("Take sunglasses\n");

    int a = 8, b = 12;
    printf("Max: %d\n", a > b ? a : b);
    return 0;
}
Output
Warm
Take sunglasses
Max: 12
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C Booleans & If...Else Practice Exercises

Check what you learned in “C Booleans & If...Else”.

/ 2 solved
Fill in the blank

Include the header that defines bool, true and false.

#include <>
✓ Correct! Not quite. Try again.
Multiple choice

In C, which value is false?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services