简体   繁体   中英

While loop condition in java

I am working my way through "Java - A beginners guide by Herbert Schildt". I hope this question is not way too ridiculous. It is about a while loop condition, where the while loop is located inside a for loop. The code example is this one:

public static void main(String[] args) {

    int e;
    int result;

    for (int i = 0; i < 10; i++) {

        result = 1;
        e = i;

        while (e > 0) {

            result *= 2;
            e--;    
        }
        System.out.println("2 to the " + i + " power is " + result);    
    }
}

I don't understand the decrementing of e, within the scope that is written after while (e > 0) . Since e = i, and i is incremented, i believe e should be larger than 0 after the for loop has been performed the first time. If e is not decremented, the program will not run properly. Why is decrementing e necessary in this example?

If you do not do e-- you will get stuck in an endless while loop .

Look at it like this:

Let's go into a random iteration of the for loop.

Let's say i=5 .

Then e=i so e=5 .

We now jump into the while loop.

while (e >0) {
    result*=2;
    e--;
}

If we did NOT do e-- , e would always stay at 5 and the while loop would never terminate.

By doing e-- , e will start at 5, then 4, .... then 0 where the while loop will terminate and we will jump back into the for loop, raise i (and consequently e ) and repeat the whole process.

What they're doing there is for each iteration of i, they're multiplying the result by 2, i times.

The walkthrough would be

i = 0, e = 0, result not multiplied, output 2 to the power of 0 is 1

i = 1, e = 1, result multiplied one time by 2, 2 to the power of 1 is 2

etc

They're decrementing e there to "count i backwards" down to 0. E is reset each time, and will always enter the while loop on iterations after the first one, and will always exit the while loop once it counts down to zero.

Simple word explanation :

In while loop you need such a statement because of which the while condition becomes false and program control comes out of the while loop and it will help to avoid endless lopping problem.

You have asked why decrementing e is important in this program?
Answer : If you don't decrement e then while(e > 0) will always return true value and program will get into an endless loop(will always be in while loop).

Sample Iteration 2 when i = 2:
result = 1
e = i = 2
while(e > 0) evaluates to true
so result = 1 * 2 = 2
e-- : e = 1

Again while(e > 0) evaluates to true because e=1 > 0
result = 2 * 2 = 4
e-- : e = 0

Now Again check while(e > 0) and it will evaluates to false and while loop ends.

For more information with few samples refer below link : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

While loops perform their task until the given condition is false. Assuming e starts off as greater than 0, not decrementing it will cause it to loop forever.

In terms of the program, it decrements e in order to calculate 2 n , which is accomplished by doubling 2 n times.

If e were not decremented, once the for loop had advanced beyond i=0 , the while loop would be executed forever. This is because without changing the value of e, e will always be greater than 0. Thus, e must be decremented in order for the exit condition to be met.

Lets consider a particular instance of the for loop and say that e = 5 in that instance (this will happen when i = 5). So, we are running the while loop till e is more than 0, which is 5 times. Each of this 5 times, we are multiplying the result(which is 1 initially), by 2 and updating it. So by the end of 5 times running of the while loop, we would have done 2^5 and then we print that 2 to the power i(5) is so much. Now, this logic is implemented for each value of the for loop.

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