0% found this document useful (0 votes)
207 views

Name: - Date: - APCSA - Loops Worksheet 1

This worksheet contains 10 coding problems involving for and while loops. Students are asked to determine the output of each code block and show the step-by-step workings. The problems cover a range of looping concepts like incrementing/decrementing variables, accumulating totals in loops, modifying variables inside loops, and checking conditions on each pass through the loop.

Uploaded by

Marcos White
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views

Name: - Date: - APCSA - Loops Worksheet 1

This worksheet contains 10 coding problems involving for and while loops. Students are asked to determine the output of each code block and show the step-by-step workings. The problems cover a range of looping concepts like incrementing/decrementing variables, accumulating totals in loops, modifying variables inside loops, and checking conditions on each pass through the loop.

Uploaded by

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

Name : ___________________________ Date : _________________

APCSA - Loops Worksheet 1

Show the output of each block of code below.

1. What is the output?


for(int i=1; i<15; i+=3)
{
System.out.println(i);
}

2. What is the output?


for(int j=11; j>-2; j=j-2)
{
System.out.println(j);
}

3. What is the output? int number = 4;


for (int count = 1; count <= number; count++)
{
System.out.println(number);
number = number / 2;
}

4. What is the output? int total=0;


for(int s=1; s<5; s++)
{
total=total+s;
}
System.out.println(total);

5. What is the output?


int p = 1;
int q = 2;
for (int k = 1; k < 4; k=k+1)
{
p = p + q;
q = q + p;
}
System.out.println(p + " " + q);
Show the output of each block of code below.

1. What is the output?


int x = 1;
while( x < 5 )
{
x = x + 1;
System.out.print(x);
}

2. What is the output?


int m=2, total=0;
while( m < 6 )
{
total = total + m;
m = m + 1;
}
System.out.print(total);

3. What is the output? int z=2, sum=0;


while( z < 9 )
{
z = z + 1; //same as z++;
sum = sum + z;
}
System.out.print(sum);

4. What is the output?    int x = 5; 


      int y = 15; 
      while (x < y) 
      { 
         x = y + 2; 
         y = x - 2; 
      } 
      System.out.println("x = " + x); 
 
5. What is the output? int b=5;
String list="";
while( b < 11)
{
b = b + 2;
if( b % 2 == 1 ) //checks remainder of b / 2
list = b + " " + list;
}
System.out.print(list);

You might also like