Author |
Message |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Mon Apr 12, 2010 5:00 pm Post subject: Is Case Statements useful? |
|
|
I have a question about case statement. During my compsci class, teacher says we have to use case, but is just like if statement, at some point is worst than if statement
Such as
if a > 1 then
end if
to do that in case you have to do
case a of
lable whatever that is bigger than 1 :
end case
is there a true use for case ???? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Mon Apr 12, 2010 5:06 pm Post subject: RE:Is Case Statements useful? |
|
|
Case statements are better for larger conditionals. They're more organized. If you have like 15 different conditions, it's far neater to do case/label than if/elseif. |
|
|
|
|
![](images/spacer.gif) |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Mon Apr 12, 2010 5:35 pm Post subject: Re: Is Case Statements useful? |
|
|
but case statements can't do if something is larger than some thing
for example
case total of
label total >0 and total >2 :
total := 2
end case |
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Mon Apr 12, 2010 5:55 pm Post subject: RE:Is Case Statements useful? |
|
|
They are useful for menus.
For example: code: | var input : int
get input
case input of
label 1: put "input=1"
label 2: put "input=2"
label 3: put "input=3"
label: exit
end case |
is much cleaner than: code: | if input = 1 then
put "input=1"
elsif input = 2 then
put "input=2"
elsif input = 3 then
put "input=3"
else
exit
end if |
They are less robust than if statements but their simplicity makes for cleaner code. |
|
|
|
|
![](images/spacer.gif) |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Mon Apr 12, 2010 5:59 pm Post subject: Re: Is Case Statements useful? |
|
|
so you cant do
case a of
label a > 0 : put 1
end case
???? |
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Mon Apr 12, 2010 6:15 pm Post subject: RE:Is Case Statements useful? |
|
|
Nope, equivalence only. The compiler/inperpreter looks at the value of the case variable and scans through the labels until it finds one that matches, then executes that instruction. This allows for optimization: since it doesnt have to do any extra calculations, case statements should be slightly faster, though the difference won't be noticeable. |
|
|
|
|
![](images/spacer.gif) |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Mon Apr 12, 2010 6:21 pm Post subject: Re: Is Case Statements useful? |
|
|
ah men....
this means that i have to some thing like this ..
case a of
label 1,2,3,4,5,6,7,8,9 :
end case
... |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Mon Apr 12, 2010 6:55 pm Post subject: RE:Is Case Statements useful? |
|
|
you could perform the comparison _before_ the case, store result in a boolean variable, and do a case on that temporary result.
Though this might be a trick question that wants you to find and use the default label. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Mon Apr 12, 2010 8:03 pm Post subject: Re: Is Case Statements useful? |
|
|
ok .. i'll give it a try
BTW thx guys for helping me ![Very Happy Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Kharybdis
![](http://compsci.ca/v3/uploads/user_avatars/111642592477ec7e78e574.gif)
|
Posted: Sat Apr 17, 2010 2:22 pm Post subject: RE:Is Case Statements useful? |
|
|
Yo, case statements are much better to do menus with (where you have options) than if statements. |
|
|
|
|
![](images/spacer.gif) |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Sat Apr 17, 2010 2:41 pm Post subject: Re: Is Case Statements useful? |
|
|
i know, but teacher force us to use it lol... ![Sad Sad](http://compsci.ca/v3/images/smiles/icon_sad.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sun Apr 18, 2010 2:34 am Post subject: RE:Is Case Statements useful? |
|
|
When case statements make the code cleaner, they're useful. When they don't... they're not. |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Sun Apr 18, 2010 2:28 pm Post subject: RE:Is Case Statements useful? |
|
|
In the beginning, I loved case statements because of their simplicity...but when I unlocked the full potential of if statements, I moved on .
Anyways, case statements are useful in simple programs whereas if statements (although messy) can be used anywhere. |
|
|
|
|
![](images/spacer.gif) |
livingheaven
![](http://compsci.ca/v3/uploads/user_avatars/12343300074bba815f781c7.png)
|
Posted: Mon Apr 19, 2010 10:46 pm Post subject: Re: Is Case Statements useful? |
|
|
..Found the answer lol =D
thx u guys..
Btw Tony thx for the help
+ bits.. |
|
|
|
|
![](images/spacer.gif) |
|