Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

What is a for loop in Java?

As you might have noticed in previous assignments, code can get a bit tedious at times. Writing many if statements can take a while and sometimes you might think, “Isn’t there a better way?”. Well, sometimes there is a better way.

When you play a song, you can set it to loop, which means that when it reaches the end it starts over at the beginning. A loop in programming, also called iteration or repetition, is a way to repeat one or more statements. If you didn’t have loops to allow you to repeat code, your programs would get very long very quickly! Using a sequence of code, selection (ifs), and repetition (loops), the control structures in programming, you can construct an algorithm to solve almost any programming problem.

There are a number of loops in Java. The most often used one is a for loop. This is usually used when you know how many times you want the loop to execute. It is often a simple counter-controlled loop to do the loop body a set number of times.

While this is similar to the Java for loop, it differs in some very important ways. Let’s take a look at how to build and use one. A for-loop has 3 parts: initialization, condition, and change. The parts are separated by semicolons (;). Each of the three parts of a for loop declaration is optional (initialization, condition, and change), but the semicolons are not optional.

for (initialization; condition; change)
{
   //loop body
   //do stuff
}

We can take a look at a for loop in action.

Initialization: The following for loop is initialized to start counting from 1.

Condition: It will continue executing code until the count variable is less than or equal to 5.

Change: Each time that it finishes executing the code in the brackets, it will increase the count variable by + 1 and execute the code all over again.

It’s important to note that the change condition can be any expression that changes the variable in the condition section. For example, I can decrease the count variable after every loop completes or increase by a number other than 1.

See how the count variable could use –, but instead we choose a different convention. Either code would be fine.

Create a For Loop

Lastly, let’s try and create our own loop. In The Simpson’s, Bart can be seen writing the same message many times on a blackboard as his punishment for something naughty he did. Let’s help him out and write a program that can do that for him.

  • Print out the message on the picture, 1000 times by using a for loop.
Bart’s Punishment

Summary

  • There are three parts in a for loop header: the initialization, the Boolean expression, and the increment or decrement statement.
  • In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable.
  • In each iteration of a for loop, the increment statement is executed after the entire loop body is executed and before the Boolean expression is evaluated again.