Loops examples and Pyramid Pattern

Do…while loop

Summary:

Loops examples and Pyramid Pattern

Example for do…while loop to print a list of integers.

Loops examples and Pyramid Pattern

In the do…while loop, code inside the loop will be executed at least once, so if the list is empty then we will get runtime exception since at 0’th index there is no element to handle such exception code is written inside try-catch. If we replace the initialization of the list with the

following code then we will get IndexOutOfBoundsException.

  List<Integer> integerList = new ArrayList<>();

Let’s see the program to print the pattern using do…while loop:

Note that since do…while loop executes at least once irrespective of the condition is true or false. Because of this, we will get one extra space before in the pattern as shown in the following example.

Loops examples and Pyramid Pattern

Related Posts