Computer Science Canada Is Case Statements useful? |
Author: | livingheaven [ 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 ???? |
Author: | Insectoid [ 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. |
Author: | livingheaven [ 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 |
Author: | chrisbrown [ Mon Apr 12, 2010 5:55 pm ] | ||||
Post subject: | RE:Is Case Statements useful? | ||||
They are useful for menus. For example:
is much cleaner than:
They are less robust than if statements but their simplicity makes for cleaner code. |
Author: | livingheaven [ 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 ???? |
Author: | chrisbrown [ 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. |
Author: | livingheaven [ 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 ... |
Author: | Tony [ 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. |
Author: | livingheaven [ 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 ![]() |
Author: | Kharybdis [ 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. |
Author: | livingheaven [ Sat Apr 17, 2010 2:41 pm ] |
Post subject: | Re: Is Case Statements useful? |
i know, but teacher force us to use it lol... ![]() |
Author: | wtd [ 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. |
Author: | ProgrammingFun [ 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. |
Author: | livingheaven [ 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.. |