1. still on namespace
- Posted by mexzony Dec 29, 2010
- 1058 views
if color = 1 then puts(1, "Red") elsif color = 2 then puts(1, "Green") else puts(1, "Blue") end if
look at the code above and the switch statement below
switch color do case 1 then puts(1, "Red") case 2 then puts(1, "Green") case else puts(1, "Blue") end switch
now my question is this,in the if statement the condition is that if color is equql to 1 then we output the string "Red".in the equivalent switch statement after switch color we say case i then puts(1,"Red).is it that in case 1 the value returned will be 1 and then red will be output to the screen and the other case 2 the value 2 will be returned. suppose in the if statement i were to replace the color = 1 with color = 4 would i now say in the switch statement case 4 then puts(1, "Red"). also pls explain the fallthru statement.thanks
2. Re: still on namespace
- Posted by irv Dec 29, 2010
- 1010 views
There is no value returned from the switch statement.
The only output from this is done by the print statement(s). Of course, what happens in the case statement is not limited to printing stuff - you can modify variables, call functions, etc.
3. Re: still on namespace
- Posted by irv Dec 29, 2010
- 971 views
if color = 1 then puts(1,"RED") -- is the same as: switch color do case 1 then puts(1,"RED")
if color were set to = 4 (or 5, or 6, or -1 etc.) then the else statement would be activated, printing "blue". Therefore, if you can't provide for an exact match for every situation, then the else statement should probably print something like "Some other color!"
Now, suppose you wanted to print "RED" if color was 1 or color was 4.
switch color do case 1,4 then puts(1,"RED") case 2 then ...
You can have more than one value in a case, just separate with commas. You can see how this might be better than having to code:
if color = 1 or color = 4 then ...
especially when there might be 10 or more possibilities to check!