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

Username:   Password: 
 RegisterRegister   
 Case of ~VS~ If statments
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SwAnK




PostPosted: Sun Jun 05, 2005 9:40 pm   Post subject: Case of ~VS~ If statments

hey quick question
People say case of is better to use then if statments, for their different reasons. Why?? I would have to agree with them on the most part ,their easy to write, look better, and probably a little faster, but is thier any other advantage from using ifs??
thanx
Sponsor
Sponsor
Sponsor
sponsor
c0bra54




PostPosted: Sun Jun 05, 2005 10:49 pm   Post subject: (No subject)

um, well i don't know about advantages, personally i find i can trace my logic better in if statements.. plus do case statements (hardly use em) work with and or, and xor??
StarGateSG-1




PostPosted: Mon Jun 06, 2005 11:19 am   Post subject: (No subject)

This is the difference from the tuirng help file:

CASE STATMENT

Quote:

case selection statement

Syntax A caseStatement is:
case expn of
{ label compileTimeExpn {, compileTimeExpn } :
statementsAndDeclarations }
[ label :
statementsAndDeclarations ]
end case

Description A case statement is used to choose among a set of statements (and declarations). One set is chosen and executed and then execution continues just beyond end case.
The expression (expn) following the keyword case is evaluated and used to select one of the alternatives (sets of declarations and statements) for execution. The selected alternative is the one having a label value equaling the case expression. If none are equal and there is a final label with no expression, that alternative is selected.

Example Output a message based on value of mark.

case mark of
label 9, 10 : put "Excellent"
label 7, 8 : put "Good"
label 6 : put "Fair"
label : put "Poor"
end case

Example Output a message based on value of name.

case name of
label "horse", "cow" : put "Farm animal"
label "tiger", "lion" : put "Jungle animal"
label "cat", "dog" : put "Pet"[code]
label : put "Unknown animal"
end case

Details The case expression is required to match one of the labels. If it does not, there must be a final label with no expression. Label expressions must have values known at compile time. All label values must be distinct. The case expression and the label values must have the same equivalent type, which must be an integer, char, boolean, an enum type or strings.
Note that there is no way to express a range of values (for example from 5 to 10) in a label. Each individual value must be expressed in the label.



IF STATMENT

Quote:
if statement

Syntax An ifStatement is:
if trueFalseExpn then
statementsAndDeclarations
{ elsif trueFalseExpn then
statementsAndDeclarations }
[ else
statementsAndDeclarations ]
end if

Description An if statement is used to choose among a set of statements (and declarations). One set (at most) is chosen and executed and then execution continues just beyond end if.
The expressions (the trueFalseExpressions) following the keyword if and each elsif are checked one after the other until one of them is found to be true, in which case the statements (and declarations) following the corresponding then are executed. If none of these expressions evaluates to true, the statements following else are executed. If no else is present and none of the expressions are true, no statements are executed and execution continues following the end if.


Example Output a message based on value of mark.

if mark >= 50 then
put "You pass"
else
put "You fail"
end if

Example Output A, B, C, D or F depending on mark.

if mark >= 80 then
put "A"
elsif mark >= 70 then
put "B"
elsif mark >= 60 then
put "C"
elsif mark >= 50 then
put "D"
else
put "F"
end if

Example If x is negative, change its sign.

if x < 0 then
x := - x
end if

Example If x is less than zero or greater than maxx, put a message.

if x < 0 or x > maxx then
put "Out of bounds!"
end if

Example If the boolean flag is true and name is "stop", put a message and return.

if flag and name = "stop" then
put "Exiting routine"
return
end if
Details Several statements and declarations can appear after a particular then.
c0bra54




PostPosted: Mon Jun 06, 2005 3:02 pm   Post subject: (No subject)

yeh cae statements must be known at time of compile.. so in other wards, i am guessing they ar not very good for whatdot colour detection or nything else like that, but instead much better for text based menus

where you know what the answers will be...
StarGateSG-1




PostPosted: Mon Jun 06, 2005 3:36 pm   Post subject: (No subject)

Ya that is the right idea.
syphon4




PostPosted: Mon Jun 06, 2005 6:25 pm   Post subject: case vs if

although case statements are quicker and easier in some cases........if statements are very valuable and you should know how to use if startement.........i have run into things that could not work with case but worked with if
c0bra54




PostPosted: Mon Jun 06, 2005 9:50 pm   Post subject: (No subject)

if = god lol

and if + whatdotcolour = better then god

and for loops + if + whatdotcolour, = the best thing since sliced bread in my opinion

-edit- ya happy now Razz
Bacchus




PostPosted: Tue Jun 07, 2005 5:37 am   Post subject: (No subject)

If statments give you more freedom to choose what you want to do. Case statments I find, only do this = this or this operator this = true. If statment I like much better, Very Happy

@c0bra54: Yes, If statments are the best thing since sliced bead, but I don't think that gives them much credit. I think their the best thing since toasted bread
Sponsor
Sponsor
Sponsor
sponsor
StarGateSG-1




PostPosted: Tue Jun 07, 2005 11:24 am   Post subject: (No subject)

No offence Bacchus but if, statments are much older then both of those inventions. If statements have been around since the beginning of decision making, maybe not in a programing term as we know it now, but as a way of thinking
wtd




PostPosted: Tue Jun 07, 2005 12:49 pm   Post subject: (No subject)

Know both. They both have their place in programming.
c0bra54




PostPosted: Tue Jun 07, 2005 3:36 pm   Post subject: (No subject)

so true ... but yeh programming in turing is almsot like speeking english..

whatdotcolour (13,20) what dot colour, is 13,20 x and y.. it's like english... but most languages aren't quite as neat Razz.. lol i think i might agree that turing does have it's place in highschool,
code:

if students = "learning" then
turing := "good"
end if

Smile
wtd




PostPosted: Tue Jun 07, 2005 5:19 pm   Post subject: (No subject)

c0bra54 wrote:
code:

if students = "learning" then
turing := "good"
end if


Better yet, let's eliminate the extraneous "end if" by using an expression oriented language. Smile

code:
let ocaml =
   if students = "learning" then
      "good"
   else
      "bad"
FeZbOy




PostPosted: Tue Jun 07, 2005 7:14 pm   Post subject: (No subject)

IF STATEMENTS
Good for logic and checking back for possible errors
CASE STATEMENTS
Good for 1 letter menus and "getch"es
c0bra54




PostPosted: Tue Jun 07, 2005 8:32 pm   Post subject: (No subject)

ew one letter menu's lol.. with RGB mode, you canhave like ... 24 bit colour depth.. i think.. and have all those menu's be pure GUI, using simple whatdotcolour (but RGB form)
Bacchus




PostPosted: Tue Jun 07, 2005 9:28 pm   Post subject: (No subject)

StarGateSG-1 wrote:
No offence Bacchus but if, statments are much older then both of those inventions. If statements have been around since the beginning of decision making, maybe not in a programing term as we know it now, but as a way of thinking
Yes I know, I was noting c0bra54's superb spelling.

@c0bra54: Yes I am happy now Very Happy
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 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: