ICSE Class 10 Computer Applications Question 7 of 69

Iterative Constructs in Java — Question 7

Back to all questions
7
Question

Question 5

Convert the following for loop statement into the corresponding while loop and do-while loop:

int sum = 0;
for (int i = 0; i <= 50; i++)
    sum = sum + i;
Answer

while loop

int sum = 0, i = 0;
while (i <= 50)    {
    sum = sum + i;
    i++;
}

do-while loop

int sum = 0, i = 0;
do {
    sum = sum + i;
    i++;
} while(i <= 50);