-7

Does this for loop ever stop?

for(int i=1; 1/i > 0; i++) {

}

If so, when and why? I was told that it stops, but I was given no reason for that.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Doesn't it stop immediately? `1/2` is `0`, so the loop should only go through a single iteration, no? – ruakh Sep 27 '16 at 05:25
  • 7
    Did you run it to find out? – Dawood ibn Kareem Sep 27 '16 at 05:25
  • 3
    Pretty funny if you compare the response the community gave to this question versus this one from earlier this year: http://stackoverflow.com/questions/37827073/does-this-for-loop-stop-and-why-why-not-for-var-i-0-1-i-0-i – Zarwan Sep 27 '16 at 05:28
  • @Zar, yes, the earlier was also mine. I wanted to understand the difference between Javascript behavior and Java's. I don't code in Java – Max Koretskyi Sep 27 '16 at 05:29
  • Makes sense, but I was commenting on the fact that this question is getting a lot of downvotes while your other version was very well received. The community is weird. – Zarwan Sep 27 '16 at 05:30
  • 2
    Zar, I downvoted both questions. It would take 10s to type this into an IDE and run it. It would take only slightly longer to look up the Java Language Specification and find out how Java divides integers. It's absolutely ridiculous to post something like this on Stack Overflow, in any language. At the end of the day, the best way to get an answer to the question "what happens when I run this Java (or JavaScript) program" is to run the program. – Dawood ibn Kareem Sep 27 '16 at 05:33
  • @Zar, indeed. thanks for you answer – Max Koretskyi Sep 27 '16 at 05:34
  • 2
    @DavidWallace, I suppose the execution environment also explains you why the loop stopped or why it didn't? :) – Max Koretskyi Sep 27 '16 at 05:35
  • Not in Java, no. Possibly in JavaScript - I'm not an expert on JavaScript. – Dawood ibn Kareem Sep 27 '16 at 05:38

6 Answers6

4

Yes.
First iteration: i = 1, 1/1 = 1 > 0, so it loops.
Second iteration: i = 2, 1/2 = 0 !> 0 (integer division), so it stops.

Zarwan
  • 5,537
  • 4
  • 30
  • 48
  • 1
    With integer division the result is truncated (i.e. the decimal numbers are just dropped). 1/2 = 0.5, so after truncating you're left with 0. – Zarwan Sep 27 '16 at 05:29
  • 1
    More than truncating, it's just integer math. 1/2 is 0 with a remainder of 1. – Codebender Sep 27 '16 at 05:35
  • @Zar, If I changed the loop to `for(double i=1; 1/i > 0; i++) {}` would I get the same behavior as in JavaScript, i.e. the loop would never stop? – Max Koretskyi Sep 27 '16 at 05:36
  • In theory yes, because dividing 1 by a non-zero number won't result in zero (Unless your number becomes small enough that it can't be stored in a double and goes to zero, but I'm not sure if that is actually what happens when you get too small. I haven't tried it). – Zarwan Sep 27 '16 at 05:39
  • @Zar, I see, ok, thanks. Possibly the behavior is similar to JavaScript here. – Max Koretskyi Sep 27 '16 at 05:41
  • 2
    The loop with the double won't stop. Eventually, doubles get so big that `++` doesn't change them. – Dawood ibn Kareem Sep 27 '16 at 05:41
  • @DavidWallace, what if I changed the first value for the `i` to 0. What would `1/i > 0` evaluated to ? – Max Koretskyi Oct 12 '16 at 05:32
  • 1/0 will throw an exception. You can't divide by zero. If you have more questions you should make a new post at this point. @Maximus – Zarwan Oct 12 '16 at 05:35
  • @Maximus Try it yourself. The best way to find out what happens when you run a particular piece of code is ... run the piece of code. – Dawood ibn Kareem Oct 12 '16 at 07:52
  • @DavidWallace, yeah, thanks. I just don't program in Java – Max Koretskyi Oct 12 '16 at 07:53
2
  1. First iteration: i =1, 1/1 equals 1 so continue.
  2. Second iteration: i = 2 , 1/2 equals 0 as it is integer division, so condition is false and it stops.

Details about integer division are documented in javadoc

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

Yes the for loop stops after one iteration. Dividing 1 by 2 in the second iteration will cause the for loop to stop because intergers don't record decimals meaning one divided by two will be 0 rather than .5.

Boo89100
  • 317
  • 2
  • 9
0

1/2 is evaluated as an integer, and simply chops the decimal point. Hence 1/2 = 0.

If one of those numbers was a floating point (ex. 1.0/2 or 1/2.0), it would return the expected 0.5.

Joe C
  • 15,324
  • 8
  • 38
  • 50
0

in this code

for(int i=1; 1/i > 0; i++) {
//any code here
}

in your code

  1. set the condition the number larger than 0
  2. the counter start from 1 (integer number)
  3. first run for loop cheek condition 1/1 > 0 => true the code execute.
  4. after first run the counter increment by 1 .
  5. after that the for loop cheek condition 1/2 > 0 => false the not execute and for loop stop

why the for loop stop in 1/2 because 1 integer and 2 integer
and integer / integer must = integer 1/2 = 0 (0.5 not integer) if you use this code the for loop many many executed . because int/float = float 1/1.0 =1 / 1/2.0= 0.5 / 1/3.0 = 0.33333 / 1/4.0 = 0.25 ...ect

for(float i=1; 1/i > 0; i++) {
    //any code here
    }
-1

this is my first time posting and I am definitely a novice so if I provide incorrect information someone please correct me :).

My understanding is that because i and 1 are both of the type integer in the line:

for(int i=1; 1/i > 0; i++)

then your program will automatically perform integer division/arithmetic which results in the modulus or remainder being dropped. Therefore during the second iteration the remainder of 0.5 will be dropped resulting in 0 and thus termination of the loop.

msojurn
  • 1
  • 1