Uploaded by Nina Huang

Nina Huang - Loops Worksheet 1

advertisement
Name Nina Huang
APCSA – Loops
Class Period 3 Date 11/8/23
Worksheet 1
1.
Complete the statements below to sum the integer values between 100 and 200, inclusive (i.e., 100 + 101
+ 102 + … + 198 + 199 + 200).
int sum =
0;
int number =
while (
100;
number <= 200 )
{
sum
= sum + number;
number
++ ;
}
Questions 2 – 5: Draw a box around your output. If there is no output, write “No Output”. If there is an infinite
loop, show 3-4 iterations and write “Infinite Loop”. Credit will only be given if all work is shown.
2.
What is output by the following code segment?
int x = 10, y = 20;
while (x + y > 20)
{
x -= 5;
y += 2;
}
System.out.println("x = " + x + "
3.
What is output by the following code segment?
int x = 10;
while (x > 5)
{
x += 2;
System.out.print(x + "
}
4.
");
What is output by the following code segment?
int x = 8, y = 3;
while (x >= y && y < 10)
{
System.out.println(x + y);
x++;
y += 3;
}
y = " + y);
System.out.println("x = " + x + "
5.
What is output by the following code segment?
int x = 3, y = 5, z = 7;
while (x < y || z % 3 != 0)
{
x = y + z;
z++;
}
System.out.println("x = " + x + "
6.
y = " + y);
y = " + y + "
What is output by the following code segment?
Scanner chopper = new Scanner("15 9 12
System.out.println(chopper.nextInt());
System.out.println(chopper.nextInt());
System.out.println(chopper.nextInt());
7.
6
27");
6
27");
What is output by the following code segment?
Scanner chopper = new Scanner("15
while (chopper.hasNextInt())
{
int value = chopper.nextInt();
if (value % 2 != 0)
System.out.println(value);
}
8.
z = " + z);
9
12
What is output by the following code segment?
Scanner chopper = new Scanner("green 1.007
System.out.println(chopper.next());
System.out.println(chopper.nextDouble());
System.out.println(chopper.next());
System.out.println(chopper.nextInt());
blue
15
red
3.25
7");
9.
What is output by the following code segment?
Scanner chopper = new Scanner("green 1.007
double sum = 0;
while (chopper.hasNext())
{
if (chopper.hasNextDouble())
{
System.out.print("#");
sum += chopper.nextDouble();
}
else
{
System.out.print(chopper.next());
}
}
System.out.println("\nsum = " + sum);
blue
15
red
3.25
7");
Download