Java Basics Tutorial – Part 7 – For Loops

In this lesson of the Java Basics Tutorial, we take a look at For Loops.

In programming, a loop is used to repeat a block of code until a specified condition is met. The For Loop is best when you want to do something for a fixed number of times.

How does For Loop work?

  1. Control falls into the For Loop. Initialization is done.
  2. The flow jumps to the Condition.
  3. The Condition is tested.
    1. If it is true, the flow goes into the body.
    2. If it is false, the flow goes outside the loop.
  4. The statements inside the body of the loop get executed.
  5. The update takes place, and the flow goes to Step 3 again.
  6. The For Loop has ended, and the flow has gone outside.

This process goes on until the test expression is false. When the test expression is false, the loop terminates.

Say we want to loop over a range of numbers and print out each one along the way. We can do this best with a For Loop. We will start from the first number, print it out, move to the next number to do the same thing, and continue until we’ve printed each number. Let’s print the numbers zero through nine:

Let’s look at the first line of the For Loop and the three parts necessary to make it work. First, we have an initialization expression, then termination expression, and increment expression in the end.

The initialization expression initializes the loop and is only executed once when the loop starts. That is where you must declare and initialize a variable, usually of type int, to hold an initial number that will be used to loop until it reaches a specific value. That can be thought of as the start of the range to iterate over. In our case, we started at 0.

The For Loop will continue looping until the termination expression evaluates to false. The termination expression is evaluated before each iteration of the loop, and it must return a boolean to decide whether or not to continue looping. If the boolean returned is equal to true, we will run the body of our For Loop again. In our case, the loop terminates after it prints 9.

for-loopsThe increment expression is executed after each iteration of the loop. To increment the variable, we initialize it by using the ++ operator. That allows the termination expression to know how many times the loop loops. The initialization variable count, in our example, starts at 0, and it’s printed out. Then the incrementer increments it, and the next iteration of the loop runs to print the new value and continues that way until it prints 9.

In conclusion, you should use a For Loop when you know how many times the loop should run. There are other more advanced iteration structures that we will look at in the next article.

Lesson Topics

In this video we review the following topics:
  • Increment adn Decrement
  • For Loops
  • Loops with a Step
  • Iterating over Characters
  • Infinite Loops

Practical Exercises

Watch the video and solve the problemsTo better understand the material, do the coding exercises and implement the knowledge you acquired. Writing code is the only way to master the skill of code.

Submit your code in the SoftUni Judge System:

Exercises: Problem Descriptions

Lesson Slides

Leave a Comment

Scroll to Top