当RHS == Integer.MAX_VALUE时,整数的布尔比较,为什么这个循环终止? [重复]

时间:2023-01-14 11:59:32

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to work out why this loop terminates...

我试图解决为什么这个循环终止...

@Test
public void test() {

    int counter=0;
    int from = 0;
    int until = Integer.MAX_VALUE;

    while(counter <= until) {
        counter++;
        if(counter < from) {
            System.out.println("continuing " + counter + " <= " + from);
            continue;
        } 
    }
    System.out.println("finished " + counter);
}

while(counter <= until) should always resolve to true because the counter cannot be increased beyond Integer.MAX_VALUE. Thus, the loop should not terminate.

while(counter <= until)应始终解析为true,因为计数器不能超过Integer.MAX_VALUE。因此,循环不应该终止。

However, in Eclipse, if I run with the JUnit runner I get:

但是,在Eclipse中,如果我使用JUnit运行器运行,我得到:

finished 108772

If I run in the debugger I get:

如果我在调试器中运行,我得到:

finished 125156

The output in the if(counter < from) is never output. If I remove that block the code still terminates, this time at Integer.MAX_VALUE.

if(counter )中的输出永远不会输出。如果我删除该块代码仍然终止,这次是在integer.max_value。

finished 2147483647

2 个解决方案

#1


0  

not really sure what you are trying to do here your while loop will work as expected but the from will never change so the "counter less than from" wont ever be executed?

不确定你在这里尝试做什么你的while循环将按预期工作但是from将永远不会改变所以“counter to than”永远不会被执行?

until (max int) will be = 2147483647

直到(max int)将= 2147483647

if you change the while loop to (counter < until) you get the output text of "finished 2147483647"

如果您将while循环更改为(counter ),则会得到“finished>

that help?

#2


0  

I can't reproduce your output so this is not an answer, just an observation. Consider just these lines.

我不能重现你的输出,所以这不是一个答案,只是一个观察。考虑这些线。

int counter=0;
int until = Integer.MAX_VALUE;
while(counter <= until) {
    counter++;

The while condition is 'less than or EQUAL'. So when counter is equal to Integer.MAX_VALUE one is added to it. This produces the largest possible Java negative number. Keep adding one to this value and it will eventually become zero, then count up once again until it reaches Integer.MAX_VALUE. Then the whole sequence starts over again. It looks like an infinite loop to me.

while条件是'小于或等于'。因此,当counter等于Integer.MAX_VALUE时,会向其添加一个。这产生了尽可能大的Java负数。继续向此值添加一个,它最终将变为零,然后再次计数,直到达到Integer.MAX_VALUE。然后整个序列重新开始。对我来说,它看起来像一个无限循环。

You would also execute the 'continuing' line on each iteration while counting up from the largest negative number.

您还可以在每次迭代时执行“继续”行,同时从最大负数开始计数。

#1


0  

not really sure what you are trying to do here your while loop will work as expected but the from will never change so the "counter less than from" wont ever be executed?

不确定你在这里尝试做什么你的while循环将按预期工作但是from将永远不会改变所以“counter to than”永远不会被执行?

until (max int) will be = 2147483647

直到(max int)将= 2147483647

if you change the while loop to (counter < until) you get the output text of "finished 2147483647"

如果您将while循环更改为(counter ),则会得到“finished>

that help?

#2


0  

I can't reproduce your output so this is not an answer, just an observation. Consider just these lines.

我不能重现你的输出,所以这不是一个答案,只是一个观察。考虑这些线。

int counter=0;
int until = Integer.MAX_VALUE;
while(counter <= until) {
    counter++;

The while condition is 'less than or EQUAL'. So when counter is equal to Integer.MAX_VALUE one is added to it. This produces the largest possible Java negative number. Keep adding one to this value and it will eventually become zero, then count up once again until it reaches Integer.MAX_VALUE. Then the whole sequence starts over again. It looks like an infinite loop to me.

while条件是'小于或等于'。因此,当counter等于Integer.MAX_VALUE时,会向其添加一个。这产生了尽可能大的Java负数。继续向此值添加一个,它最终将变为零,然后再次计数,直到达到Integer.MAX_VALUE。然后整个序列重新开始。对我来说,它看起来像一个无限循环。

You would also execute the 'continuing' line on each iteration while counting up from the largest negative number.

您还可以在每次迭代时执行“继续”行,同时从最大负数开始计数。