5. If Statements and Choices
Learn If Statements and Choices through beginner-friendly explanations, working GOCO examples, and their expected output.
5.1 The If, Elseif, and Else rules
Programs can make smart choices. It will check a rule. If the rule is True, it does the action. If not, it can check another rule using Elseif or do a default action using Else.
goco
Number age = 10.If (age > 18) {Displaynl("You are an adult.").}Elseif (age == 18) {Displaynl("You just became an adult!").}Else {Displaynl("You are a kid.").}
5.2 Using Switch for Many Choices
When you have lots of exact choices to pick from, a Switch is cleaner than writing many If rules.
goco
Number luck = 2.Switch(luck) {Case(1) {Displaynl("Gold Coin!").}Case(2) {Displaynl("Silver Coin!").}Default {Displaynl("No Coin!").}}