How does this While loop exit ? (java) -
i tried figure out how program works, , got stuck @ while loop, don't understand how second loop exit, since v
never end equal 0 or negative. since it's conditions exit loop, or missing deeper? code converts integers (>0) binary.
public class binary { public static void main(string[] args) { // read in command-line argument int n = integer.parseint(args[0]); // set v largest power of 2 <= n int v = 1; while (v <= n/2) { v = v * 2; } // check presence of powers of 2 in n, largest smallest while (v > 0) { // v not present in n if (n < v) { system.out.print(0); } // v present in n, remove v n else { system.out.print(1); n = n - v; } // next smallest power of 2 v = v / 2; } system.out.println(); } }
v int, , in java 1/2 int gives 0. loop goes through powers of 2 reach one, , 0.
run in debugger see!
Comments
Post a Comment