HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
if / elseswitchforfor-each ( : )while
🧩 Method & Class
Java›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.
for-each ( : )Iterates over each element of an array/list.
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 in { }. Intermediate conditions with else if.

Example

public class Main {
    public static void main(String[] args) {
        int score = 75;
        if (score >= 50) {
            System.out.println("Passed");
        } else {
            System.out.println("Failed");
        }
    }
}

Expected output

Passed

When to use?

Flow branching.

Common mistakes

  • ⚠️ String equality is checked with .equals(), not ==.

switch

Try it yourself →

Multi-branch comparison.

Syntax

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

Description

Don't forget to put break after each case (otherwise the following ones will also execute).

Example

public class Main {
    public static void main(String[] args) {
        int day = 2;
        switch (day) {
            case 1: System.out.println("Mon"); break;
            case 2: System.out.println("Tue"); break;
            default: System.out.println("?");
        }
    }
}

Expected output

Tue

When to use?

Menu, state control.

for

Try it yourself →

Loops a specific number of times.

Syntax

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

Description

Initialization; condition; increment. The most common loop.

Example

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            System.out.println(i);
        }
    }
}

Expected output

1
2
3

When to use?

Known repetition.

for-each ( : )

Try it yourself →

Iterates over each element of an array/list.

Syntax

for (type e : arr) { }

Description

Iterates over elements without dealing with indices.

Example

public class Main {
    public static void main(String[] args) {
        for (String color : new String[]{"red", "purple"}) {
            System.out.println(color);
        }
    }
}

Expected output

red
purple

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. Don't forget to increment.

Example

public class Main {
    public static void main(String[] args) {
        int s = 0;
        while (s < 3) {
            System.out.println(s);
            s++;
        }
    }
}

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