OneBite.Dev - Coding blog in a bite size

implement a two nested loop in java

Code snippet on how to implement a two nested loop in java

  for (int i = 0; i < 5; i++) { 
      for (int j = 0; j < 5; j++) { 
          System.out.println("Hello"); 
      }
  }

This code shows an example of a two nested loop in Java. The outer for loop begins with a variable i set to zero, and only runs as long as the variable is less than 5. Inside this loop, a second for loop is placed, this time using the variable j with the same parameters. Between both for loops is a print statement that prints the word “Hello.” This basically means for every i, the inner for loop will run 5 times and print “hello” 5 times before the outer loop updates i by one and runs the inner loop again. In the end, the statement “Hello” will be printed 25 times.

java