Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 2

Loops

We can repeatedly execute a sequence of code by creating a loop. Java supplies a powerful assortment of loop constructs that enable programmers to control the flow of execution by repetitively performing a set of statements as long as the continuation condition remains true. These are while, do-while and for.

While loop

The while loop repeats a statement or a group of statements as long as a given condition remains true. The loop tests its condition before executing the loop body.

Syntax:

 while (expression) {
     // statement(s)
} 

The while statement evaluates expression which returns a boolean value. If the expression is true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

Example:

public class Counter {
    public static void main(String[] args) {
        int count = 1;
        while (count < 4) {
            System.out.println("Count is: " + count);
            count++;
        }
    }
}

The output of the program would be:

 Count is: 1
 Count is: 2
 Count is: 3

Do-While loop

Do-While loop is like a while statement, except that it tests the condition at the end of the loop body.

Syntax:

 do {
     // statement(s)
} while (expression); 

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Example:

public class Counter {
    public static void main(String[] args) {
        int count = 1;
        do {
            System.out.println("Count is: " + count);
            count++;
        } while (count < 4);
    }
}

The result is the same as in the previous example.

For loop

For loop provides a compact way to iterate over a range of values.

Syntax:

 for (initialization; termination; increment) {
    // statement(s)
} 

Keep in mind, that

  • the initialization expression initializes the loop; it is executed once when the loop begins;
  • when the termination expression evaluates to false, the loop terminates;
  • the increment expression is invoked after each iteration through the loop.

Example:

public class Counter {
    public static void main(String[] args) {
         for(int i = 1; i < 4; i++) {
              System.out.println("Count is: " + i);
         }
    }
}

The result is the same as in the previous example.

Like in Python, use:

  • continue to break the current iteration and proceed with the next iteration;
  • break to jump out of the loop.

Some more examples on loops:

Optional material

Nested loops

Chapter 2
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment