How many times this loop will execute? Explain pls | SoloLearn: Learn to code for FREE!

0

How many times this loop will execute? Explain pls

int p =2; for (int q = 2; q<32;q=q*2) { while (p<q) { p=p*2; } q=p-q; }

3/24/2017 10:37:23 AM

illia

1 Answer

New Answer

+7

I don't completely understand what this code actually does. Anyways, here is it's execution. BEGIN Iteration 1 : q = 2-2 = 0 & q = 0*2 = 0 Iteration 2 : q = 2-0 = 2 & q = 2*2 = 4 Iteration 3 : p = 2*2 = 4 & q = 4-4 = 0 & q = 0*2 = 0 Iteration 4 : q = 4 & q = 8 Iteration 5 : p = 8 & q = 0 & q = 0 Iteration 6 : q = 8 & q = 16 Iteration 7 : p = 16 & q = 0 & q = 0 Iteration 8 : q = 16 & q = 32 END