Results 1 to 10 of 10
Thread: help explaining small loop
- 02-12-2012, 07:26 PM #1
Noob
- Join Date
- Jul 2011
- Location
- M-I-yayo
- Posts
- 11
- Rep Power
- 0
help explaining small loop
public static void main (String[] args)
{
int num = 1, max = 20;
while (num < max)
{
if (num%2 == 0 )
System.out.println (num);
num++;
}
}
this is the output when i run it :
2
4
6
8
10
12
14
16
18
how is the output in a +2 pattern?
also, I'm a little confused by (num%2 == 0 ) loop. To start off, it would read ( 1%2 == 0 ). How is the inner loop executed considering the math? 1 / 2 = 0.5 . Isn't the decimal portion(.5) the remainder and thus .5 =/= 0??
Last edited by derb2k2; 02-12-2012 at 07:29 PM.
- 02-12-2012, 08:08 PM #2
Re: help explaining small loop
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-12-2012, 11:53 PM #3
Noob
- Join Date
- Jul 2011
- Location
- M-I-yayo
- Posts
- 11
- Rep Power
- 0
Re: help explaining small loop
- 02-13-2012, 06:40 AM #4
Re: help explaining small loop
So, everything clear now?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-13-2012, 06:07 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
Re: help explaining small loop
The result of the output comes from your if statement, you are only printing numbers where the num variable % 2 == 0.
The % sign represents modulus division, which returns the REMAINDER of the division problem (essentially, you do division to the nearest whole number, and you return the remainder). So, 3 % 5 would return 2, because 3 / 5 in integer division returns 1, and there is a remainder of 2.
In this case, you are doing modulus division by two in order to determine if the number is a divisor of 2, in which case the remainder would be 0. Therefore, any number that is divisible evenly by two is printed, and the other numbers are not.
I hope that helped.
- 02-13-2012, 06:11 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Re: help explaining small loop
It's not a loop.
It's an 'if' statement.
Exactly, which in an 'if' statement would mean...?
- 02-14-2012, 12:55 AM #7
Noob
- Join Date
- Jul 2011
- Location
- M-I-yayo
- Posts
- 11
- Rep Power
- 0
Re: help explaining small loop
thank you Xero and Tolls. I'm looking into it now as I've been super busy.
- 02-14-2012, 01:09 AM #8
Noob
- Join Date
- Jul 2011
- Location
- M-I-yayo
- Posts
- 11
- Rep Power
- 0
Re: help explaining small loop
ahah. Man, I'm so dumb. I see it now. I made a mistake in reading the code if that's possible with this small fragment of code.
if (num%2 == 0 )
System.out.println (num);
num++;
question: the "num++" operation is executed regardless of the (num%2 == 0) condition right?
thank you guys for the help
- 02-14-2012, 03:52 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: help explaining small loop
Yes the increment occurs whether or not the print took place.
For this reason many people (ie me and all others I consider to be right thinking) use braces: even with one line blocks.
Java Code:if(num % 2 == 0) { System.out.println(num); } num++;
- 02-14-2012, 01:31 PM #10
Noob
- Join Date
- Jul 2011
- Location
- M-I-yayo
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Can someone go into depth in explaining this code?
By coding in forum New To JavaReplies: 15Last Post: 03-08-2011, 07:03 PM -
Explaining Exceptions
By sunde887 in forum New To JavaReplies: 5Last Post: 01-22-2011, 08:00 AM -
Help explaining the difference.
By Romally in forum New To JavaReplies: 1Last Post: 11-14-2010, 02:40 PM -
Constructors, Setter & Getter << need some explaining
By A.M.S in forum New To JavaReplies: 4Last Post: 01-01-2009, 12:03 PM -
A small Question
By Eku in forum JDBCReplies: 7Last Post: 09-01-2008, 06:10 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks