CompSci Blogs

August 2023 to June 2024

while loop

loop control variable

You know the i variable that you use for while/for loops? It actually has a name, loop control variable

public static void main(String[] args) {
    int i = 0; // initialize loop control variable
    while (i < 10)  // checks the loop control variable
    {
        System.out.println("Doing some code");
        i++;  // update the loop control variable
    }
}
main(null)
Doing some code
Doing some code
Doing some code
Doing some code
Doing some code
Doing some code
Doing some code
Doing some code
Doing some code
Doing some code

Infinite loop

an infinite loop is when a while loop always evaluates to true. avoid this when you can because it’s probably not good for your computer. if this happens by accident, i reccomend copying all code in the block and deleting the block. After you delete the code block, close and reopen the tab that the code block was in.

What’s wrong with this code block?

public static void main(String[] args) {
    while (true)
    {
        System.out.print("CONTROL ");
    }
}
// DO NOT RUN THE CODE

Do While loop

What will this code block output?

// Quite shrimple
public static void main(String[] args) {
    int i = 0;
    do 
    {
        System.out.print("Quite shrimple. ");
        i++;
    }
    while (i < -5);
}
main(null)

In a do while loop, it will run the “do” once before it reaches the “while”, and at that point it will start to act like a while loop.

for loop

this is the standard structure of a for loop

public static void main(String[] args) {
    for (initialization; Boolean expression; update)
    {
        System.out.println("Doing some code");
    }
}

initialization will run at the start of the loop, boolean expression will get checked with every loop, and update runs after every loop.

How many times will this code print “Doing some code?”

public static void main(String[] args) {
    for (int num = 1; num <= 5; num++)
    {
        System.out.println("Doing some code");
    }
}
main(null)

In this code, it creates the variable num at the start of the loop, it checks if num is less than or equal to 5 after each loop, and it adds 1 to num after each loop.

Enhanced for loop

this is essentially a javascript for loop, as it will iterate through a list and run code in the loop to each variable inside the list

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(2);
    list.add(1);
    list.add(5);
    list.add(3);
    list.add(8);
    for (int j : list)
    {
        System.out.print(j);
        System.out.print(" ");
    }
}
main(null)
2 1 5 3 8 

Break and Continue

In java there are breaks, but there are also continues.

Break

Breaks, as you likely already know, end a loop. They tend to be used with an if statement

How many times will this code print “Big guy?”

public static void main(String[] args) {
    int i = 0; 
    while (i < 10) 
    {
        System.out.println("Big guy");
        i++;  
        if (i == 5) {
            break;
        }
    }
}
main(null)

Continue

Continue will skip code for an iteration, but will still keep the loop running

public static void main(String[] args) {
    int i = 0; 
    while (i < 10) 
    {
        if (i == 5) {
            i++; // don't forget this, it creates an error similar to an infinite loop
            System.out.println("");
            continue;
        }
        System.out.println(i);
        i++;  
    }
}
main(null)
0
1
2
3
4

6
7
8
9

Hacks

What is the order for the header of a for loop?
What is the order for the header of an enhanced for loop?
How do you stop an infinite loop?
What is the name of the i that everyone uses with loops?

public static void main(String[] args)
{
    int number = 5;
    while (number > 100)
    {
        System.out.print(number + " ");
        number += 5;
    }
}
main(null)
public static void main(String[] args) {
    int i = 0; 
    while (i < 10) 
    {
        if (i == 5) {
            continue;
        }
        System.out.println(i);
        i++;  
    }
}
main(null)
public static void main(String[] args)
{
    int value = 10;
    while (value >= 0)
    {
        System.out.print(value);
        value++;
    }
}
main(null)