Java Nested Loops with Examples Last Updated : 11 Jan, 2024 Comments Improve Suggest changes Like Article Like Report A Nested loop means a loop statement inside another loop statement. That is why nested loops are also called "loop inside loop". Nested For loop for ( initialization; condition; increment ) { for ( initialization; condition; increment ) { // statement of inside loop } // statement of outer loop} Nested While loopwhile(condition) { while(condition) { // statement of inside loop } // statement of outer loop} Nested Do-While loopdo{ do{ // statement of inside loop } while(condition); // statement of outer loop}while(condition); Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level. Hybrid Java Nested Loopsdo{ while(condition) { for ( initialization; condition; increment ) { // statement of inside for loop } // statement of inside while loop } // statement of outer do-while loop}while(condition); Below are some examples to demonstrate the use of Nested Loops: Examples of Java Nested LoopsExample 1: Below program uses a nested for loop to print a 2D matrix. Java // Java program to print the elements of // a 2 D array or matrix import java.io.*; // Driver Class class GFG { public static void print2D(int mat[][]) { // Loop through all rows for (int i = 0; i < mat.length; i++) { // Loop through all elements of current row for (int j = 0; j < mat[i].length; j++) System.out.print(mat[i][j] + " "); System.out.println(); } } // main function public static void main(String args[]) throws IOException { int mat[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; print2D(mat); } } Output1 2 3 4 5 6 7 8 9 10 11 12Example 2: Below program uses a nested for loop to print all prime factors of a number. Java // Java program to print all prime factors import java.io.*; import java.lang.Math; // Driver Class class GFG { // A function to print all prime factors // of a given number n public static void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { System.out.print(i + " "); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) System.out.print(n); } // main function public static void main(String[] args) { int n = 315; primeFactors(n); } } Output3 3 5 7 Comment More infoAdvertise with us Next Article Java Nested Loops with Examples C code_r Follow Improve Article Tags : Java loop Practice Tags : Java Similar Reads Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Properties keys() method in Java with Examples The keys() method of Properties class is used to get the enumeration of the keys in this Properties object. This enumeration can be used to traverse and iterate the keys sequentially. Syntax: public Enumeration keys() Parameters: This method accepts no parameters Returns: This method returns an Enum 2 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Properties elements() method in Java with Examples The elements() method of Properties class is used to get the enumeration of this Properties object. It can be further used to retrieve the elements sequentially using this Enumeration received. Syntax: public Enumeration elements() Parameters: This method do not accepts any parameters.Returns: This 2 min read Stack iterator() method in Java with Example The Java.util.Stack.iterator() method is used to return an iterator of the same elements as that of the Stack. The elements are returned in random order from what was present in the stack. Syntax: Iterator iterate_value = Stack.iterator(); Parameters: The function does not take any parameter. Return 2 min read AbstractList listIterator() Method in Java with Examples The listIterator() method of java.util.AbstractList class is used to return a list-iterator containing the same elements as that of the AbstractList in proper and same sequence starting from a specific position or index number which is passed as a parameter to this method. Syntax: ListIterator new_l 2 min read Collections fill() method in Java with Examples The fill() method of java.util.Collections class is used to replace all of the elements of the specified list with the specified element. This method runs in linear time. Syntax: public static void fill(List list, T obj) Parameters: This method takes following argument as parameter list - the list t 2 min read Comparing Streams to Loops in Java A stream is an ordered pipeline of aggregate operations(filter(), map(), forEach(), and collect()) that process a (conceptually unbounded) sequence of elements. A stream pipeline consists of a source, followed by zero or more intermediate operations; and a terminal operation. An aggregate operation 7 min read HashTable forEach() method in Java with Examples The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. 2 min read Like