Iterative Constructs IN JAVA
Iterative Constructs IN JAVA
Introduc on
Itera ve constructs, also called loops, are used to execute a block of code repeatedly as long
as a given condi on is true.
Defini on
Jump Statements
con nue: Skips the current itera on and moves to the next itera on.
Syntax of Loops
}
Example:
System.out.println(i);
Example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
do {
Example:
int i = 1;
do {
System.out.println(i);
i++;
Interconversion of Loops
1. For to While:
// For loop
System.out.println(i);
}
// Equivalent While loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
2. While to Do-While:
// While loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
int i = 1;
do {
System.out.println(i);
i++;
while (true) {
System.out.println("Infinite Loop");
}
Delay Loop
Example:
Counter Variables
Example:
int counter = 0;
counter++;
int i = 1;
while (i <= 5) {
i++;
java
Copy code
int i = 1;
do {
i++;
Key Points
2. Use con nue to skip the rest of the loop body for the current itera on.
3. Entry controlled loops are preferred when the number of itera ons is known.
4. Exit controlled loops are preferred when the loop must execute at least once.