
-----------------------------------
SwAnK
Sun Jun 05, 2005 9:40 pm

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

-----------------------------------
c0bra54
Sun Jun 05, 2005 10:49 pm


-----------------------------------
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
Mon Jun 06, 2005 11:19 am


-----------------------------------
This is the difference from the tuirng help file:

CASE STATMENT


case selection statement 

Syntax   A caseStatement is:
  case expn of 
    { label compileTimeExpn {, compileTimeExpn } : 
      statementsAndDeclarations } 
    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"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

if statement 

Syntax   An ifStatement is:
  if trueFalseExpn then 
    statementsAndDeclarations 
  { elsif trueFalseExpn then 
    statementsAndDeclarations } 
  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
Mon Jun 06, 2005 3:02 pm


-----------------------------------
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
Mon Jun 06, 2005 3:36 pm


-----------------------------------
Ya that is the right idea.

-----------------------------------
syphon4
Mon Jun 06, 2005 6:25 pm

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
Mon Jun 06, 2005 9:50 pm


-----------------------------------
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 :P

-----------------------------------
Bacchus
Tue Jun 07, 2005 5:37 am


-----------------------------------
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, :D

@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

-----------------------------------
StarGateSG-1
Tue Jun 07, 2005 11:24 am


-----------------------------------
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
Tue Jun 07, 2005 12:49 pm


-----------------------------------
Know both.  They both have their place in programming.

-----------------------------------
c0bra54
Tue Jun 07, 2005 3:36 pm


-----------------------------------
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 :P.. lol i think i might agree that turing does have it's place in highschool, 

if students = "learning" then 
turing := "good"
end if

 :)

-----------------------------------
wtd
Tue Jun 07, 2005 5:19 pm


-----------------------------------

if students = "learning" then 
turing := "good"
end if


Better yet, let's eliminate the extraneous "end if" by using an expression oriented language.  :)

let ocaml = 
   if students = "learning" then
      "good"
   else
      "bad"

-----------------------------------
FeZbOy
Tue Jun 07, 2005 7:14 pm


-----------------------------------
IF STATEMENTS
Good for logic and checking back for possible errors
CASE STATEMENTS
Good for 1 letter menus and "getch"es

-----------------------------------
c0bra54
Tue Jun 07, 2005 8:32 pm


-----------------------------------
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
Tue Jun 07, 2005 9:28 pm


-----------------------------------
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 :D

-----------------------------------
c0bra54
Tue Jun 07, 2005 9:33 pm


-----------------------------------
PS.. wtd.. whats that..

-----------------------------------
wtd
Tue Jun 07, 2005 11:35 pm


-----------------------------------
PS.. wtd.. whats that..

It's a programming language I'm rather fond of.  It's oriented more towards expressions than statements.  An expression is a piece of code which returns some new value, while a statement modifies an existing variable.
