knowt logo

Unit 9 - APCSA

Disclaimer: Notes based off A+ Computer Science

Loops

A loop is a series of code that repeats until a given condition is reached. Take this sample “code” for instance:

loop (if tired and hungry)
{
You will eat and then sleep
}


For Loops

A for loop is a block of code that runs depending on the loop condition and the increment/decrement value. E.g.:

for (in it value; boolean condition placed here; inc/dec)
{
Do a thing
}


You have to tell the loop where to Start / Stop / Increment or Decrement E.g.:

for (int i = 1; i <= 5; i += 1)
{
System.out.print(i);
}
//Output 1 2 3 4 5


Some loops use a start, stop, step structure.

  • Start

    • Initial value

  • Stop

    • Final value

  • Step

    • Amount to add or substract from the loop variable

The stop is a boolean, that condition must be met before the loop can stop. (E.g. for (int i = 1; i <= 10; i++) - i starts at 1 and increments by 1 every iteration. The loop will stop after 10 iterations) After every iteration, the condition is evaluated to see if it has been met; as long as the i is less than or equal to 10, it will run.


You can also set a loop up like this:

int i = 1;
for (; i <= 6;)
{
System.out.println(i);
i = i + 1;
}
//Output 1 2 3 4 5 6

This works because you are already defining a variable outside of the loop - then, during the iteration, you are incrementing the condition variable. You must still put the semicolons where they would be in the condition.


Sometimes you may need to add the value of a condition variable to a total variable. E.g.:

int total = 0;
for (int i = 1; i < 6; i++)
{
total = total + i;
}
System.out.println(total);

//Output 15

Here’s a table to visualise what’s going on here:

i

Total

Output

0

1

1

2

3

3

6

4

10

5

15

6

15


While Loops

A while loop runs while a boolean is false. It stops when the condition has been met - the condition only contains the stop, not the start or step. E.g.:

int i = 1;
while (i <= 10)
{
i + 1;
System.out.println(i);
}
//Output 2 3 4 5 6 7 8 9 10

And remember do not put a semicolon before a curly bracket. E.g.:

Illegal - ;{

Legal - };


Nested Loops

A nested loop is a loop inside of another loop. For every iteration on the outer loop, the inner loop runs to completion. E.g.:

for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
System.out.println(j);
}
System.out.println(i);
}
//Output 1 2 3 4 5 1 1 2 3 4 5 2 1 2 3 4 5

JD

Unit 9 - APCSA

Disclaimer: Notes based off A+ Computer Science

Loops

A loop is a series of code that repeats until a given condition is reached. Take this sample “code” for instance:

loop (if tired and hungry)
{
You will eat and then sleep
}


For Loops

A for loop is a block of code that runs depending on the loop condition and the increment/decrement value. E.g.:

for (in it value; boolean condition placed here; inc/dec)
{
Do a thing
}


You have to tell the loop where to Start / Stop / Increment or Decrement E.g.:

for (int i = 1; i <= 5; i += 1)
{
System.out.print(i);
}
//Output 1 2 3 4 5


Some loops use a start, stop, step structure.

  • Start

    • Initial value

  • Stop

    • Final value

  • Step

    • Amount to add or substract from the loop variable

The stop is a boolean, that condition must be met before the loop can stop. (E.g. for (int i = 1; i <= 10; i++) - i starts at 1 and increments by 1 every iteration. The loop will stop after 10 iterations) After every iteration, the condition is evaluated to see if it has been met; as long as the i is less than or equal to 10, it will run.


You can also set a loop up like this:

int i = 1;
for (; i <= 6;)
{
System.out.println(i);
i = i + 1;
}
//Output 1 2 3 4 5 6

This works because you are already defining a variable outside of the loop - then, during the iteration, you are incrementing the condition variable. You must still put the semicolons where they would be in the condition.


Sometimes you may need to add the value of a condition variable to a total variable. E.g.:

int total = 0;
for (int i = 1; i < 6; i++)
{
total = total + i;
}
System.out.println(total);

//Output 15

Here’s a table to visualise what’s going on here:

i

Total

Output

0

1

1

2

3

3

6

4

10

5

15

6

15


While Loops

A while loop runs while a boolean is false. It stops when the condition has been met - the condition only contains the stop, not the start or step. E.g.:

int i = 1;
while (i <= 10)
{
i + 1;
System.out.println(i);
}
//Output 2 3 4 5 6 7 8 9 10

And remember do not put a semicolon before a curly bracket. E.g.:

Illegal - ;{

Legal - };


Nested Loops

A nested loop is a loop inside of another loop. For every iteration on the outer loop, the inner loop runs to completion. E.g.:

for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
System.out.println(j);
}
System.out.println(i);
}
//Output 1 2 3 4 5 1 1 2 3 4 5 2 1 2 3 4 5