HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
if / elseswitchforwhile
🧩 Function
👉 Pointer
C›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision making and repetition.

CommandDescription
if / elseExecutes code based on a condition.
switchMulti-branch comparison.
forLoops a specific number of times.
whileLoops as long as the condition is true.

if / else

Try it yourself →

Executes code based on a condition.

Syntax

if (condition) { } else { }

Description

Condition is inside parentheses. In C, 0 is considered false, non-zero is true (no separate bool needed).

Example

#include <stdio.h>
int main() {
    int score = 75;
    if (score >= 50) {
        printf("Passed\n");
    } else {
        printf("Failed\n");
    }
    return 0;
}

Expected output

Passed

When to use?

Flow branching.

switch

Try it yourself →

Multi-branch comparison.

Syntax

switch (value) { case x: ...; break; }

Description

Don't forget to put a break after each case.

Example

#include <stdio.h>
int main() {
    int day = 2;
    switch (day) {
        case 1: printf("Mon\n"); break;
        case 2: printf("Tue\n"); break;
        default: printf("?\n");
    }
    return 0;
}

Expected output

Tue

When to use?

Menu, status.

for

Try it yourself →

Loops a specific number of times.

Syntax

for (int i = 0; i < n; i++) { }

Description

Initialization; condition; increment.

Example

#include <stdio.h>
int main() {
    for (int i = 1; i <= 3; i++) {
        printf("%d\n", i);
    }
    return 0;
}

Expected output

1
2
3

When to use?

Known repetition.

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while (condition) { }

Description

When the number of repetitions is unknown. Don't forget to increment.

Example

#include <stdio.h>
int main() {
    int s = 0;
    while (s < 3) {
        printf("%d\n", s);
        s++;
    }
    return 0;
}

Expected output

0
1
2

When to use?

Indefinite repetition.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code