0% found this document useful (0 votes)
61 views4 pages

Check Your Understanding of While Loops With These Problems

This document contains 12 examples of while loops with varying conditions and operations within the loop body. It tests understanding of while loop syntax and behavior by having the reader examine the code snippets and determine the output produced.

Uploaded by

Levi Trash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views4 pages

Check Your Understanding of While Loops With These Problems

This document contains 12 examples of while loops with varying conditions and operations within the loop body. It tests understanding of while loop syntax and behavior by having the reader examine the code snippets and determine the output produced.

Uploaded by

Levi Trash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Check your understanding of while loops with these problems.

1. char ltr = ‘g’;


while (ltr < ‘l’)
{
out.print(ltr + “ “);
ltr++;
}
ghijk

2. char ltr = ‘g’;


while (ltr > ‘a’)
{
out.print(ltr + “ “);
ltr--;
}
gfedcb

3. char ltr = ‘g’;


while (ltr < ‘l’)
{
out.print(ltr + “ “);
}
Infinite loop

4. int sum = 0, num = 1;


while (num <= 5)
{
sum += num;
num++;
}
out.println(sum);

15

5. int sum = 0, num = 1;


while (num <= 5)
{
sum += num;
num++;
out.println(sum);
}
1 3 6 10 15
6. int sum = 0, num = 1;

while (num <= 5) ;


{
sum += num;
num++;
out.println(sum);
7. int x = 10;
while (x <= 20)
{
out.print(x + “ “);
x += 2;
}
10 12 14 16 18

8. int x = 10;
while (x <= 20)
{
x += 2;
out.print(x + “ “);
}
12 14 16 18 20

9. int a = 1, b = 10;
while (a < b)
{
out.println(a + “ “ + b);
a++;
b--;
}
1 102 93 84 75 6

10. int a = 1, b = 10;


while (a < b)
{
a++;
b--;
}
out.print(a + “ “ + b);
65

11. int a = 0;
int b = 10;
while (a < 50)
{
a += b;
b +=a;
out.println(a + " " + b);
}
10 2030 5080 130
12. int x = 0;
int y = 1;
while (x < 6)
{
y = y * 2;
x++;
} out.println(“x = “ + x); out.println(“y = “ + y);

x=6
y = 64

You might also like