ScratchData LogoScratchData
Back to gtoal's profile

switch/case statement substitute

GTgtoal•Created October 5, 2014
switch/case statement substitute
8
3
383 views
View on Scratch

Instructions

Look inside at the code. Copy it to your backpack for reuse. Sometimes you need to do different actions depending on the value of a variable, and a long chain of if/then/else's is too expensive at run-time. In most languages, a switch or 'computed goto' uses a lookup table of destinations to efficiently jump to the desired option. Scratch doesn't support such a thing, but a binary decision tree like this will decode the value in log_2(n) steps rather than (n) steps.

Description

This is useful in programs like emulators, where you need to decode an opcode quickly, such as @DCPU-16's Gameboy Emulator http://scratch.mit.edu/projects/10098276/ Example decodes 32 cases. Easy to follow the pattern and extend this.

Project Details

Project ID28547022
CreatedOctober 5, 2014
Last ModifiedOctober 5, 2014
SharedOctober 5, 2014
Visibilityvisible
CommentsAllowed