Loops examples and Pyramid Pattern

Examples of how to print the Pyramid pattern using while, do…while, and the for loop

Loops examples and Pyramid Pattern

We know what is loops in java in this blog we will see examples for different loops in java. And how we can achieve printing data using different loops. We will also see how to print the Pyramid pattern using while, do…while, and the for loop.


While Loop

Summary:

Loops examples and Pyramid Pattern

Example: following is the example for the while loop to print the list of integers.

Loops examples and Pyramid Pattern

We have initialized i = 0 and while i is less than the side of the list will print the elements in the list.

Do not forget to increment the value of the “i” inside the while condition else loop will be infinite and will print the 23 until the program is explicitly terminated by a user.

If we also want to print the position of the element we can write the following code in the print statement.

System.out.println("Element at position " + i + " : "+ integerList.get(i));

We can also use nested while to print the data. Let’s see how to print the pattern using the nested while loop.

Loops examples and Pyramid Pattern

Related Posts