ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C Programming Tutorial• 6 min read

Control Flow: if-else, switch & Loops

Evaluating truthiness in C (non-zero is true), break, and continue

In C, there was historically no native boolean type. Any expression evaluating to 0 is FALSE, and any NON-ZERO value is TRUE.

Truthiness and Loop Constructs

Zero is False: if (0) does not execute; if (42) or if (-1) executes because non-zero integers are logically true.

stdbool.h: C99 introduced <stdbool.h> providing bool, true (1), and false (0).

switch: Operates strictly on integer or character expressions. Fallthrough occurs unless terminated with break.

Loops and Truthiness in C
#include <stdio.h>
#include <stdbool.h>

int main(void) {
    bool isRunning = true;
    int counter = 3;

    while (counter) { // Runs while counter is non-zero (3, 2, 1)
        printf("Tick: %d\n", counter);
        counter--;
    }
    printf("Blastoff!\n");
    return 0;
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

In C, how does the compiler evaluate the integer condition `if (-5)`?