BASIC · lesson 9 of 30 · Decisions and loopsChoose from a menu
switch compares one value with several cases. break ends a case, default takes the rest.
09-switch.yab Download 09-switch.yab
print "1 show the time 2 show a greeting 3 stop"
input "Choice: " choice
switch choice
case 1
print "It is noon"
break
case 2
print "Good morning"
break
case 3
print "Stopped"
break
default
print "There is no choice ", choice
end switch
Terminal
$ yabasic 09-switch.yab
1 show the time 2 show a greeting 3 stop
Choice: 2
Good morning
$ yabasic 09-switch.yab
1 show the time 2 show a greeting 3 stop
Choice: 7
There is no choice 7Exercise
Add a fourth choice that prints today's date with date$.