Loops In Java

In this article we are going to learn about loops in java. We weill learn about for loop, while loop and do while loop. We will see working of all loop and write some java program using loops in Java.

Why Loops are Important ?

In programming languages loops are used to repeat some lines of code or block of code according to our requirement . Loops are usefull in saving time and energy because they execute code fast and also saves time during writing code

Let’s see the different type of loops in Java

While loop in java:-

Working of while loop in java is same as while loop in C, C++. While loop is keep iterating code present insided untill the given condition is true. In while loop first condition is checked only after that code is executed.

While loop syntax:-

While(condition){
  // codes of (including increment or decrement for condition)
}

While loop java program:-

package gangforcode;

public class While {

	public static void main(String[] args) {
		int i=1;
		while(i<=100) {
			System.out.println(i);
			i++;
			
		}
	}

}

Using java code we are printing numbers from 1 to 100 using while loop.

Do-While loop in java:-

Do while loop is also similar like while looop only diffrence between do while loop and while loop is Do while loop first execute the code than check the condition.

Syntax of the do-while loop:-

Do{
// codes
}
while(condition);

Do while loop program in java:-

package gangforcode;

public class DoWhile {

	public static void main(String[] args) {
	
		int i=1;
		do{
			System.out.println(i);
			i++;	
		}while(i<=100);
	}

}

In the above java code we are printing number from 1 to 100 using Do-while loop.

For loop in java:-

Working of for loop in java is same as working of for loop in C and C++. Syntax of for loop in more convenient for programer than syntax of while and do while loop.

Syntax of for loop:-

for(initialization;condition;updation){
  // code
}

For loop program in java:-

package learn;

public class For {

	public static void main(String[] args) {
	
		for(int i=1;i<=100;i++) {
			System.out.println(i);
		}
	}

}

In the above java code we are priniting numbers from 1 to 100 using java code.

For each loop in java

for each loop in java is used to iterate list of data like array, collections etc. In following example we are iteratting an int array using for each loop

for each loop java example –

package gangforcode;

public class For {

	public static void main(String[] args) {
	int[]  array = {1,5,3,63,6,64,363,246};
		for(int i :array)
		{
			System.out.println(i);
		}
	}
}

for each loop java output-

for each loop java

Nested loop

When we use loop inside a loop is know as nested loop. Nested loop is used in multiple cases like taking input for multi dimension array , searching, sorting and many other operations.

Nested loop Example

package learn;

public class For {

	public static void main(String[] args) {
		for(int i=1;i<=10;i++)
		{
			for(int j=1;j<=10;j++)
			{
				System.out.print(i*j+" ");
			}
			System.out.println();
		}
	}
}

In the above java code we are using nested for loop for printing table from 1 to 10.

Similar Java Tutorials

Hello World – https://gangforcode.com/java-code-to-print-hello-world/
data types in java – https://gangforcode.com/data-types-in-java/
identifiers in java – https://gangforcode.com/identifiers-in-java/
keywords in java – https://gangforcode.com/keywords-in-java/
Multidimensional Array in Java – https://gangforcode.com/multidimensional-array-in-java/
Matrix Addition in Java – https://gangforcode.com/matrix-addition-in-java/
Binary Search in Java – https://gangforcode.com/binary-search-in-java/
first non repeating character – https://gangforcode.com/fiirst-non-repeating-character-in-the-string/
Armstrong number in java – https://gangforcode.com/armstrong-number-in-java/
All Armstrong Numbers – https://gangforcode.com/all-armstrong-numbers/

2 thoughts on “Loops In Java”

Leave a Comment