Tuesday, December 1, 2020
Home Programming How to Write a Loop for Java |

How to Write a Loop for Java |

Loops are very powerful programming tools that complement instructions until a condition is met. They are very handy and should be one of the first programming concepts you learn. There are many different loops, however for loops are without a doubt one of the most useful loops.


For For Loop Java

The loops continue to execute the code block until the condition is met. It is important to note that the for for loop checks the status at the beginning of the loop, not at the end. This means that if the condition is met, the loop will not start.

The loop syntax is the same in all programming languages. So if you have created a for loop in another programming language, Java for loop looks familiar. However, if you are not familiar with Java at all, it is recommended that you read the Beginner’s Guide before learning advanced topics such as loops.

for([statement1]; [condition]; [statement2]){
//code to execute each loop

Keyword for indicates a for loop. The condition that determines how long the loop continues is located between the parentheses. The first instruction is executed once the for loop is started. The condition determines when the loop should stop. The second sentence is executed at the end of each loop. The semicolons mark the end of the year Opinion 1 and fitness. Typically, statements are used to create a counter and the condition ends the loop when the counter reaches a certain number. Finally, the code to be executed in each loop is placed between the curly brackets.

public class Main{
public static void main(String[] args) {
for(int i = 1; i < 4; i++){
//Output: 123

In the example above, the for loop prints a value i. Keyword for format the loop. Variable i is initially set to 1. The condition checks to see if i is four or greater. This is not the case, so the loop is executed. The loop code prints the value i, which is still 1 at this point. When the loop code is complete, i grows by one and the loop starts again. At the end of the third loop i has been raised to four. When the next loop starts, our condition is met, so the loop stops.

Related to: Basic Java concepts you should learn when getting started

Nested for the loop

Once you’ve got the hanging for the for for loop, try creating a nested loop. In this case, you have for a loop inside another loop. This is an advanced technique because it can be difficult to understand how these two loops interact. A good way to illustrate the nestedness of loops is to create the following pattern with nested loops.


To create this, we need one loop to control how many stars are printed on each line, and another loop to control how many lines you create. When you have new loops nested, it can be difficult to determine which loop is the inner loop. In this case, the star-printing loop is the inner loop. We need that loop to work whenever a new row is created.

When creating a nested loop, be careful when choosing the name of the counter variables. Although often programmers use the general i counter, the use of common counters becomes confusing when multiple loops interact.

for(int lineCounter = 1; lineCounter < 4; lineCounter++){
for(int starCounter = 1; starCounter <= lineCounter; starCounter++){

Let’s go through this example to better understand how it works. The first loop counts how many lines we make. When the loop is completed three times, it stops. The next loop is a little more complicated. This loop controls how many stars are printed on each line. In our figure, we want the same number of stars as the line number. The first line has one star, the second two, and the third three. So we want the loop to print as many stars as the current line counter. When the star loop is complete, the line loop creates a new line by printing n, which is a newline command.

Infinite loops

One of the dangers of encoding any type of loop is that you can accidentally create an infinite loop. These are loops that never run out. Although in some cases an endless loop is required, they are usually created accidentally when the loop space is not carefully planned. In these cases, the program continues until you force it to close.

To create an infinite loop, use the following syntax:

//code that never stops looping

Related to: Websites and applications that can help you learn Java programming

Using For Loop with a matrix

A common way to use a for-loop is to iterate through a table. For example, if you want to print all the queues in a queue, you simply cannot say


This command prints table data, not table contents. To print the contents of a table, you must print each individual element of the table. This coding takes time, but you can create a for-loop to go through each element.

String[] words = {"Hello", " ", "World", "!"};

for(int i = 0; i < words.length; i ++){

Remember that table locations start at zero, not one, so we want the loop to start at zero. The first loop prints Hey, the second loop prints a space and so on. After the fourth loop, the counter increases to four, which is at least the length of the matrix, which is also four. This stops the loop.


Hello World!

For each loop

Although iterate using a table with a for for loop, it is easier to use for each loop. These loops are specially designed for matrices. Each loop A goes through each element of the matrix and executes the code. Each loop has a slightly different syntax. Keyword for is still used, but the condition is not defined.

for([dataType] [arrayElement] : [array]){
//code to be executed

Our previous example can be rewritten for each loop using this syntax:

String[] words = {"Hello", " ", "World", "!"};

for(String word : words){

The loop starts with a keyword for. We then determine that the data in our matrix are strings. Next, we choose the name of the variable to refer to the elements of the table by iterating through the loop. In this case, we used word. This is followed by the colon and the name of the table through which we want to repeat. Now inside the loop we just have to use a variable word refer to each element of the table.

When to use a loop

Loops are great tools that can save a lot of coding. They are the best type of loops to use when you know exactly how many times you want the loop to run. You can even increase the complexity of the loops by nesting them. Built-in loops are especially handy when working with multidimensional groups. For loops it is easy to learn and an important skill for beginners. This technique certainly saves unnecessary repetitive code from coding.

9 best free coding software tools for writing your first application

The 9 best free code editors for writing your first application

About the author


Patriotic Hackershttps://patriotichackers.com
Patriotichackers.com is an online tech and life hacks website. You can get here all kinds of content on Simplified Technology and Crafts. So you can catch us on Social Media Platforms to stay updated with us.


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments