
-----------------------------------
storm2713687
Wed Feb 27, 2013 1:43 pm

Help for if statements
-----------------------------------
I was just wondering if I can use if statements like this (For an example):


var operation : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get operation
if 1 then
    put "1+1=", 1 + 1
elsif 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if



Would that work? I've only used booleans for my if statements so far, but I'd like to try this, but I'm not sure if it works (last time I remember doing something like that, it failed).

-----------------------------------
Tony
Wed Feb 27, 2013 5:28 pm

RE:Help for if statements
-----------------------------------
you could read the documentation -- [tdoc]if[/tdoc] -- to see exactly what you can do with the statement.

-----------------------------------
Insectoid
Wed Feb 27, 2013 7:39 pm

RE:Help for if statements
-----------------------------------
The nice thing about programming is that if you want to try something, you can.

-----------------------------------
storm2713687
Thu Feb 28, 2013 8:31 pm

Re: RE:Help for if statements
-----------------------------------
The nice thing about programming is that if you want to try something, you can.
I was at school so I couldn't use Turing xD
Since there's compsci at high schools, the computer probably had it, but I just didn't know the location of it. Thanks for helping though, guys!

-----------------------------------
jbking
Fri Mar 01, 2013 12:30 pm

Re: Help for if statements
-----------------------------------
[url=http://compsci.ca/holtsoft/doc/case.html]Case would be like what you want so there is a command to do that, if you want to know about it.

-----------------------------------
yazdmich
Fri Mar 01, 2013 12:55 pm

Re: Help for if statements
-----------------------------------


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing [int, real, nat, string etc] can be inferred, but everything else is declared)

-----------------------------------
storm2713687
Fri Mar 01, 2013 5:11 pm

Re: Help for if statements
-----------------------------------
Jbking, what do you mean by that?


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing 
Can you give an example plz? :)
I don't quite understand that :(

-----------------------------------
yazdmich
Fri Mar 01, 2013 5:39 pm

Re: Help for if statements
-----------------------------------
Jbking, what do you mean by that?


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing 
Can you give an example plz? :)
I don't quite understand that :(

I fixed your code so it would work in turing. declaring means to say what it is checking, in this case the value of the variable op

-----------------------------------
jbking
Fri Mar 01, 2013 6:17 pm

Re: Help for if statements
-----------------------------------
I mean that the structure of code you want where you just want to state the value and have it automatically do the line you want already exists as a separate command called "case" which you may want to research.  From the documentation that should look a lot like what you wanted to some degree if I interpret what you wanted correctly.  Here would be an example that is similar to what you wanted:


        case mark of
            label 9, 10 :   put "Excellent"
            label 7, 8 :        put "Good"
            label 6 :       put "Fair"
            label :     put "Poor"
        end case


-----------------------------------
storm2713687
Fri Mar 01, 2013 7:38 pm

Re: Help for if statements
-----------------------------------
Jbking, what do you mean by that?


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing 
Can you give an example plz? :)
I don't quite understand that :(

I fixed your code so it would work in turing. declaring means to say what it is checking, in this case the value of the variable op
Ohhh thanks :P
Is it possible to change else to elsif op = 3?

I mean that the structure of code you want where you just want to state the value and have it automatically do the line you want already exists as a separate command called "case" which you may want to research.  From the documentation that should look a lot like what you wanted to some degree if I interpret what you wanted correctly.  Here would be an example that is similar to what you wanted:


        case mark of
            label 9, 10 :   put "Excellent"
            label 7, 8 :        put "Good"
            label 6 :       put "Fair"
            label :     put "Poor"
        end case

I didn't get there in turing yet xD
Thanks for helping though :)

-----------------------------------
Raknarg
Sat Mar 02, 2013 1:11 pm

RE:Help for if statements
-----------------------------------
Personally I wouldn't use cases in turing, they're stupid. In most languages you can set ranges, but not in Turing. With bigger ranges you might as well use ifs.

-----------------------------------
storm2713687
Sat Mar 02, 2013 7:51 pm

Re: RE:Help for if statements
-----------------------------------
Personally I wouldn't use cases in turing, they're stupid. In most languages you can set ranges, but not in Turing. With bigger ranges you might as well use ifs.

Well now I know another thing not to use in turing :P


Btw, is it possible to have the if statement something like this:
if variable1 = yes then
       put "hi"
elsif variable1 = no then
       put "bye"
end if

-----------------------------------
Raknarg
Sat Mar 02, 2013 7:56 pm

RE:Help for if statements
-----------------------------------
Why not? It's essentially the same as what you were doing before.

-----------------------------------
storm2713687
Sat Mar 02, 2013 9:25 pm

Re: RE:Help for if statements
-----------------------------------
Why not? It's essentially the same as what you were doing before.
Well, I just wanted to have a bigger variety of options for my if statements. 
Now I have another question :P
Can I use an if statement within another if statement?

-----------------------------------
Tony
Sat Mar 02, 2013 9:36 pm

Re: RE:Help for if statements
-----------------------------------

Can I use an if statement within another if statement?
Yes. As I've linked to above ? http://compsci.ca/holtsoft/doc/if.html "An ifStatement is:"

if trueFalseExpn then
 	 	statementsAndDeclarations
 	{ elsif trueFalseExpn then
 	 	statementsAndDeclarations }
 	
So inside of an ifStatement you can have any statementsAndDeclarations, which includes other ifStatement.

-----------------------------------
storm2713687
Sat Mar 02, 2013 9:42 pm

Re: RE:Help for if statements
-----------------------------------

Can I use an if statement within another if statement?
Yes. As I've linked to above ? http://compsci.ca/holtsoft/doc/if.html "An ifStatement is:"

if trueFalseExpn then
 	 	statementsAndDeclarations
 	{ elsif trueFalseExpn then
 	 	statementsAndDeclarations }
 	
So inside of an ifStatement you can have any statementsAndDeclarations, which includes other ifStatement.

Oh whoops, idk what happended with me at that time... idk why I didn't look at it lol. Thanks though, that answers a hell lot :P

-----------------------------------
Raknarg
Sat Mar 02, 2013 10:02 pm

RE:Help for if statements
-----------------------------------
It's called nesting, which you can do with basically any structure. A loop within a loop, if within an if, for within a for, and such.

-----------------------------------
storm2713687
Sat Mar 02, 2013 10:06 pm

Re: RE:Help for if statements
-----------------------------------
It's called nesting, which you can do with basically any structure. A loop within a loop, if within an if, for within a for, and such.

Do I have to add a certain code to my program or will it work if I just use an if within an if?

-----------------------------------
Raknarg
Sat Mar 02, 2013 10:12 pm

Re: Help for if statements
-----------------------------------

var x : int := 14

if x mod 2 = 0 then
     if x >= 10 then
          put "This is an even number greater or equal to ten."
     else
          put "This is an even number less than ten."
     end if
else
     if x > 10 then
          put "This is an odd number greater than ten."
     else
          put "This is an odd number less than ten."
     end if
end if


try running that.

-----------------------------------
storm2713687
Sun Mar 03, 2013 8:39 pm

Re: Help for if statements
-----------------------------------

var x : int := 14

if x mod 2 = 0 then
     if x >= 10 then
          put "This is an even number greater or equal to ten."
     else
          put "This is an even number less than ten."
     end if
else
     if x > 10 then
          put "This is an odd number greater than ten."
     else
          put "This is an odd number less than ten."
     end if
end if


try running that.

Thanks for helping, but what does mod do after the "if" statement at the top? Sorry, I'm so bad at this :P

-----------------------------------
Dreadnought
Sun Mar 03, 2013 9:12 pm

Re: Help for if statements
-----------------------------------
It's just a positive remainder from integer division. Basically, "x mod 2" will return 1 if x is an odd number and 0 if x is an even number.

-----------------------------------
Raknarg
Mon Mar 04, 2013 12:46 pm

RE:Help for if statements
-----------------------------------
Or if I was looking at an angle, for instance, and wanted to have it only between 0 and 359. Then lets say I had an angle of 450. 

450 mod 360 = 90

Divides the left number by the right one, then returns whatever the remainder is.

-----------------------------------
storm2713687
Mon Mar 04, 2013 8:12 pm

Re: Help for if statements
-----------------------------------
I'm confused :(
So is mod supposed to be division?

-----------------------------------
Insectoid
Mon Mar 04, 2013 8:21 pm

RE:Help for if statements
-----------------------------------
Mod returns the remainder of a division. 10/3 = 3, with a remainder of 1. 10 mod 3 = 1. 

The way to check if a number is odd or even is to divide it by 2 and take the remainder. If the remainder is 0, then it's even. If the remainder is 1, then it's even. 5/2 = 2, with a remainder of 1. 5 mod 2 = 1, therefor 5 is odd. 4/2 = 2, with a remainder of 0. 4 mod 2 = 0.

-----------------------------------
storm2713687
Tue Mar 05, 2013 11:49 am

Re: RE:Help for if statements
-----------------------------------
Mod returns the remainder of a division. 10/3 = 3, with a remainder of 1. 10 mod 3 = 1. 

The way to check if a number is odd or even is to divide it by 2 and take the remainder. If the remainder is 0, then it's even. If the remainder is 1, then it's even. 5/2 = 2, with a remainder of 1. 5 mod 2 = 1, therefor 5 is odd. 4/2 = 2, with a remainder of 0. 4 mod 2 = 0.
I still don't really get it... isn't 10/3 supposed to be like 3.something?

-----------------------------------
DemonWasp
Tue Mar 05, 2013 1:22 pm

RE:Help for if statements
-----------------------------------
If you divide 10 solid objects (each indivisible) into 3 even piles, then each pile has 3 objects in it and you have one left over (the remainder).

10 div 3 will give you 3, the number of objects in each pile.
10 mod 3 will give you 1, the number of objects left over.

-----------------------------------
storm2713687
Tue Mar 05, 2013 1:24 pm

Re: RE:Help for if statements
-----------------------------------
If you divide 10 solid objects (each indivisible) into 3 even piles, then each pile has 3 objects in it and you have one left over (the remainder).

10 div 3 will give you 3, the number of objects in each pile.
10 mod 3 will give you 1, the number of objects left over.

Ohhh I get it now, thanks :P
Is mod supposed to be some kinda simple math thing?

-----------------------------------
DemonWasp
Tue Mar 05, 2013 2:11 pm

RE:Help for if statements
-----------------------------------
mod stands for "modulus", which basically means "divide evenly and find the remainder".

This leads to a kind of mathematics called "modular arithmetic". It sounds scary, but the hours in a day follow the same basic rules: hours start at 1, count up to 12, then return to 1 again. But! The next number after 12 is 13, and 13 mod 12 = 1. And, the hour after that, hour #14, is normally called "2pm", but notice that 14 mod 12 = 2. For more detail, see http://en.wikipedia.org/wiki/Modular_arithmetic . There's a discussion of modular arithmetic in a first-year university class, usually called "Classical Algebra"  or similar.

Note: Hours don't follow exactly the same system, because 12 mod 12 = 0 (you can divide 12 objects into 12 even piles with 0 left over). However, the idea is the same, just offset by one. The hour "12am midnight" can be thought of as 0, and the hour "1am" thought of as 1. Then "11am" is 11, and "12pm noon" is 0 (because 12 mod 12 = 0).

Note: Negative numbers may not behave as you expect with mod. For details, see the div mod and rem commands:
http://compsci.ca/holtsoft/doc/div.html
http://compsci.ca/holtsoft/doc/mod.html
http://compsci.ca/holtsoft/doc/rem.html

-----------------------------------
Raknarg
Tue Mar 05, 2013 4:56 pm

RE:Help for if statements
-----------------------------------
For instance, you can try this:

put -450 rem 360
put -450 mod 360

Notice how the first one results -90 and the second results 270.
