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

Username:   Password: 
 RegisterRegister   
 [tutorial] Case
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Wed Jan 25, 2006 3:50 pm   Post subject: [tutorial] Case

a case is basically another form of an if. it takes a variable that youve gotten it and compares it to a set of possibilities for it to match ex.
code:

var a:int:=0
%pretend we have a numbered list with choices represented by numbers
get a
case a of:
%tells the program we want to compare our conditions to a
label 1:
%label represents our condition with 1 being the actual thing we want to compare it to
home
label 2:
play
label 3:
options
label 4:
instructions
label 5:
quit
end case
%we then end the case with the command end case stating that we are finished comparing our variable

in this way we can save enormous amounts of type and time typing out our conditions in our if statements. however, in a case , it only compares for something to be equal to it, so if you are having an if for say sorting, you cannot use an if because sorting usually needs to know > or < and as stated, you can only compare for equal to. for strings it is the same thing
code:

var a:string(1)
%assume we have given the user a choice of letters to press
getch(a)
case a of:
label "y":
continue
label "n":
quit
label "i":
instructions
label "b":
back
end case

so as you can see there is no difference in comparing strings and comparing integers other than the fact when comparing strings you need quotes around your conditions. that concludes my tutorial on cases. feel free to correct me if i am somehow wrong, i will take no offence hope it helps
Sponsor
Sponsor
Sponsor
sponsor
person




PostPosted: Wed Jan 25, 2006 5:32 pm   Post subject: (No subject)

1) there is no colon after "of"
2) please indent

ps: for a char format, u need single quotes just in case someone tries a char and it doesnt work with double quotes
Clayton




PostPosted: Thu Jan 26, 2006 12:19 am   Post subject: (No subject)

srry bout the colon and yes your right it is single quote thnx
kanri




PostPosted: Fri Jun 02, 2006 6:49 pm   Post subject: (No subject)

that was really really helpful
thank you!! Very Happy that helped me Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: