What will be the output of the following code snippet?var a = 1; var b = 0; while (a <= 3) { a++; b += a * 2; print(b); }*4 10 181 2 3None of Above1 4 7
Question
What will be the output of the following code snippet?
var a = 1;
var b = 0;
while (a <= 3) {
a++;
b += a * 2;
print(b);
}
- 4 10 181 2 3
- None of Above
- 1 4 7
Solution
The output of the given code snippet will be:
4 10 18
Here's the step-by-step explanation:
- Initialize
a
to 1 andb
to 0. - Enter the while loop because
a
(1) is less than or equal to 3. - Increment
a
by 1, makinga
now 2. - Add
a * 2
(which is 4) tob
, makingb
now 4. - Print
b
, which is 4. - Since
a
(2) is still less than or equal to 3, repeat the loop. - Increment
a
by 1, makinga
now 3. - Add
a * 2
(which is 6) tob
(which is 4), makingb
now 10. - Print
b
, which is 10. - Since
a
(3) is still less than or equal to 3, repeat the loop. - Increment
a
by 1, makinga
now 4. - Add
a * 2
(which is 8) tob
(which is 10), makingb
now 18. - Print
b
, which is 18. - Now
a
(4) is greater than 3, so the loop ends.
Similar Questions
What is the output of the following code snippet?int i = 0;do { if (i == 3) continue; System.out.print(i + " "); i++;} while (i < 5);
What is the output of following code?var a = 10;function test() { a = 20;}test();console.log(a);Options: Pick one correct answer from below1020
What will be the output for the following code?i=0while(i<3): j=0 while(j<3): print(i,j,end=" ") j+=1 print()
What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
What will be the result?1. int i = 10;2. while(i++ <= 10){3. i++;4. }5. System.out.print(i);10111213
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.