Skip to content

Control Flow

Control Flow#

Control flow determines how your program executes code. It allows programs to make decisions, repeat actions, and choose different paths based on conditions.

graph TD
    A[Control Flow Types] --> B[Conditional Statements]
    A --> C[Switch Statements]
    A --> D[Loops]

    B --> B1[if/else<br/>Make decisions based on conditions]
    C --> C1[switch/match<br/>Choose between multiple options]
    D --> D1[for loops<br/>Repeat known number of times]
    D --> D2[while loops<br/>Repeat while condition is true]
    D --> D3[do-while loops<br/>Execute at least once, then repeat]

    style A fill:#e8f5e8
    style B fill:#e3f2fd
    style C fill:#e3f2fd
    style D fill:#e3f2fd
    style B1 fill:#c8e6c9
    style C1 fill:#c8e6c9
    style D1 fill:#fff3e0
    style D2 fill:#fff3e0
    style D3 fill:#fff3e0

There are three main types of control flow:

1. Conditional Statements (if/else)#

Make decisions based on conditions. If a condition is true, do one thing; otherwise, do something else.

int budget = 500;

if (budget >= 1000) {
    System.out.println("High-end guitar");
} else if (budget >= 500) {
    System.out.println("Intermediate guitar");
} else {
    System.out.println("Beginner guitar");
}
val budget = 500

if (budget >= 1000) {
    println("High-end guitar")
} else if (budget >= 500) {
    println("Intermediate guitar")
} else {
    println("Beginner guitar")
}
const budget = 500;

if (budget >= 1000) {
    console.log("High-end guitar");
} else if (budget >= 500) {
    console.log("Intermediate guitar");
} else {
    console.log("Beginner guitar");
}
int budget = 500;

if (budget >= 1000) {
    print("High-end guitar");
} else if (budget >= 500) {
    print("Intermediate guitar");
} else {
    print("Beginner guitar");
}
let budget = 500

if budget >= 1000 {
    print("High-end guitar")
} else if budget >= 500 {
    print("Intermediate guitar")
} else {
    print("Beginner guitar")
}
budget = 500

if budget >= 1000:
    print("High-end guitar")
elif budget >= 500:
    print("Intermediate guitar")
else:
    print("Beginner guitar")

2. Switch Statements#

Choose between multiple options based on a single value. More efficient than long if-else chains.

int budget = 500;

switch (budget) {
    case 1000:
        System.out.println("High-end guitar");
        break;
    case 500:
        System.out.println("Intermediate guitar");
        break;
    default:
        System.out.println("Beginner guitar");
}
val budget = 500

when (budget) {
    1000 -> println("High-end guitar")
    500 -> println("Intermediate guitar")
    else -> println("Beginner guitar")
}
const budget = 500;

switch (budget) {
    case 1000:
        console.log("High-end guitar");
        break;
    case 500:
        console.log("Intermediate guitar");
        break;
    default:
        console.log("Beginner guitar");
}
int budget = 500;

switch (budget) {
    case 1000:
        print("High-end guitar");
        break;
    case 500:
        print("Intermediate guitar");
        break;
    default:
        print("Beginner guitar");
}
let budget = 500

switch budget {
    case 1000:
        print("High-end guitar")
    case 500:
        print("Intermediate guitar")
    default:
        print("Beginner guitar")
}
budget = 500

match budget:
    case 1000:
        print("High-end guitar")
    case 500:
        print("Intermediate guitar")
    case _:
        print("Beginner guitar")

3. Loops#

Repeat code multiple times. Three main types:

For Loops#

Repeat a specific number of times or iterate through collections.

String[] items = {"A", "B", "C"};

for (int i = 0; i < items.length; i++) {
    System.out.println(items[i]);
}
val items = listOf("A", "B", "C")

for (item in items) {
    println(item)
}
const items = ["A", "B", "C"];

for (let i = 0; i < items.length; i++) {
    console.log(items[i]);
}
List<String> items = ["A", "B", "C"];

for (int i = 0; i < items.length; i++) {
    print(items[i]);
}
let items = ["A", "B", "C"]

for i in 0..<items.count {
    print(items[i])
}
items = ["A", "B", "C"]

for item in items:
    print(item)

While Loops#

Repeat while a condition is true.

int count = 0;

while (count < 3) {
    System.out.println("Count: " + count);
    count++;
}
var count = 0

while (count < 3) {
    println("Count: $count")
    count++
}
let count = 0;

while (count < 3) {
    console.log("Count: " + count);
    count++;
}
int count = 0;

while (count < 3) {
    print("Count: $count");
    count++;
}
var count = 0

while count < 3 {
    print("Count: \(count)")
    count += 1
}
count = 0

while count < 3:
    print(f"Count: {count}")
    count += 1

Do-While Loops#

Execute at least once, then repeat while condition is true.

int count = 0;

do {
    System.out.println("Count: " + count);
    count++;
} while (count < 3);
var count = 0

do {
    println("Count: $count")
    count++
} while (count < 3)
let count = 0;

do {
    console.log("Count: " + count);
    count++;
} while (count < 3);
int count = 0;

do {
    print("Count: $count");
    count++;
} while (count < 3);
var count = 0

repeat {
    print("Count: \(count)")
    count += 1
} while count < 3
count = 0

while True:
    print(f"Count: {count}")
    count += 1
    if count >= 3:
        break

Loop Control#

  • break: Exit the loop immediately
  • continue: Skip to next iteration