⚡ Code it · 40 XP
Loops, conditionals and switch
Loops, conditionals and switch
if / else
if (xp >= 1000) {
rank = 'Legend';
} else if (xp >= 100) {
rank = 'Hero';
} else {
rank = 'Novice';
}
Comparisons: ==, !=, <, >=… Logic: &&, ||, !. String == is case-insensitive in Apex ('a' == 'A' is true). Use .equals() when case matters.
Loops
for (Integer i = 0; i < 10; i++) { ... } // classic
for (Account a : accounts) { ... } // for-each: you'll use this most
while (queue.size() > 0) { ... }
do { ... } while (condition);
break and continue work as you'd expect.
There's also a special SOQL for loop that processes query results in chunks of 200, which saves heap on big queries:
for (Account a : [SELECT Id FROM Account]) { ... }
switch
switch on status {
when 'New' { ... }
when 'In Progress', 'Blocked' { ... } // multiple values
when null { ... }
when else { ... }
}
switch on works with Integer, Long, String, sObject types and enums. It can't match ranges, so use if/else for "greater than" logic.
Your mission
scores and day already exist. Count, find the max, grade it, and name the day.