For loop
Objective
In this lab, we will learn how to use for loop in Java with the help of examples and we will also learn about the workings of Loop in computer programming.
Learning Objective
By the end of this lab, learners will be able to use for loop in Java.
Overview of the Loop
A simple for loop allows us to iterate over items until a specified condition is met or satisfied. This normally works with the help of a counter, which will be initialized at the start of the execution and will be incremented with each iteration.
Based on the Java doc definition, a simple for loop has the following syntax:
Instructions
Example 1: Display a Text Five Times
Output:
Example 2: Display Sum of n Natural Numbers
Output:
The value of sum is 0 initially. Then the for loop is iterated from i = 1 to 1000. In each iteration, i is added to the sum and its value is increased by 1. When i becomes 1001, the test condition is false, and the sum will be equal to 0 + 1 + 2 + .... + 1000.
Example 3: Palindrome
In this example, we are going to see whether a number is a palindrome. A for loop is used in this case. A palindrome number, when reversed, represents the same number.
Example 4: Pyramid
In this example, we are going to see another application of for loop, which is a nested for loop. A nested for loop refers to a for loop within a for loop. This means that two for-loops are inside each other. They are generally used to print complex patterns on a Java platform. An example of a nested for loop is shown below.
Here, the class name is PyramidExample. The main() is declared. After that, the two loop control variables are declared. One is the loop control variable “i,” and the other is the loop control variable “j.” Then the “*” is printed in the loop control. The new line is given so that the given format of the pyramid structure is maintained. In this code, the program runs five times. However, by increasing the value of the letter “i,” we can make sure that the pyramid is bigger.
Output:
Last updated