Computer Science Canada if statement default case |
Author: | karseet [ 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, ![]() Joe Martin says: Title fix, no worries. ![]() |
Author: | Drakain Zeil [ Thu Jan 19, 2006 6:02 pm ] |
Post 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. |
Author: | Delos [ 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.
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.
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). |
Author: | GoVikingsGo [ Thu Jan 19, 2006 8:01 pm ] | ||
Post subject: | |||
u know guys i think he means if the booleans arent true is still wants something to happen in that case use else
That is of course if I understand ur statement |
Author: | Rasta Fella [ Thu Jan 19, 2006 8:12 pm ] |
Post 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. |
Author: | GoVikingsGo [ Thu Jan 19, 2006 8:14 pm ] |
Post subject: | |
point being thats what i just said ITS BAD PROGRAMMING STYLE NOT TO HAVE AN ELSE AT THE END OF A IF STRUCTURE! |
Author: | Drakain Zeil [ Thu Jan 19, 2006 8:27 pm ] |
Post subject: | |
GoVikingsGo wrote: point being thats what i just said
I don't see how... nor have I seen this style used in any of the books I've read.ITS BAD PROGRAMMING STYLE NOT TO HAVE AN ELSE AT THE END OF A IF STRUCTURE! |
Author: | Delos [ Thu Jan 19, 2006 11:17 pm ] |
Post 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 ![]() 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. |
Author: | Martin [ 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:
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:
is the same as writing
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:
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? |