Computer Science Canada

[tutorial] If - elsif - else statments

Author:  Tony [ Mon Mar 10, 2003 1:27 am ]
Post subject:  [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.

code:

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.

code:

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.

Author:  Namis [ Thu Sep 11, 2003 2:55 pm ]
Post subject:  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

Author:  krishon [ Thu Sep 11, 2003 4:32 pm ]
Post subject: 

maybe u shuld add an input line........

Author:  Tony [ Thu Sep 11, 2003 4:39 pm ]
Post subject: 

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

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

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)

Author:  krishon [ Thu Sep 11, 2003 4:41 pm ]
Post subject: 

lol, ye tony put everything in a nutshell Smile

Author:  theguru [ Fri Oct 07, 2005 10:03 pm ]
Post subject: 

Very Happy 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. Question Question

Author:  [Gandalf] [ Fri Oct 07, 2005 10:27 pm ]
Post subject: 

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'.

Author:  wtd [ Fri Oct 07, 2005 11:03 pm ]
Post subject: 

"else if" is a way of having multiple clauses without actually having anything more complex than "if" and "else".

Author:  [Gandalf] [ Sat Oct 08, 2005 4:40 pm ]
Post subject: 

Yeah, I forgot about that.
It wouldn't work quite well in Turing though:
code:
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

Laughing

Author:  MysticVegeta [ Tue Nov 08, 2005 8:19 pm ]
Post subject:  Re: [tutorial] If - elsif - else statments

Tony wrote:

code:


elsif tony = "not cool" then
put "error, something wrong with your program"




oh my god LOL Laughing really humorous!

Author:  xXInsanityXx [ Mon Nov 28, 2005 8:39 pm ]
Post subject: 

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

Author:  Nose_Pilot44 [ Mon Nov 28, 2005 10:19 pm ]
Post subject: 

keep getting an error. says "Syntax error at 'var' expected ':'" and it says "syntax error at then"
code:
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

Author:  Mr. T [ Mon Nov 28, 2005 11:34 pm ]
Post subject:  Alex's Opinion

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

Author:  DIIST [ Mon Dec 19, 2005 7:43 am ]
Post subject: 

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


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

Author:  Cervantes [ Mon Dec 19, 2005 5:35 pm ]
Post subject: 

thuvs wrote:
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.

Author:  wtd [ Tue Dec 20, 2005 12:14 am ]
Post subject: 

thuvs wrote:
'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!
code:


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".

Author:  Anonymous [ Mon May 29, 2006 8:47 pm ]
Post subject: 

theguru wrote:
Very Happy 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. Question Question


Why not elif? =p

Author:  wtd [ Mon May 29, 2006 9:08 pm ]
Post subject: 

vahnx wrote:
theguru wrote:
Very Happy 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. Question Question


Why not elif? =p


Because that is widely considered to be one of the less than wonderful quirks of Python syntax. Wink

Author:  kay188 [ Wed Dec 06, 2006 7:19 pm ]
Post subject:  Re: Im using Turing 4.0.4c Problem

Namis wrote:
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

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

Author:  tooring [ Mon Dec 11, 2006 9:43 pm ]
Post subject: 

learned some new stuff, thanks

Author:  Clayton [ Mon Dec 11, 2006 9:52 pm ]
Post subject: 

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!

Author:  [Gandalf] [ Tue Dec 12, 2006 8:19 pm ]
Post subject: 

Freakman wrote:
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.

Author:  Reira [ Fri Nov 02, 2007 3:07 pm ]
Post subject: 

code:


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?

Author:  CodeMonkey2000 [ Fri Nov 02, 2007 7:27 pm ]
Post subject:  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?

Author:  Reira [ Fri Nov 02, 2007 7:40 pm ]
Post subject:  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 ^^

Author:  CodeMonkey2000 [ Fri Nov 02, 2007 7:51 pm ]
Post subject:  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


: