Control Flow
Control flow statements allow you to control the execution path of your program.
If / Else Statements
Basic If
let age = 18;
if (age >= 18) {
print("Adult");
}If-Else
let age = 15;
if (age >= 18) {
print("Adult");
} else {
print("Minor");
}If-Else If-Else
let age = 15;
if (age < 13) {
print("Child");
} else if (age < 18) {
print("Teenager");
} else {
print("Adult");
}Nested If Statements
let score = 85;
let extra_credit = true;
if (score >= 60) {
if (extra_credit) {
print("Pass with extra credit!");
} else {
print("Pass");
}
} else {
print("Fail");
}While Loops
Execute a block of code repeatedly while a condition is true:
let i = 0;
while (i < 5) {
print(i);
i = i + 1;
}
// Output: 0 1 2 3 4While Loop with Complex Condition
let count = 0;
let total = 0;
while (count < 10 && total < 50) {
total = total + count;
count = count + 1;
}
print("Count: " + tostring(count));
print("Total: " + tostring(total));Infinite Loop (with Break)
let i = 0;
while (true) {
print(i);
i = i + 1;
if (i >= 5) {
break;
}
}Break Statement
Exit a loop early:
let i = 0;
while (i < 10) {
if (i == 5) {
break; // Exit loop when i is 5
}
print(i);
i = i + 1;
}
// Output: 0 1 2 3 4Break in Nested Loops
let i = 0;
while (i < 3) {
let j = 0;
while (j < 3) {
if (j == 2) {
break; // Only breaks inner loop
}
print("i=" + tostring(i) + ", j=" + tostring(j));
j = j + 1;
}
i = i + 1;
}Continue Statement
Skip the rest of the current iteration:
let i = 0;
while (i < 5) {
i = i + 1;
if (i == 3) {
continue; // Skip when i is 3
}
print(i);
}
// Output: 1 2 4 5 (3 is skipped)Continue with Multiple Conditions
let i = 0;
while (i < 10) {
i = i + 1;
// Skip even numbers
if (i % 2 == 0) {
continue;
}
print(i); // Only odd numbers printed
}
// Output: 1 3 5 7 9Combining Break and Continue
let i = 0;
while (i < 10) {
i = i + 1;
if (i == 3) {
continue; // Skip 3
}
if (i == 7) {
break; // Stop at 7
}
print(i);
}
// Output: 1 2 4 5 6Common Patterns
Iterate Over Array
let numbers = [10, 20, 30, 40, 50];
let i = 0;
while (i < len(numbers)) {
print(numbers[i]);
i = i + 1;
}Sum Array Elements
let numbers = [1, 2, 3, 4, 5];
let sum = 0;
let i = 0;
while (i < len(numbers)) {
sum = sum + numbers[i];
i = i + 1;
}
print("Sum: " + tostring(sum)); // 15Find Element in Array
let names = ["Alice", "Bob", "Charlie", "Diana"];
let search = "Charlie";
let found = false;
let i = 0;
while (i < len(names)) {
if (names[i] == search) {
found = true;
break;
}
i = i + 1;
}
if (found) {
print("Found " + search);
} else {
print(search + " not found");
}Process Until Condition
let balance = 100;
let day = 1;
while (balance > 0) {
let expense = day * 5;
if (expense > balance) {
print("Insufficient funds on day " + tostring(day));
break;
}
balance = balance - expense;
print("Day " + tostring(day) + ": $" + tostring(balance) + " remaining");
day = day + 1;
}Menu Loop
let running = true;
while (running) {
print("1. Option A");
print("2. Option B");
print("3. Exit");
let choice = 3; // Simulated input
if (choice == 1) {
print("You selected Option A");
} else if (choice == 2) {
print("You selected Option B");
} else if (choice == 3) {
print("Exiting...");
running = false;
} else {
print("Invalid choice");
}
}Logical Operators in Conditions
AND (&&)
let age = 25;
let has_license = true;
if (age >= 18 && has_license) {
print("Can drive");
}OR (||)
let is_weekend = true;
let is_holiday = false;
if (is_weekend || is_holiday) {
print("Day off!");
}NOT (!)
let is_raining = false;
if (!is_raining) {
print("Good weather");
}Complex Conditions
let age = 25;
let has_ticket = true;
let is_member = false;
if ((age >= 18 && has_ticket) || is_member) {
print("Entry granted");
} else {
print("Entry denied");
}Guard Clauses
Use early returns to avoid deep nesting:
func process_user(user) {
// Guard clauses
if (user == null) {
print("User is null");
return;
}
if (user["age"] < 18) {
print("User is too young");
return;
}
if (!user["is_active"]) {
print("User is inactive");
return;
}
// Main logic
print("Processing user: " + user["name"]);
}Tips and Best Practices
Avoid Infinite Loops
// BAD - infinite loop
let i = 0;
while (i < 10) {
print(i);
// Forgot to increment i!
}
// GOOD
let i = 0;
while (i < 10) {
print(i);
i = i + 1;
}Use Meaningful Conditions
// OK
if (x > 0 && x < 100) {
// ...
}
// BETTER
let is_valid_percentage = x > 0 && x < 100;
if (is_valid_percentage) {
// ...
}Keep Conditions Simple
// Complex - hard to read
if ((a && b) || (c && d) || (e && f)) {
// ...
}
// Better - break down
let condition1 = a && b;
let condition2 = c && d;
let condition3 = e && f;
if (condition1 || condition2 || condition3) {
// ...
}Examples
FizzBuzz
let i = 1;
while (i <= 20) {
if (i % 15 == 0) {
print("FizzBuzz");
} else if (i % 3 == 0) {
print("Fizz");
} else if (i % 5 == 0) {
print("Buzz");
} else {
print(i);
}
i = i + 1;
}Fibonacci Sequence
let n = 10;
let a = 0;
let b = 1;
let i = 0;
while (i < n) {
print(a);
let temp = a + b;
a = b;
b = temp;
i = i + 1;
}Prime Number Checker
func is_prime(n) {
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
let i = 2;
while (i * i <= n) {
if (n % i == 0) {
return false;
}
i = i + 1;
}
return true;
}
let num = 17;
if (is_prime(num)) {
print(tostring(num) + " is prime");
} else {
print(tostring(num) + " is not prime");
}