What is a Loop?
A loop is a programming construct that allows you to execute a block of code repeatedly based on a
condition. Loops reduce redundancy and improve code efficiency, especially when the same
operation needs to be performed multiple times.
1. For Loop
Detailed Explanation:
Purpose: Used when the number of iterations (repetitions) is known beforehand.
How It Works:
1. The initialization step runs once before the loop starts. It sets up the loop control
variable (e.g., int i = 0).
2. The condition is checked before each iteration:
If true, the loop body executes.
If false, the loop stops.
3. The increment/decrement step runs after the loop body to update the loop control
variable.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute repeatedly
}
Flowchart:
1. Initialize the loop variable.
2. Check the condition:
o If true, execute the loop body.
o If false, exit the loop.
3. Update the loop variable (increment or decrement).
4. Repeat steps 2 and 3 until the condition becomes false.
Example 1: Print Numbers from 1 to 5
public class ForLoopExample {
, public static void main() {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Example 2: Calculate the Sum of First 10 Natural Numbers
public class SumExample {
public static void main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i; // sum = sum + i
}
System.out.println("Sum of first 10 numbers: " + sum);
}
}
Output:
Sum of first 10 numbers: 55
Practical Uses of For Loop:
Printing multiplication tables.
Calculating the factorial of a number.
Repeating an operation a fixed number of times.
A loop is a programming construct that allows you to execute a block of code repeatedly based on a
condition. Loops reduce redundancy and improve code efficiency, especially when the same
operation needs to be performed multiple times.
1. For Loop
Detailed Explanation:
Purpose: Used when the number of iterations (repetitions) is known beforehand.
How It Works:
1. The initialization step runs once before the loop starts. It sets up the loop control
variable (e.g., int i = 0).
2. The condition is checked before each iteration:
If true, the loop body executes.
If false, the loop stops.
3. The increment/decrement step runs after the loop body to update the loop control
variable.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute repeatedly
}
Flowchart:
1. Initialize the loop variable.
2. Check the condition:
o If true, execute the loop body.
o If false, exit the loop.
3. Update the loop variable (increment or decrement).
4. Repeat steps 2 and 3 until the condition becomes false.
Example 1: Print Numbers from 1 to 5
public class ForLoopExample {
, public static void main() {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Example 2: Calculate the Sum of First 10 Natural Numbers
public class SumExample {
public static void main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i; // sum = sum + i
}
System.out.println("Sum of first 10 numbers: " + sum);
}
}
Output:
Sum of first 10 numbers: 55
Practical Uses of For Loop:
Printing multiplication tables.
Calculating the factorial of a number.
Repeating an operation a fixed number of times.