Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Looping statements in Java are used to repeatedly execute a block of code as long as a specified condition is met. Java supports several types of loops: for, while, and do-while. Here’s a detailed explanation of each:

1. for Loop

The for loop is typically used when the number of iterations is known beforehand. It consists of three parts:

  • Initialization: This is executed once at the beginning of the loop.
  • Condition: The loop continues to execute as long as this condition is true.
  • Update: This is executed after each iteration of the loop.

Syntax:

				
					for (initialization; condition; update) {
    // code to be executed
}

				
			

Example:

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

				
			

In this example:

  • Initialization: int i = 0
  • Condition: i < 5
  • Update: i++
  • The loop will print “Iteration: ” followed by the current value of i, from 0 to 4.

2. while Loop

The while loop is used when the number of iterations is not known and the loop should continue until a specified condition becomes false.

Syntax:

				
					while (condition) {
    // code to be executed
}

				
			

Example:

				
					int i = 0;
while (i < 5) {
    System.out.println("Iteration: " + i);
    i++;
}

				
			

In this example, the loop will print “Iteration: ” followed by the current value of ‘ i ‘, from 0 to 4. The loop continues as long as ‘ i < 5 ‘.

3. do-while Loop

The do-while loop is similar to the  ‘ while ‘ loop, but the key difference is that the block of code is executed at least once before the condition is tested. This is because the condition is evaluated after the execution of the loop body.

Syntax:

				
					do {
    // code to be executed
} while (condition);

				
			

Example:

				
					int i = 0;
do {
    System.out.println("Iteration: " + i);
    i++;
} while (i < 5);

				
			

In this example, the loop will print “Iteration: ” followed by the current value of ‘i‘, from 0 to 4. The loop continues as long as ‘i < 5‘. Even if the initial value of ‘i‘ were 5 or greater, the loop would execute once before checking the condition.

Key Differences

  • for vs. while:

    • for loop is preferred when the number of iterations is known.
    • while loop is used when the number of iterations is not known and the loop should continue until a specific condition is met.
  • while vs. do-while:

    • while loop checks the condition before executing the loop body.
    • do-while loop checks the condition after executing the loop body, ensuring the loop body is executed at least once.

Practical Considerations

  • Infinite Loops: Care must be taken to ensure that loops terminate. For example, if the condition in a while loop always evaluates to true, or the update in a for loop doesn’t eventually make the condition false, the loop will run indefinitely.
  • Break and Continue: Java provides break to exit a loop prematurely and continue to skip the current iteration and proceed to the next iteration of the loop.

Example with break and continue:

				
					for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i is 5
    }
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    System.out.println("Iteration: " + i);
}

				
			

In this example, the loop will print odd numbers from 1 to 3 and then exit when i equals 5.

Understanding these looping constructs and their appropriate use cases will help you write efficient and effective Java programs.

Scroll to Top