Lesson 11
Loops in Java
Loops are control structures used to repeatedly execute a block of code until a condition becomes false. Learn about for, while, do-while, and enhanced for loops.
Lesson content
What are Loops in Java?
Loops in Java are used to execute a block of code repeatedly until a specific condition becomes false. Loops help reduce code repetition and make programs more efficient and easier to manage.
A loop allows a program to repeat a set of statements multiple times based on a condition. Loops are commonly used for tasks like printing values, processing arrays, and repeating calculations.
Definition List
- Loop: A control structure that repeatedly executes a block of code while a condition is true.
- Iteration: One complete execution of a loop block.
- Infinite Loop: A loop that never ends because its condition always remains true.
Types of Loops in Java
Java provides three main types of loops:
- for loop
- while loop
- do...while loop
Java also supports enhanced for loop and unconditional statements like break and continue.
for Loop in Java
The for loop is used when the number of iterations is known in advance.
Syntax of for Loop
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}Explanation of Statements
- Statement 1: Initialization step executed once before the loop starts.
- Statement 2: Condition checked before every iteration.
- Statement 3: Increment or decrement executed after each iteration.
Example 01
The example below prints numbers from 0 to 4.
for (int i = 0; i < 5; i++)
{
System.out.println(i);
}Output: 0 1 2 3 4
Example 02
package Looping_Stat;
import java.util.Scanner;
public class for_loop {
public static void main(String args[])
{
System.out.println("Enter The Limit : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=1; i<=n; i++)
{
System.out.println(i);
}
}
}This program takes a limit from the user and prints numbers from 1 to the entered value.
Example 03: Nested for Loop
A nested loop means placing one loop inside another loop.
package Looping_Stat;
public class nested_for {
public static void main(String args[]) {
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}This program prints a pattern of stars using nested for loops.
while Loop in Java
The while loop executes a block of code as long as the given condition remains true.
Syntax of while Loop
Initialization;
while (condition)
{
// code block to be executed
Increment/decrement;
}Example 04
int i = 0;
while (i < 5)
{
System.out.println(i);
i++;
}Output: 0 1 2 3 4
Example 05
package Looping_Stat;
import java.util.Scanner;
public class while_loop {
public static void main(String args[])
{
System.out.println("Enter The Limit : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i = 1;
while(i <= n)
{
System.out.println(i);
i++;
}
}
}This program prints numbers from 1 to the entered limit using a while loop.
do...while Loop in Java
The do...while loop executes the code block at least once before checking the condition.
Syntax of do...while Loop
Initialization;
do
{
// code block to be executed
Increment/decrement;
}
while (condition);Example 06
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);Output: 0 1 2 3 4
Example 07
package Looping_Stat;
import java.util.Scanner;
public class do_while {
public static void main(String args[])
{
System.out.println("Enter The Limit : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i = 2;
do {
System.out.println(i);
i = i + 2;
}while (i <= n);
}
}This program prints even numbers using the do...while loop.
Enhanced for Loop in Java
The enhanced for loop is used to iterate through arrays and collections more easily. It was introduced in Java 5 and is also known as the for-each loop.
Features of Enhanced for Loop
- Iterates sequentially from start to end
- Simplifies array traversal
- Reduces code complexity
Example 08
public class Enhanced_for {
public static void main(String args[])
{
int numbers[] = {10, 20, 30, 40, 50, 60, 70};
for(int n : numbers)
{
System.out.println(n);
}
}
}This program prints all elements of an array using the enhanced for loop.
Unconditional Statements in Java
Unconditional statements are used to control loop execution immediately.
break Statement
The break statement terminates the loop immediately.
continue Statement
The continue statement skips the current iteration and moves to the next iteration.
Example 09
package Looping_Stat;
public class break_continue {
public static void main(String args[]) {
for (int i = 1; i <= 10; i++) {
if(i == 5)
continue;
System.out.println(i);
if(i == 8)
break;
}
}
}- When i == 5, the continue statement skips printing the value.
- When i == 8, the break statement terminates the loop.
Advantages of Loops in Java
Reduces Code Repetition
Loops eliminate the need to write the same code multiple times.
Improves Efficiency
Loops make programs shorter and easier to maintain.
Useful for Data Processing
Loops are commonly used for arrays, collections, and repeated calculations.
FAQ
1. What are loops in Java?
Loops are control structures used to repeatedly execute a block of code until a condition becomes false.
2. What is the difference between while and do...while loops?
The while loop checks the condition before execution, while the do...while loop executes the code once before checking the condition.
3. What is the purpose of the enhanced for loop?
The enhanced for loop is used to easily iterate through arrays and collections without using indexes.