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

Username:   Password: 
 RegisterRegister   
 Help for if statements
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
storm2713687




PostPosted: Sat Mar 02, 2013 9:42 pm   Post subject: Re: RE:Help for if statements

Tony @ Sat Mar 02, 2013 9:36 pm wrote:
storm2713687 @ Sat Mar 02, 2013 9:25 pm wrote:

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:"
Quote:

if trueFalseExpn then
statementsAndDeclarations
{ elsif trueFalseExpn then
statementsAndDeclarations }
[ else
statementsAndDeclarations ]
end if

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 Razz
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Mar 02, 2013 10:02 pm   Post subject: 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




PostPosted: Sat Mar 02, 2013 10:06 pm   Post subject: Re: RE:Help for if statements

Raknarg @ Sat Mar 02, 2013 10:02 pm wrote:
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




PostPosted: Sat Mar 02, 2013 10:12 pm   Post subject: Re: Help for if statements

Turing:

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




PostPosted: Sun Mar 03, 2013 8:39 pm   Post subject: Re: Help for if statements

Raknarg @ Sat Mar 02, 2013 10:12 pm wrote:
Turing:

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 Razz
Dreadnought




PostPosted: Sun Mar 03, 2013 9:12 pm   Post subject: 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




PostPosted: Mon Mar 04, 2013 12:46 pm   Post subject: 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




PostPosted: Mon Mar 04, 2013 8:12 pm   Post subject: Re: Help for if statements

I'm confused Sad
So is mod supposed to be division?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Mar 04, 2013 8:21 pm   Post subject: 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




PostPosted: Tue Mar 05, 2013 11:49 am   Post subject: Re: RE:Help for if statements

Insectoid @ Mon Mar 04, 2013 8:21 pm wrote:
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




PostPosted: Tue Mar 05, 2013 1:22 pm   Post subject: 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




PostPosted: Tue Mar 05, 2013 1:24 pm   Post subject: Re: RE:Help for if statements

DemonWasp @ Tue Mar 05, 2013 1:22 pm wrote:
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 Razz
Is mod supposed to be some kinda simple math thing?
DemonWasp




PostPosted: Tue Mar 05, 2013 2:11 pm   Post subject: 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




PostPosted: Tue Mar 05, 2013 4:56 pm   Post subject: 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.
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 2 of 2  [ 29 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: