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

Username:   Password: 
 RegisterRegister   
 if statement default case
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
karseet




PostPosted: Thu Jan 19, 2006 5:57 pm   Post subject: if statement default case

Hey guys,
For my program I am making I have an if statement to which a person selects something.
Eg
if selection="a" then blalblablabla
elsif selection="b" then blablablabla
etc... you get the point.

My question is, I have 5 eslifs and I want a 6th one to react if the selection isn't "a","b","c","d" or "e"

I tried :
if selection not = "a" or "b" or "c" or "d" or "e"
then blablablabla,
end if
but that doesn't seem to work, I get an error about boolean. Is there a non boolean alternative to what I am trying to accomplish?

In addition, I cannot put an "elsif" in the bottom of the code because then it skips all the code above it, and in my situation, that's not what I want. The solution needs to be placed before all the elsifs, such as what I mentioned before with the boolean.

Thanks for the taking the time to read, Very Happy
Joe

Martin says: Title fix, no worries. Smile
Sponsor
Sponsor
Sponsor
sponsor
Drakain Zeil




PostPosted: Thu Jan 19, 2006 6:02 pm   Post subject: (No subject)

You should use case instead, it's much better for you're purpose.

Also, the problem you have is that each OR statement seperates a boolean value, so...

statment = "a" may result true or false, but simply "b" or "c" or "d" (etc) will not, as they are not being evaluated. This is what boolean is for, is 1>2? No? Okay, false, is 1=2? No? Okay, false, is ((1 =1) and (2>1))? Yes? Excelent, true.

Anyway, as I said, use a case structure instead.
Delos




PostPosted: Thu Jan 19, 2006 6:46 pm   Post subject: Re: Help with a minor thing

I agree with Drakain Zell on this one. If you have that many options for a single event then stick them in a case.

code:

case selection of
   label "a", "bee", "sea":
      put "Letter!"
   label "1", "too", "E" :
      put "Number"
   label :
      put "None of the above"
end case


Also:
karseet wrote:

if selection not = "a" or "b" or "c" or "d" or "e"
then blablablabla,
end if


That's not quite how if selection would work in this case. Right there you've tried to OR 5 elements - which is some complicated maths alright.

code:

if selection not= "a" or selection not="b" or...then
...
end if


Is what you were trying to do. Remember, the computer will not assume anything. You must be very explicit in what you want it to do. (In this case telling it that each comparision was using the same variable).
GoVikingsGo




PostPosted: Thu Jan 19, 2006 8:01 pm   Post subject: (No subject)

u know guys i think he means if the booleans arent true is still wants something to happen in that case use else

code:

if selection="a" then
put "blalblablabla "
elsif selection="b" then
put "blablablabla "
else
put "if not true this will happen"
end if


That is of course if I understand ur statement
Rasta Fella




PostPosted: Thu Jan 19, 2006 8:12 pm   Post subject: (No subject)

GoVikingsGo that's what I was going to reply with but I don't think that's what he wants to do. But lets say you have 6 statements; 1=if, 2,3,4,5=elsif, 6=else. The last one should be else.
GoVikingsGo




PostPosted: Thu Jan 19, 2006 8:14 pm   Post subject: (No subject)

point being thats what i just said

ITS BAD PROGRAMMING STYLE NOT TO HAVE AN ELSE AT THE END OF A IF STRUCTURE!
Drakain Zeil




PostPosted: Thu Jan 19, 2006 8:27 pm   Post subject: (No subject)

GoVikingsGo wrote:
point being thats what i just said

ITS BAD PROGRAMMING STYLE NOT TO HAVE AN ELSE AT THE END OF A IF STRUCTURE!
I don't see how... nor have I seen this style used in any of the books I've read.
Delos




PostPosted: Thu Jan 19, 2006 11:17 pm   Post subject: (No subject)

GoVikingsGo wrote:
point being thats what i just said

ITS BAD PROGRAMMING STYLE NOT TO HAVE AN ELSE AT THE END OF A IF STRUCTURE!


For someone who doesn't indent, that's a lot to say Wink .
In some cases yes, this holds. In others, no. It's very difficult to define 'correct' programming style. Unless you're talking to wtd. He always has correct style. No questions asked.
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Fri Jan 20, 2006 9:12 am   Post subject: Re: if statement default case

karseet wrote:
I tried :
if selection not = "a" or "b" or "c" or "d" or "e"
then blablablabla,
end if
but that doesn't seem to work, I get an error about boolean. Is there a non boolean alternative to what I am trying to accomplish?


The problem here is how a programming language seperates an if statement. In English, your above makes perfect sense. However, to the computer, not so.

An if statement is in the following form:
code:
if (expression) then
 ...
end if


The computer evaluates the expression, and if the expression evaluates to true the code inside the if statement is run. If the expression evaluates to false, the code inside isn't run.

For example, in the following:

code:
if selection = "a" then


is the same as writing

code:
if (selection = "a") = true then


Now, a statement can also be a bunch of miniature expressions, connected using and and or statements.

for example, if expr1 and expr2 then runs if both expr1 and expr2 are true. if expr1 or expr2 then runs if expr1 or expr2 are true.

So here's the problem with your code:
code:
if selection not="a" or "b" or "c" or "d" or "e" then


has the program checking the following:
does (selection not="a") = true?
does "b" = true?
does "c" = true?
does "d" = true?
does "e" = true?

And then the block will execute if any one of the above statements is true (since you use or statements). But the thing is, the computer doesn't know what you mean when you ask 'does "b" = true?'. So to correct your statement, you would have to do:

if selection not= "a" or selection not= "b" or selection not="c" or selection not="d" or selection not="e" then ...

Cool?
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 1  [ 9 Posts ]
Jump to:   


Style:  
Search: