DEV Community

nadirbasalamah
nadirbasalamah

Posted on

Java Tutorial - 3 Control Flow (Iteration)

Iteration or loop is a mechanism that is useful to execute a repeated code. There are many types of iteration in Java including while, for and do while.

while loop

while loop is a loop or iteration that is executed if the certain condition is true or match. The basic syntax of while loop looks like this:

while(condition) {
    // code..
}
Enter fullscreen mode Exit fullscreen mode

This is the example of while loop to prints out a number from 0 to 10.

public class MyApp {
    public static void main(String[] args){
        // declare a variable called counter
        int counter = 0;
        // while the counter is less or equals 10,
        // execute the code inside the while block
        while (counter <= 10) {
            // inside the while block,
            // prints out the counter
            System.out.println(counter);
            // increase the counter by 1 (counter = counter + 1)
            counter++;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

0
1
2
3
4
5
6
7
8
9
10

Enter fullscreen mode Exit fullscreen mode

The flow of while loop is like this:

  1. Check the condition.
  2. If the condition is true, execute the code inside the while block.
  3. If the condition is false, stop the execution.

Based on that code, there is a operation to increase a counter's value by 1 using ++ operator, this operator is equals with counter = counter + 1.

Here it is another similiar operator that usually used together with loop mechanism.

Operator Description
+= a += b equals to a = a + b
-= a -= b equals to a = a - b
*= a *= b equals to a = a * b
/= a /= b equals to a = a / b
%= a %= b equals to a = a % b
++ a++ equals to a = a + 1
-- a-- equals to a = a - 1

for loop

Another loop or iteration mechanism that can be used is for loop. this loop mechanism is suitable to repeat an execution in an exact amount of time.

This is the basic syntax of for loop.

for(initial state; condition; post-condition) {
    // code..
}
Enter fullscreen mode Exit fullscreen mode

There are 3 parts in this syntax:

  1. initial state: defines the initial value.
  2. condition: defines the condition.
  3. post-condition: defines the action if the condition is true or match.

This is the example of using for loop to prints out the number from 1 to 5.

public class MyApp {
    public static void main(String[] args){
        for (int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
3
4
5

Enter fullscreen mode Exit fullscreen mode

This is the explanation from the code above.

  1. initial state: the initial state or value is a variable i equals 1.
  2. condition: the condition is if i less than or equals 5.
  3. post-condition: if the condition matches or true, increase the value of i by 1.
  4. then execute the code inside the for loop until the condition is false.

The nested for loop is also available. Here it is the example of using nested for loop.

public class MyApp {
    public static void main(String[] args){
        for (int i = 0; i <= 3; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output


*
**
***

Enter fullscreen mode Exit fullscreen mode

Based on that code, the inner loop is executed first then the outer loop is executed.

do while loop

do while loop is a loop mechanism that checks the condition after the code inside the block is executed. This is the basic syntax of do while loop.

do {
    // the code..
} while(condition);
Enter fullscreen mode Exit fullscreen mode

This is the example of using do while loop.

public class MyApp {
    public static void main(String[] args){
        int counter = 0;
        do {
            System.out.println(counter);
            counter++;
        } while (counter <= 5);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

0
1
2
3
4
5

Enter fullscreen mode Exit fullscreen mode

Based on the code above, the code inside the do while block is executed then the condition inside while(..) is checked.

In this another example, the counter value is printed although the condition is false inside the while(..). Because the condition is checked after the code inside the do while block is executed.

public class MyApp {
    public static void main(String[] args){
        int counter = 10;
        do {
            System.out.println(counter);
            counter++;
        } while (counter <= 5);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

10

Enter fullscreen mode Exit fullscreen mode

The quick tips of using loop mechanism is to consider which loop mechanism is suitable and make sure the condition is defined correctly to avoid infinite loop.

Sources

  • Check out this resource to learn more about loop in Java.

I hope this article is helpful for learning the Java programming language. If you have any thoughts or comments you can write in the discussion section below.

Top comments (0)