简体   繁体   中英

What is the reason behind this infinite loop?

I'm new to java, I wrote a code to practice loops, and here is it:

public class scratch {

    public static void main(String args[]) {

        int value = 0;
        boolean myBoolean = (value < 5);

        while(myBoolean) {

            System.out.println(value);
            value = value + 1;
        }
    }
}

When I run it, I get an infinite loop, but if i change the "while" argument like this:

public class scratch {

    public static void main(String args[]) {

        int value = 0;
        boolean myBoolean = (value < 5);

        while(value < 5) {

            System.out.println(value);
            value = value + 1;
        }
    }
}

it works and here is the output:

0
1
2
3
4

Can you please tell me what I'm missing here, thank you!

You must also reevaluate the boolean expression to set your value in the loop body for it to work, like

final int LIMIT = 5; // <-- try to avoid magic numbers.
boolean myBoolean = (value < LIMIT); // <-- assigns the result of the expression 
                                    //    `value < LIMIT` to `myBoolean`.
while(myBoolean) {
    System.out.println(value);
    value = value + 1; // <-- value++;
    myBoolean = (value < LIMIT);
}

without that last line updating myBoolean , when myBoolean is true it will always be true .

In your first example you only set "myBolean" once and never change that value again. That is why your loop runs endless.

In the second example you check the value < 5 for every run of your loop.

If you'd move that asignment into your loop it would work too.

public class scratch {

public static void main(String args[]) {
    int value = 0;
    boolean myBoolean = true;
    while(myBoolean) {
        System.out.println(value);
        value = value + 1;
        myBoolean = (value < 5);
    }
}

}

The reason is simple, in the first case

public class scratch {

    public static void main(String args[]) {

        int value = 0;
        boolean myBoolean = (value < 5);

        while(myBoolean) {

            System.out.println(value);
            value = value + 1;
        }
    }
}

You are setting boolean variable to be boolean myBoolean = (value < 5); which is true as value is initialized to zero. and you are not changing that later, so you end up in an infinite loop

While in the second case

public class scratch {

    public static void main(String args[]) {

        int value = 0;
        boolean myBoolean = (value < 5);

        while(value < 5) {

            System.out.println(value);
            value = value + 1;
        }
    }
}

you are increasing the value in the loop and comparing the value itself for the loop to proceed and hece when value becomes 5 the condition becomes false and the loop terminates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM