0

How many times is the body of the following loop executed? What is the value of x after the loop termination?

int x = 15; do { x −= 2; if (x < 5) { break; } } while (true);

24th Jun 2021, 3:00 AM
Rao Muzaffar
Rao Muzaffar - avatar
5 Respostas
0
Rao Muzaffar Yes. 5 times. edited: It works for 6 times.
24th Jun 2021, 3:18 AM
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜ - avatar
+ 1
🅰đŸ…č 🅐🅝🅐🅝🅣 Yes. It works for 6 times. But I typed '2nd' for two times .
24th Jun 2021, 5:43 AM
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜ - avatar
0
Rao Muzaffar x=15 1st round x-=2//x=13 In if statement: x<5= 13<5= false//so loop is not terminate. 2nd round x-=2//x=11 In if statement: x<5= 11<5= false//so loop is not terminate. 3rd round x-=2//x=9 In if statement: x<5= 9<5= false//so loop is not terminate. 4th round x-=2//x=7 In if statement: x<5= 7<5= false//so loop is not terminate. 5th round: x-=2//x=5 In if statement: x<5= 5<5= false//so loop is not terminate. 6th round: x-=2//x=3 In if statement: x<5= 3<5= true//so loop is terminated
24th Jun 2021, 3:11 AM
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜ - avatar
0
So loop will execute 5 times??
24th Jun 2021, 3:14 AM
Rao Muzaffar
Rao Muzaffar - avatar
0
˜”*°‱.˜”*°‱ Mohan 333 ‱°*”˜.‱°*”˜ Check again that is 6 times. On 6th time loop is breaking. Rao Muzaffar did you verify before giving best answer mark?
24th Jun 2021, 4:53 AM
AÍąJ
AÍąJ - avatar