
-----------------------------------
Tubs
Tue Feb 25, 2003 11:01 am

Case compared to If
-----------------------------------
my first year of programming i was taught to use if statements to do the exact same thing as case statements. now i am introduced to case statements and told i have to use them in place of my old if statements, but i find that they are way less straightforward... what do you guys think? case or if?

ex.

var Choice : string

put "blahblahblah"..
get Choice

if Choice = "blah" or Choice = "whatever else it may be" then

%do whatever

elsif ..

end if


compared to

case

label 1:

label 2:

etc

-----------------------------------
Tony
Tue Feb 25, 2003 3:02 pm


-----------------------------------
as far as I understand it, IF statments give you much more control over the situation. Such as you can write complex situations with AND, OR, NOT=, , etc.

Case statment just checks for the = statment, but it checks it for multiple values. Such as:


case mark of
            label 9, 10 :   put "Excellent"
            label 7, 8 :        put "Good"
            label 6 :       put "Fair"
            label :     put "Poor"
        end case
 outputs how good your mark is. It is equivalent to


var flag=0
if (mark = 9 or mark = 10) then
put "excellent"
flag = 1
end if
if (mark = 7 or mark = 8) then
put "good"
flag = 1
end if
if (mark = 6) then
put "fair"
flag=1
end if
if (flag = 0) then
put "poor"
end if


As you can see, it takes more lines to write out using IF statments, but they give you more control when you have to solve a complex statment like

if (a=b and b=c and a not=d) or (d=e) then...
try coding THAT using case statment :wink:

-----------------------------------
Tubs
Wed Feb 26, 2003 6:18 am


-----------------------------------
i dont know any of the and or any if 'add-ons' yet. but i suspect i will this semester

-----------------------------------
Tony
Wed Feb 26, 2003 8:43 am


-----------------------------------
basically keywords and, or link more then 1 if statment to produce an overall result.

such as

if (tony = "cool" AND dan = "cool") then
put "swat is cool"
end if


the program will output "swat is cool" only if both tony AND dan equal to "cool". If one of those statments return false, then the overall if statment is also false.

-----------------------------------
Tubs
Wed Feb 26, 2003 9:19 am


-----------------------------------
so the and / or modifiers do the same thing
