java - this while loop wont give me back the same as the for loop -


Can someone help me why loop does not have the same loop when doing the same?

  public square hallowld {public static zero main (string [] args) {System.out.println ("Hello World"); Int bits = 0; Int data length = 8; Println (dataLength); For / * (int i = (int) (Math.pow (2, numCheckBits) -1); i & lt; datalength + bits; i ++) {numCheckBits = i; } * / While ((Math.pow (2, bits) - 1) & lt; datalength + bits) ++; Println (bits); }}    

 for  (int i = (int) (Math. pow) (2, numCheckBits) -1); I & lt; Data lang + bits; i ++) {numCheckBits = i;   

When the loop starts, i 2 is set to numCheckBits -1 Then again, every time Through the loop, this has increased by 1. Thus, if the value starts as numCheckBits 0, i , then the value will take to 1, 2, 3. .., data length + bit - 1 . Inside the loop, the numCheckBits resizing is not effect, because expression is calculated only at the beginning of the loop using numCheckBits .

  while ((Math.pow (2, bits) - 1) & lt; datalength + bits) ++;   

In this case, the expression related to Math.pow is evaluated each time. This means that something like on bits 0, 1, ... value (log 2 (data-bound + bits) ) or on values Takes . Second expression in the form of Math.pow in the loop

for loop which will be equal to your So that the value will be tested against dataLength + Bits each time:

 for  (int i = 0; Math.pow (2, I) -1 And lt; datalength + i; i ++) {}   

[or whatever you want to do inside the loop - you numCheckBits any more].

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -