
-----------------------------------
Tony
Mon Mar 10, 2003 1:27 am

[tutorial] If - elsif - else statments
-----------------------------------
Some basics you cant program without - If statments

An if statment is a command that compares 2 (or more) values and outputs based on the result.

The only program you can make without an if stament is "Hello World" one. In the rest, you have some values to compare.


if tony = "cool" then
put "tony is cool"

elsif tony = "not cool" then
put "error, something wrong with your program"

else
put "something something, blah blah blah"

end if


this statment compares the value of variable tony to different values and if one of them matches, it outputs a result. If none are matched, it outputs the commands under else

You can have as many elsifs as you'd like. Just keep on adding them.

Also you dont have to have elsif or else. You can have both, or just 1 or none. You always need to start with an if though.

Someone once asked the difference between case and if and as far as I can see... If statments offer you much more control. You're not limited to just = comparisment. You can have much, much more :wink:


<
=
>
not=
and
or


you can use a combination of those to check for very spesific conditions.


if ((tony = "cool") or (dan = "cool")) and (numposts >= 1000) then
put "compsci has over 1000 posts and a cool admin"
end if


As you can see if first checks if ether tony or dan is cool. And if one of those is true, the overall statment is true. Then it checks if number of posts is more or equals to a 1000 and returns true/false for that. Now if both left and right side of "and" are true, then the overall statment is true.

confusing, isn't it? Thats why I used brakets to show each individual comparisment. You dont have to use that, but it just shows you how your if statment is structured and keeps trouble away when you have a bunch of "or" and "and" statments.

Hope this clears things up.

-----------------------------------
Namis
Thu Sep 11, 2003 2:55 pm

Im using Turing 4.0.4c Problem
-----------------------------------
this is my code, im new so bare with me :\

var one:string
var two:string
put " Yes or No? "
if one = "Yes" then
put " Good Choice! "
elsif No = "No" then
put " Bad Choice! "

Im getting an error in 

elsif No = "No" Then

Whats wrong? Thanks

-----------------------------------
krishon
Thu Sep 11, 2003 4:32 pm


-----------------------------------
maybe u shuld add an input line........

-----------------------------------
Tony
Thu Sep 11, 2003 4:39 pm


-----------------------------------
well first of all, you didnt declear "No"... I think you wanted to use variable named "one" instead since you used it in

if one = "Yes" then...

Now there's soo many things wrong with this program... I donno where to start.

Lets start with bigest and most obvious mistakes:

The variable you're using in your ifstatment has no value. You should promp to enter their choice and assign input to the variable

var choice:string
put "enter choice"
get choice
put choice


You should read up a tutorial on I/O (input/output for more details).

also variable named "one" doesnt make much sence. one is a number, so a line "if one = "Yes" then" doesnt make much sence. I know it would work ether way, but if you would name your variables appropriatly, it would be just easier for yourself to understand how your own code works.

Also you forgot to end your if statment.

Here's how your program should have looked like:

var choice:string
put "enter choice, yes/no"
get choice
if choice="yes" then
put "you have picked yes"
else
put "you did not pick yes"
end if


I hope that helped - and dont worry... You'll get better with practice and understanding of how code works - just read some tutorials we got here and look around the site  8)

-----------------------------------
krishon
Thu Sep 11, 2003 4:41 pm


-----------------------------------
lol, ye tony put everything in a nutshell  :)

-----------------------------------
theguru
Fri Oct 07, 2005 10:03 pm


-----------------------------------
:D this tutorial has everything you need to know about the if statement. i was wondering why turing has elsif instead of elseif. isn't that wierd. :?:  :?:

-----------------------------------
[Gandalf]
Fri Oct 07, 2005 10:27 pm


-----------------------------------
Quite a few programming languages have it like that actually.
I think they reason behind it is to make a better differentiation between 'else' and 'if' so that you know it does something else, and so that newbs don't mistake it for and 'else' preceding an 'if'.

-----------------------------------
wtd
Fri Oct 07, 2005 11:03 pm


-----------------------------------
"else if" is a way of having multiple clauses without actually having anything more complex than "if" and "else".

-----------------------------------
[Gandalf]
Sat Oct 08, 2005 4:40 pm


-----------------------------------
Yeah, I forgot about that.
It wouldn't work quite well in Turing though:
var word : string
get word
if word (1) = 'a' then
    put "yes"
else
    if word (1) = 'b' then
        put "no"
    else
        if word (1) = 'c' then
            put "this is even deeper.."
        end if
    end if
end if
:lol:

-----------------------------------
MysticVegeta
Tue Nov 08, 2005 8:19 pm

Re: [tutorial] If - elsif - else statments
-----------------------------------



elsif tony = "not cool" then
put "error, something wrong with your program"


 

oh my god LOL  :lol: really humorous!

-----------------------------------
xXInsanityXx
Mon Nov 28, 2005 8:39 pm


-----------------------------------
This is an excellent Tut, for the super newbs, and gets them into basic programming logic. helped me a lot as a beginer thanks a lot

-----------------------------------
Nose_Pilot44
Mon Nov 28, 2005 10:19 pm


-----------------------------------
keep getting an error. says "Syntax error at 'var' expected ':'" and it says "syntax error at then"
var num1
var num2

get num1
get num2

if num1 > num2 then
put "Num1 is greater"

elsif num2 > num1 then
put "Num2 is greater"

elsif num1 = num2 then
put "They're equal"

elsif then
put "error! omgwtfbbq!" 

end if

-----------------------------------
Mr. T
Mon Nov 28, 2005 11:34 pm

Alex's Opinion
-----------------------------------
var num1 : int
var num2 : int

get num1
get num2

if num1 > num2 then
    put "Num1 is greater"

elsif num2 > num1 then
    put "Num2 is greater"

elsif num1 = num2 then
    put "They're equal"

else
    put "error! omgwtfbbq!"

end if
1) You were declaring the variables incorrectly
2) else covers every other option that doesn't fall under the if or elsif conditions

-----------------------------------
DIIST
Mon Dec 19, 2005 7:43 am


-----------------------------------
'If's are nice but case statments become lot easier and faster when you are dealing for known values like yes and no or 1 and 0! 


var answer : string
loop
    put "Is compsci.ca cool?"
    get answer

    case answer of
        label "Yes", "Y", "yes", "y" :
            put "You are so right"
            exit
        label "No", "no", "n", "N" :
            put "Are you crazy?"
            exit
        label :
            put "You did not answer correctly!"
    end case
end loop


-----------------------------------
Cervantes
Mon Dec 19, 2005 5:35 pm


-----------------------------------
 yes and no or 1 and 0! 
yes = 1
no = 0?

How about:
yes = true
no = false

Cases are nice, but less flexible than if statements.

-----------------------------------
wtd
Tue Dec 20, 2005 12:14 am


-----------------------------------
'If's are nice but case statments become lot easier and faster when you are dealing for known values like yes and no or 1 and 0! 


var answer : string
loop
    put "Is compsci.ca cool?"
    get answer

    case answer of
        label "Yes", "Y", "yes", "y" :
            put "You are so right"
            exit
        label "No", "no", "n", "N" :
            put "Are you crazy?"
            exit
        label :
            put "You did not answer correctly!"
    end case
end loop


You're doing too much work.  Make the answer all upper or lowercase and you can cut the number of things you're looking for in half.  Plus, you don't have to think about things like "nO" or "yEs".

-----------------------------------

Mon May 29, 2006 8:47 pm


-----------------------------------
:D this tutorial has everything you need to know about the if statement. i was wondering why turing has elsif instead of elseif. isn't that wierd. :?:  :?:

Why not elif? =p

-----------------------------------
wtd
Mon May 29, 2006 9:08 pm


-----------------------------------
:D this tutorial has everything you need to know about the if statement. i was wondering why turing has elsif instead of elseif. isn't that wierd. :?:  :?:

Why not elif? =p

Because that is widely considered to be one of the less than wonderful quirks of Python syntax.  ;)

-----------------------------------
kay188
Wed Dec 06, 2006 7:19 pm

Re: Im using Turing 4.0.4c Problem
-----------------------------------
this is my code, im new so bare with me :\

var one:string
var two:string
put " Yes or No? "
if one = "Yes" then
put " Good Choice! "
elsif No = "No" then
put " Bad Choice! "

Im getting an error in 

elsif No = "No" Then

Whats wrong? Thanks

you're missing somethings in there
try this

var one : string
put " Yes or No? "
get one
if one = "Yes" then
    put " Good Choice! "
elsif one = "No" then
    put " Bad Choice! "
end if


-----------------------------------
tooring
Mon Dec 11, 2006 9:43 pm


-----------------------------------
learned some new stuff, thanks

-----------------------------------
Clayton
Mon Dec 11, 2006 9:52 pm


-----------------------------------
It's very nice that you learned some new stuff, but we generally discourage just posting to say "good stuff". Please refrain from doing so in the future. Other than that, welcome to CompSci!

-----------------------------------
[Gandalf]
Tue Dec 12, 2006 8:19 pm


-----------------------------------
It's very nice that you learned some new stuff, but we generally discourage just posting to say "good stuff". Please refrain from doing so in the future. Other than that, welcome to CompSci!
Nothing wrong with saying "thanks" once in a while...

There is, however, something wrong with being nitpicky with every thread, and replying for the sole purpose of critisizing a post that, really, didn't break any rules.  Just something to ponder about, no harm done.

-----------------------------------
Reira
Fri Nov 02, 2007 3:07 pm


-----------------------------------


var answer : string
loop
    put "Is compsci.ca cool?"
    get answer

    case answer of
        label "Yes", "Y", "yes", "y" :
            put "You are so right"
            exit
        label "No", "no", "n", "N" :
            put "Are you crazy?"
            exit %is this the only option?
        label :
            put "You did not answer correctly!"
    end case
end loop


When an unwanted value has been input, the "exit" command is usually input. But is it possible to continue the program after this happens, without exiting it, such as goto but without the need of inputting another loop? In general, is it possible to clear the value of a variable after it has been defined (through "get"), within an "if" statement?

-----------------------------------
CodeMonkey2000
Fri Nov 02, 2007 7:27 pm

RE:[tutorial] If - elsif - else statments
-----------------------------------
variable:=""%or what ever you like.

And you do realize that you are posting in thread that is nearly four years old?

-----------------------------------
Reira
Fri Nov 02, 2007 7:40 pm

RE:[tutorial] If - elsif - else statments
-----------------------------------
lol umm well it was on the first page...
Thanks for bringing it to my attention, though!
I'll watch better next time ^^

-----------------------------------
CodeMonkey2000
Fri Nov 02, 2007 7:51 pm

Re: [tutorial] If - elsif - else statments
-----------------------------------
Here is a better tutorial (sorry tony). It's nice and in-depth. 
http://compsci.ca/v3/viewtopic.php?t=14656
