Check Your Understanding of While Loops With These Problems

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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