Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Case compared to If
Author Message
Tubs

Posted: Tue Feb 25, 2003 11:01 am   Post subject: 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

Posted: Tue Feb 25, 2003 3:02 pm   Post subject: (No subject)

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:

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

 code: 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
Tony's programming blog. DWITE - a programming contest.
Tubs

Posted: Wed Feb 26, 2003 6:18 am   Post subject: (No subject)

i dont know any of the and or any if 'add-ons' yet. but i suspect i will this semester
Tony

Posted: Wed Feb 26, 2003 8:43 am   Post subject: (No subject)

basically keywords and, or link more then 1 if statment to produce an overall result.

such as
 code: 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.
Tony's programming blog. DWITE - a programming contest.
Tubs

Posted: Wed Feb 26, 2003 9:19 am   Post subject: (No subject)

so the and / or modifiers do the same thing
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: