AnyCompiler

Operators and Control Flow

if and else work exactly as you'd expect — a condition in parentheses, followed by a block. C has no built-in boolean type before C99's stdbool.h; any nonzero value is treated as true, and 0 is treated as false.

for and while

A classic for loop declares an index, a condition, and an increment all in its header. while just repeats as long as its condition holds.

#include <stdio.h>

int main() {
    int score = 73;
    if (score >= 60) {
        printf("pass\n");
    } else {
        printf("fail\n");
    }
    for (int i = 1; i <= 3; i++) {
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
Output
pass
1 2 3 

Next: functions — defining functions, parameters, and why C needs you to declare a function before you call it.

Practice exercise

Set a variable score of type int to 73. Print "pass" if score is 60 or above, otherwise print "fail".