How many times this loop will execute? Explain pls | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

24th Mar 2017, 10:37 AM
illia
1 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
24th Mar 2017, 11:25 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar