Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Is Case Statements useful?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
livingheaven




PostPosted: 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 ????
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
livingheaven




PostPosted: 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
chrisbrown




PostPosted: 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.
livingheaven




PostPosted: 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

????
chrisbrown




PostPosted: 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.
livingheaven




PostPosted: 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

...
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
livingheaven




PostPosted: 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
Kharybdis




PostPosted: 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.
livingheaven




PostPosted: 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
wtd




PostPosted: 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.
ProgrammingFun




PostPosted: 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 Mr. Green .

Anyways, case statements are useful in simple programs whereas if statements (although messy) can be used anywhere.
livingheaven




PostPosted: 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..
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: