简体   繁体   中英

Variable changing within a while loop

I'm trying to make a program that calculates PI using the following algorithm:

PI = 4 x (1 - 1/3 + 1/5 - 1/7 + 1/9 ....) etc.

I don't understand how the operator placement is affecting the variable.

The outcome produced on the first iteration of the loop makes sense, but then it seems to reset to initialised value and ignore the assignment operators the second time through. Repeating this outcome 1, then outcome 2, then outcome 1, outcome 2, and so forth...

I tried looking in this book called "Java: How to Program (Early Objects), 11th Edition" and in chapter four they went over operators but I could see that they didn't cover the conditions within a loop.

   double k = 1.0, j = 1.0;

   double sum = 0, PI = 0;

   while((Math.abs(PI-Math.PI)>0.000001)){
       sum += k/j;

       j = j + 2;

       k=-k;

       PI = 4 * sum;    

       System.out.println(k);
    }   

I changed the conditions of the while loop to run 4 times and print k. I expected the first printing of the variable 'k' to be -1.0. It is, but the second printing of k (second looping of while loop) is 1.0. The third is -1.0, then 4th is 1.0, and so forth...

I don't understand why it isn't -1.0 on all iterations, because with java assignment operators, as far as I know, if the left operator is '=' and the right operator is an incremental or decremental symbol, then the result should always be that the variable k will always = -k.

You initialize k outside the loop, so that only happens once. The initial value is 1 .

During each iteration you negate k :

k=-k;
  • During the first iteration 1 is negated to become -1 .
  • During the second iteration -1 is negated to become 1 .
  • During the third iteration 1 is negated to become -1 .
  • and so on

as far as I know, if the left operator is '=' and the right operator is an incremental or decremental symbol, then the result should always be that the variable k will always = -k.

I think you are confusing the operation -= with the operation =-

a -= b perform ab and store the result in a

a = -b perform -b (change sign) and then store -b into a

That is what that k = -k does. Take the value of k , change its sign and store into k . That is equivalent to say that change the sign of k .

Before loop

k = 1

  • loop #1 k = -(1.0) : so now k = -1.0 , it's carried -1.0 over to the next loop.

  • loop #2 k = -(-1.0) : so now k = 1.0 , it's carried 1.0 over to the next loop.

  • loop #3 k = -(1.0) : so now k = -1.0 , it's carried -1.0 over to the next loop.

  • and so on

k is changing every loop, just like how j is not 3.0 every loop.

The incremental and decremental symbol you're talking about is probably k-- and k++ , or maybe k-=k and k+=k . I'm not sure.

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