HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C++VariablesData TypesOperatorsStringsVectors (vector)Conditions (if / switch)Loops (for / while)FunctionsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Vector
🔁 Condition & Loop
if / elseforRange-based for ( : )while
🧩 Function & Class
C++›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision making and repetition.

CommandDescription
if / elseExecutes code based on a condition.
forLoops a specific number of times.
Range-based for ( : )Iterates over array/vector elements.
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 in parentheses, blocks inside { }.

Example

#include <iostream>
using namespace std;
int main() {
    int score = 75;
    if (score >= 50) cout << "Passed" << endl;
    else cout << "Failed" << endl;
    return 0;
}

Expected output

Passed

When to use?

Flow branching.

for

Try it yourself →

Loops a specific number of times.

Syntax

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

Description

Initialization; condition; increment.

Example

#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 3; i++) {
        cout << i << endl;
    }
    return 0;
}

Expected output

1
2
3

When to use?

Known repetition.

Range-based for ( : )

Try it yourself →

Iterates over array/vector elements.

Syntax

for (type e : collection) { }

Description

Iterates over elements without dealing with indices (C++11+).

Example

#include <iostream>
#include <vector>
using namespace std;
int main() {
    for (int x : {10, 20, 30}) {
        cout << x << endl;
    }
    return 0;
}

Expected output

10
20
30

When to use?

Iterating collections.

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while (condition) { }

Description

When the number of repetitions is unknown.

Example

#include <iostream>
using namespace std;
int main() {
    int s = 0;
    while (s < 3) {
        cout << s << endl;
        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