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

while loops answer key

Uploaded by

pinkcattwentytwo
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)
35 views4 pages

while loops answer key

Uploaded by

pinkcattwentytwo
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

“While” Loops Practice ANSWER KEY

Track the following code segments to determine what they will print.

For problems 1-6, use the following variable declarations:

final int MIN = 10,


final MAX = 20;
int num = 15;
num: OUTPUT:

1. while (num < MAX) 15 15


{ 16 16
System.out.println (num); 17 17
num = num + 1; 18 18
} 19 19
20 stop

2. while (num < MAX) 15


{ 16 16
num = num + 1; 17 17
System.out.println (num); 18 18
} 19 19
20 20
Stop

3. while (num < MAX) 15 15


{ 14 14
System.out.println (num); 13 13
num = num - 1; runtime error - infinite loop
} num < MAX is always true

4. while (num > MIN) 15 15


{ 14 14
System.out.println (num); 13 13
num = num - 1; 12 12
} 11 11
10 stop

5. while (num < MAX) 15 15


{ 17 17
System.out.println (num); 19 19
num += 2; 21 stop
}
num: output:

6. while (num < MAX) 15


{ 16 16
if (num%2 == 0) 17
System.out.println (num); 18 18
num++; 19
} 20

Problems 7-11 are independent of each other: num: dum: output:

7. int num = 0; 0 0
while (num < 3) 1 1
{ 2 2
System.out.println(num); 3 stop
num++;
}

8. int num = 10; 10 0 1098


int dum = 0; 9 1
while (num > 8 && dum < 3) 8 2
{ 7 3stop
System.out.print(num);
num--;
dum++;
}

9. int num = 1; 1 1-2-3-4-5


while (num <= 5) 2
{ 3
System.out.print(num + "-"); 4
num++; 5
} 6 stop

10. int num = 0;


while (num < 10) 0 11
{ 2
num += 2; 4
} 6
num++; 8
System.out.println(num); 10 stop
11
num: dum: output:
11. num = 0; 0 8
dum = 8; 8 975975975
while (num < 3) 6
{ 4
dum = 8; 2 stop
while (dum > 2) 1 8
{ 6
System.out.print(dum + 1); 4
dum -= 2; 2 stop
} 2 8
num++; 6
} 4
2 stop
3 stop

12. Write a “while” loop that prints the integers from 1 through 10 on the same line.

int num = 1;
while (num < 11) // or (num <= 10)
{
System.out.print(num)
num++; }

13. Write a “while” loop that prints the integers 10 down to 1 on the same line.

int num = 10;


while (num > 0) // or (num >=1)
{
System.out.print(num)
num--; } // or num -= 1 or num = num - 1

14. Write a “while” loop that prints the even integers 2 through 24 on separate lines.

int num = 2;
while (num < 26)
{
System.out.println(num)
num += 2; } // or num = num + 2

15. Write a “while” loop that prints multiples of 3 between 9 and 27 on the same line.

int num = 9;
while (num < 30) // or (num <= 27)
{
System.out.print(num)
num += 3; } // or num = num + 3

16. Write a “while” loop that prints numbers that are multiples of either 3 or 7 between 3 and 51.

int num = 3;
while (num <= 51)
{
if (num % 3 == 0 || num % 7 == 0)
System.out.println(num)
num++; }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy