Free Java Tutorials >> Table of contents >> Loops

1. While Loops, Do-While Loops

The While Loop

A loop allows you to run the same lines of code over and over. Usually, loops also have a condition, which must be true for the loop to run. When the condition becomes false, a loop stops running. Take a look at this first while loop:

public class MyProgram {
    public static void main(String[] args) {
        int i = 0;       //We often use the name i, which stands for "iterator" or "index"
		while(i < 10) {  //Does not run when i is greater or equal to 10
			System.out.println(i);
			i++; //remember this is pretty much the same as i = i + 1, adding 1 to i
		}
    }
}

A while loop always has the following syntax: while(CONDITION) { STATEMENTS }. If the condition is always true, we call the loop an infinite loop

The loop above starts with i equal to zero. Because i is less than ten, the loop begins. If you started i with 10, the loop would not even run once, since the while loop checks the condition prior to beginning. Once we enter the loop, it prints i, and then it adds one to i. After that, it jumps back to the top of the loop, checking if i is still less than 10. The result is a program that outputs the numbers 0 through 9.

While loop variable declaration

The variables used in the condition of while loops always need to be declare before the beginning of a while loop. This is because variables declared in the curly brackets are reset every time it re-enters the loop:

public class MyProgram {
    public static void main(String[] args) {
        int ab = 0;
		while(ab < 10) {
			int cd = 42;      //cd is defined here, and is redefined each time the loop runs
			System.out.println(ab + cd);
			cd = cd + 5;      //this line doesn't change the program
			ab++;
		}                     //cd ceases to exist here even when the loop restarts
    }
}

The do-while loop

A do while loop behaves almost exactly the same as a while loop, except the condition is checked at the bottom of the loop. In other words, even if the condition is false, the loop will execute at least once:

public class MyProgram {
    public static void main(String[] args) {
        int x = 9999;
		do {
			System.out.println(x); //This outputs 9999
			x++;
		}while( x < 10 ); //This is false, so the loop ends
    }
}

While loops are especially useful when getting user input. It allows you to run code before the user enters input, and repeat the code if the user inputs certain information.

2. Infinite Loops, Break, Continue

Warning about infinite loops

Infinite loops cause your program to run forever. If your program does something that slows down the computer in an infinite loop, it can cause a program or even a computer to freeze.

There are different ways to end a program. If you are running a Windows computer, you will need to search for the task manager. MacOSX computers use an activity monitor. Unix, Linux, and Terminal computers can kill task with the control-c shortcut or the kill command.

The break keyword

You can end a loop early with the break keyword. For example, let's add a break keyword to a loop that normally outputs 0 to 9:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++) { //loop wants to go from 0 to 9
			if(i*i == 4) {
				break;                   //but we will stop when i is 2
			}
			System.out.println(i);  //this only outputs 0 and 1 as a result
		}
    }
}

This is especially useful later on when we do not know before hand how many times the loop will run. Generally, try to change the condition of a loop rather than add break statements. This allows a read to know how many times the loop will run without reading the body of the loop.

The continue keyword

The continue keyword, unlike break, jumps back to the top of the loop:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++) { //loop wants to go from 0 to 9
			if(i*i == 4) {
				continue;                   //Skips to 3 when i is 2
			}
			System.out.println(i);  //this outputs 0,1, skips 2, then prints 3,4,5,6,7,8,9
		}
    }
}

The result is "continue" skips all the code below it within the body, but the loop resumes if the condition is true.

3. For Loops

For Loops

Notice that in all of the previous while loops, we had three common parts. First, we always created a variable above the loop (i,j,k, etc). Secondly, we had a boolean condition that was checked at the top of each loop. Thirdly, we changed the variable at the end of each loop. A for loop combines these three parts into one line:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 0; i<10 ; i++) {
			System.out.println(i);
		}
		//int i = 0; is run first. the i variable exists only in the loop {}
		//i < 10 is checked at the top of each loop
		//System.out.println(i) is run after the condition is checked
		//i++ is run *after* the println, and the loop goes back to the condition
    }
}

For loops are always of the form: for(INIT;CONDITION;UPDATE){ STATEMENTS }. The order that things are run is

  1. Run the INIT. Variables created here persist between multiple loops. However, they stop existing after the loop ends
  2. Check the CONDITION. If this is false, then we exit the loop entirely, jumping after the last close curly.
  3. Run the STATEMENTS. This is confusing for most students, but the STATEMENTS occur before the UPDATE
  4. Run the UPDATE
  5. Go back to step 2 (the CONDITION)

Using the i variable outside the loop

In some cases, you would like to use the i variable after a for loop. To do that, i must be declared above the loop:

public class MyProgram {
    public static void main(String[] args) {
		int i = 0;
        for(i = 0; i<10 ; i++) {
			System.out.println(i);
		}
		System.out.println("i is now:");
		System.out.println(i); //i is now 10
		//note, you cannot declare i again
    }
}

Empty conditions, declarations, iterators

It may surprise you that the following is a legal program:

public class MyProgram {
    public static void main(String[] args) {
		int i = 0;
        for(;;) { //Warning: this is considered bad style by most programmers
			System.out.println(i);
			i++;
			if(i == 10) break;
		}
    }
}

This program outputs 0 to 9, but it is considered difficult to read and bad style. As you can see, all segments of the for loop are optional, so this loop behaves like an infinite loop. It does the same thing as while(true){}. Although it is rare to see all three portions empty, there are some cases where leaving out one portion is reasonable.

public class MyProgram {
    public static void main(String[] args) {
        int i = 0;       //We often use the name i, which stands for "iterator" or "index"
		while(i < 10) {  //Does not run when i is greater or equal to 10
			System.out.println(i);
			i++; //remember this is pretty much the same as i = i + 1, adding 1 to i
		}
    }
}

A note about the for each loop

You may see a loop that looks like this:

for(int x : y){}

This is called a "for each" loop, and it is covered in the section on arrays. It operates in a completely different manner than the for loop above, even though it uses the for keyword.

4. Simple Loop Algorithms

What are Algorithms?

A computer program is a sequence of commands like what we have been writing in Java. In contrast, an algorithm is a step-by-step procedure for solving a problem, where the steps may be written in no particular language. The act of creating an algorithm is to figure out how to translate a human problem into a computer program. Once the problem is defined as a computer program, we can study its effectiveness and efficiency in a mathematical manner.

Printing even numbers between 2 and 100 (inclusive)

Our first algorithm is to print the number 2,4,6,8,10, all the way up to 100. Here is the English for an algorithm:

//1. Start a variable at 2
//2. Check if the variable is still less than or equal to 100. Stop otherwise
//3. Print the variable
//4. Increase the variable by 2
//5. Jump to step 2

You will discover that this English algorithm can be written as code in any "turing complete" language including Java. Let's see how we can write it in Java:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 2; i<= 100 ; i+=2) {
			System.out.println(i);
		}
    }
}

That is it! However, there is more than one solution to the problem:

A second way to print even numbers

Let's look at another algorithm that also prints the even numbers from 2 to 100:

//1. Start a variable at 1
//2. Check if the variable is still less than or equal to 100. Stop otherwise
//3. Check if the variable is even. Print the variable if it is even
//4. Increase the variable by 1
//5. Jump to step 2

Notice that this algorithm requires a loop from 1 to 100 by increments of one. We can start writing it with this loop:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 1; i<= 100 ; i++) {
			//do something
		}
    }
}

Then, we can insert an if statement inside of it to check if i is even:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 1; i<= 100 ; i++) {
			if(i % 2 == 0) {
				System.out.println(i);
			}
		}
    }
}

Thus our algorithm is complete. Notice how this algorithm needs to go over more values of i to print the same output. In everyday English, we call this "efficiency", but in Computer Science, we call this computational complexity. We will talk more about complexity and speed in a later chapter

Counting down from 10 to 1

Here the algorithm starts at 10 and goes down:

//1. Start a variable at 10
//2. Check if the variable is still positive. Stop otherwise
//3. Print the variable
//4. Decrease the variable by 1
//5. Jump to step 2

Step 2 is the most important. If you made a mistake and checked "if the number was less than 10", then the loop would go forever. Here is the Java code:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 10; i> 0 ; i--) {
			System.out.println(i);
		}
    }
}

Printing perfect squares from 1 to 100

At first glance, you may try to write a loop from 1 to 100 for this problem. Then you would need to somehow check if that number was a perfect square. However, there is an easier algorithm to write:

//1. Start a variable at 1
//2. Check if the variable is at most 10. Stop otherwise
//3. Print the variable squared
//4. Increase the variable by 1
//5. Jump to step 2

Notice that the loop we want executed exactly 10 times, so we do not need to iterate over 100 values:

public class MyProgram {
    public static void main(String[] args) {
        for(int i = 1; i<= 10 ; i++) {
			System.out.println(i*i);
		}
    }
}

To be more explicit that we want perfect squares, we could also write the condition as i*i <= 100, which would have the same effect. Even though this is longer (more "verbose"), some people might prefer it. Java, like any language, can be written in many styles. Think about who will read your code, for what reason, and when. That thought process will guide both your algorithms and your style.

5. Scoping

Scope

The scope of a variable is where a variable exists. Outside of the scope, the variable does not exist. Scope in Java is defined by curly brackets:

public class MyProgram {
    public static void main(String[] args) {
		if(true) {
			int abc = 42;
			System.out.println(abc); //This compiles
		}
		System.out.println(abc); //This is an error
		//We can no longer use abc here, since the "scope" around abc has ended. 
		//If we wanted to use abc here, we should have declared it above the if statement
    }
}

Nested Blocks

A set of curly brackets is also called a block in Java. When you have a block inside a block, the inner block has access to all the variables of the outer block (but not vice versa):

public class MyProgram {
    public static void main(String[] args) {
		//neither i nor j exists until they are defined:
		for(int i = 0; i < 10; i++) {
			System.out.print("Loop: ");
			System.out.println(i);
			for(int j = 0; j < i; j++) {
				System.out.print(i);
				System.out.print(" ");
				System.out.println(j);
			}
			//We can still access i here, but not j
		}
		//We can access neither i nor j here, out of scope
    }
}

Sequential scope declarations

This is allowed:

public class MyProgram {
    public static void main(String[] args) {
		for(int i = 0; i < 10; i++) {
			int j = i*2;
			System.out.println(j);
		}
		for(int i = 0; i < 10; i++) {
			int j = i*2;
			System.out.println(j);
		}
    }
}

Variables declared in for loops (either in the parenthesis or in the body) end at the close curly. Thus, we can create a new i and j in the next scope.

Bare scope

You can declare scope without an if, while, or for-loop to go with it:

public class MyProgram {
    public static void main(String[] args) {
		{
			int i = 5;
			System.out.println(i);
		}
		int i = 42; //This is okay!
		System.out.println(i); 
    }
}

Indentation

To make code more readable, it is important to indent code whenever you enter a new scope. However, Java does not read any whitespace characters in your code. Thus, the following is legal even though it is hard to read:

public class MyProgram {
public static void main(String[] args) {
	for(int i = 0; i < 10; i++) {
	System.out.print("Loop: "); System.out.println(i);
	for(int j = 0; j < i; j++) { System.out.print(i); System.out.print(" "); System.out.println(j); }
	}
    }
}

Try copying the above code into the code box and re-indenting it. See our appendix for auto-indentation shortcuts in common IDEs.

Previous: Boolean Logic

Next: Loop Algorithms