JAVA loops: An introduction
A JAVA loop is needed when we want to repeat the same processing for a
set of items.
Repeating the same instructions for each item using the copy/paste operation
will increase considerably the number of lines of code of our program.
This will result in big programs which are difficult to maintain.
For example, if we want to display a “Hello World” message 100 times
without using JAVA loops, we write this code:
System.out.println(“Hello World”);
System.out.println(“Hello World”);
System.out.println(“Hello World”);
.
.
.
System.out.println(“Hello World”);
The JAVA loops solve this problem by allowing the programmer to repeat a
common processing for a set of items without using the copy/paste operation.
For example, the previous source code becomes like this:
for(int i =0; i < 100; i++){
System.out.println(“Hello World”);
}
The JAVA language, like most of programming languages, implements the
JAVA loops through looping statements:
- Java for loop statement
- JAVA while loop statement
- JAVA do while loop statement
- JAVA foreach loop statement
Please, click on the links to get into the details of each JAVA loop statement.
Please, make sure to follow us on Facebook to get our latest Core JAVA tutorials.
Post a Comment