In a programming language we have to repeat set of instruction many time as per programmer’s requirements.
To repeat instructions java provides looping statements. Looping is also known as Iterative statement
Following are types of loop in java.
1. For loop statement
2. While loop statement
3. Do while loop statement
4. For each loop (enhanced for loop)
Work of above statement are same but the have syntax difference. Let us see all of them one by one
1 For Loop in Java
for loop syntax in java
1 2 3 | for( initialization; conditionCheck;updating){ Body of loop } |
Example: Print 1 to 10 numbers using for loop in java
1 2 3 4 5 6 7 | public class ForStatement { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.println("Iteration number " + i); } } } |
In for statement first loop variable is initialized then condition is checked if condition is true body of loop is executed after that variable is updated.
Again condition is cheeked condition satisfied body of loop is executed and variable updated this process is continue until condition is false.
Variable initialization is done only once.
Output
1 2 3 4 5 6 7 8 9 10 | Iteration number 1 Iteration number 2 Iteration number 3 Iteration number 4 Iteration number 5 Iteration number 6 Iteration number 7 Iteration number 8 Iteration number 9 Iteration number 10 |
2 While Loop in Java
while loop syntax in java
1 2 3 4 | Initialization While (condition){ Body of loop; } |
In while statement only condition is checked if condition is true then the body of loop is executed loop variable initialization should be before the while statement.
Variable updating is part of body of loop.
Example: Print 1 to 10 numbers using for loop in java
1 2 3 4 5 6 7 8 9 | public class WhileStatement { public static void main(String[] args) { int i = 1; while (i <= 10) { System.out.println("Iteration Number " + i); i++; } } } |
Output
1 2 3 4 5 6 7 8 9 10 | Iteration Number 1 Iteration Number 2 Iteration Number 3 Iteration Number 4 Iteration Number 5 Iteration Number 6 Iteration Number 7 Iteration Number 8 Iteration Number 9 Iteration Number 10 |
3 do while in Java
do while loop syntax in java
1 2 3 4 | initialization; do{ Body of loop; }while (condition); |
In do loop condition is cheeked at last. First time body of loop is excuted without checking the condition after that if condition specified in while is true then body of loop is executed until condition is false.
Example: Print 1 to 10 numbers using for loop in java
1 2 3 4 5 6 7 8 9 | public class DoWhileStatement { public static void main(String[] s) { int i = 1; do { System.out.println("Iteration Number " + i); i++; } while (i <= 10); } } |
Result
1 2 3 4 5 6 7 8 9 10 | Iteration Number 1 Iteration Number 2 Iteration Number 3 Iteration Number 4 Iteration Number 5 Iteration Number 6 Iteration Number 7 Iteration Number 8 Iteration Number 9 Iteration Number 10 |
If condition is false then also do while statement is executed once.
Example: Executing do while loop once in false condition.
1 2 3 4 5 6 7 8 9 | public class DoWhileStatement1 { public static void main(String[] s) { int i = 20; do { System.out.println("Iteration Number " + i); i++; } while (i <= 10); } } |
Result
1 | Iteration Number 20 |
Here condition in do while is false then also body of loop is executed once.
4 for each loop in java
For each loop is more readable and easier then for loop.
It is used to iterate over array and collection.
Syntax of for each loop in java
1 2 3 | for(DataType variable: Elements){ //body of for each loop } |
Difference between for and for each loop is that in for loop we can access index value but not in for each.
Example: Print all array elements using for each loop
1 2 3 4 5 6 7 8 9 | public class ForEach { public static void main(String[] args) { String[] names = {"Ram", "Mohan", "Sohan", "Rohan", "Nita", "Sona"}; System.out.println("Names are "); for (String name : names) { System.out.print(name + " "); } } } |
Output
1 2 | Names are Ram Mohan Sohan Rohan Nita Sona |
Example: Iterating a student object using for each loop.
Created a class student it contains a method getStudents() to get student list.
Class forEach1 call getStudents() and store its data in studentList.
then for each loop is used to iterate the student object.
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.List; public class ForEach1 { public static void main(String[] args) { Student obj = new Student(); List<Student> studentList = obj.getStudents(); System.out.println("Student Details are "); for (Student student : studentList) { System.out.println("RollNo=" + student.getRollNo() + " Name= " + student.getName() + " Class= " + student.getClass1() + " Section= " + student.getSection()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import java.util.ArrayList; import java.util.List; class Student { private int rollNo; private String name; private String class1; private String section; List<Student> studentList = new ArrayList<Student>(); public Student() { } public Student(int rollNo, String name, String class1, String section) { this.rollNo = rollNo; this.name = name; this.class1 = class1; this.section = section; } public List<Student> getStudents() { //String[] names = {"Ram", "Mohan", "Sohan", "Rohan", "Nita", "Sona"}; studentList.add(new Student(1, "Ram", "First Year", "A")); studentList.add(new Student(2, "Mohan", "First Year", "A")); studentList.add(new Student(3, "Sohan", "Second Year", "A")); studentList.add(new Student(4, "Rita", "First Year", "A")); studentList.add(new Student(5, "Nita", "Second Year", "A")); return studentList; } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClass1() { return class1; } public void setClass1(String class1) { this.class1 = class1; } public String getSection() { return section; } public void setSection(String section) { this.section = section; } } |
Output
1 2 3 4 5 6 | Student Details are RollNo=1 Name= Ram Class= First Year Section= A RollNo=2 Name= Mohan Class= First Year Section= A RollNo=3 Name= Sohan Class= Second Year Section= A RollNo=4 Name= Rita Class= First Year Section= A RollNo=5 Name= Nita Class= Second Year Section= A |
5 Examples on Loops
Example: Multiplication table program in java using while loop in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Scanner; public class MultiplicationTable { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number to generate its table"); int number = scanner.nextInt(); int i = 1; while (i <= 10) { System.out.println(number + " * " + i + " = " + (number * i)); i++; } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 | Enter a number to generate its table 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 |
Example: Sum of n natural numbers in java using for loop in java
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; public class NaturalNoSum { public static void main(String[] args) { int sum = 0; Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); for (int i = 1; i <= number; i++) { sum += i; } System.out.println("Sum of first " + number + " natural numbers are " + sum); } } |
Output
1 2 | 10 Sum of first 10 natural numbers are 55 |
Example Java program to check whether a given number is prime or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class PrimeNumberChecker { public static void main(String[] args) { System.out.println("Enter a number to check Prime or Not"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int i = 2, count = 0; while (i <= number / 2) { if (number % i == 0) { count++; break; } i++; } if (count == 0) { System.out.println(number + " is prime number"); } else { System.out.println(number + " is not a prime number"); } } } |
Output
1 2 3 | Enter a number to check Prime or Not 23 23 is prime number |
Read more about prime numbers
Program: Program to find sum of digits in java
1 |