? SWITCH/CASE Library ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password
What's going on?? See: Site Update March 2022

SWITCH/CASE Library

Submitted
Updated
Author
computablee
Public Key
REMOVED
Min. Compat.
All
Rating
5 votes
Description Were you one of many who were upset that SmileBASIC did not come with a switch/case-like command? I have a solution! My program! Instructions Switch/case statements are used in languages like C where you have one variable with many possible values, and you want to do something different for each value (e. g. If A==1, do this. If A==2, do this. If A==3, do this, etc.). Instead of writing tons of IF statements comparing your variable each time, a switch statement does the majority of the work for you! The first step is to load PRG:SWITCHFN into a program slot not in use. Then add USE [program slot SWITCHFN is in] to the top of your program. If you are testing a string, use this format: SWITCH$ String to test IF CASE$("comparison 1") THEN 'insert code here ENDIF IF CASE$("comparison 2") THEN 'insert code here ENDIF IF CASE$("comparison 3") THEN 'insert code here ENDIF 'et cetera ENDSW If you are testing an integer or decimal, use this format: SWITCH Variable to test IF CASE(comparison 1) THEN 'insert code here ENDIF IF CASE(comparison 2) THEN 'insert code here ENDIF IF CASE(comparison 3) THEN 'insert code here ENDIF 'et cetera ENDSW Example: USE 3 VAR S$="Hi!" SWITCH$ S$ IF CASE$("Hi!") THEN PRINT "Hi there!" ENDIF IF CASE$("Hello!") THEN PRINT "Hello there!" ENDIF IF CASE$("Sup?") THEN PRINT "Sup with you?" ENDIF ENDSW 'Output: Hi there!
Categories
Keywords
8 Comment(s) HTV04 HTV04 Forum Leader Hidden Achievements Third Year My account is over 3 years old Website Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Why was this removed? This would be very useful in my programs. RaichuBender RaichuBender Avatar Taboo I didn't change my avatar for 180 days Website Avatar Embargo I didn't change my avatar for 90 days Website Avatar Block I didn't change my avatar for 30 days. Website I use the ON command a lot, it works similarly and is good for my purpose. 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself It would be called SELECT CASE, since that's what it is in qbasic. SELECT CASE variable CASE value: do something CASE othervalue: do something else ENDSELECT SquareFingers SquareFingers SWITCHes cannot be nested. You could fix this with VAR TOSWITCH$[0], in the SWITCH procedures use UNSHIFT, in the CASE functions, instead of TRIGGERED% use LEN(TOSWITCH$)>0, instead of TOSWITCH$ use TOSWITCH$[0], and in ENDSW use SHIFT. snail_ snail_ Power User QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! Helper Received for being very helpful around SmileBASIC Source Achievements Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements The key issue with this library is it's required use of IFs, which the switch statements were intended to replace. NeatNit NeatNit First Week Joined in the very first week of SmileBASIC Source Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Night Person I like the quiet night and sleep late. Express Yourself I'm confused... What advantage does this have over comparison? I mean, drop the SWITCH and ENDSW statements, and replace every CASE(K) with (TEST%==K) and you get the exact same thing. Am I missing something? This does help a little bit with readability, but not by much. joelable joelable First Day Joined on the very first day of SmileBASIC Source Website Night Person I like the quiet night and sleep late. Express Yourself Video Games I like to play video games! Hobbies At first, when I looked at this library, I was like "pish that's what IF and ELSE does!". But then I started using JavaScript, and I was taught how to use SWITCH and CASE and I was like "WOW this will change everything! Anyways nice program. I like the way you did it. Mariominer Mariominer First Day Joined on the very first day of SmileBASIC Source Website Promoted Page My page was promoted on SmileBASIC Source! Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength This looks really nice! Though I'll probably just stick with my many IF's and ELSEIF's.