Loops are fundamental programming constructs that allow you to execute a block of code repeatedly. In Java, there are three main types of loops: for loops, while loops, and do-while loops. For AP Computer Science A, you’ll primarily focus on for loops and while loops.
A for loop is used to execute a block of code repeatedly based on a specified number of iterations. The syntax for a for loop is as follows:
Syntax:
for (initialization; condition; iteration) {
// Code to be executed
}
Example:
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
In this example, the for loop initializes the variable i
to 0, checks if i
is less than 10, and increments i
by 1 after each iteration. The code inside the loop is then executed 10 times, with the value of i
changing from 0 to 9 in each iteration.
- int i = 0 is the initialization part.
- i < 5 is the condition that must be true for the loop to continue.
- i++ is the iteration part that increments i by 1 after each iteration.
A while loop is used to execute a block of code repeatedly as long as a specified condition is true. The syntax for a while loop is as follows:
Syntax:
while (condition) {
// Code to be executed
}
Example:
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
In this example, the while loop checks if i
is less than 10. If it is, the code inside the loop is executed, and i
is then incremented by 1. This process repeats until i
is no longer less than 10.
- i < 5 is the condition that must be true for the loop to continue.
- i++ is the iteration part that increments i by 1 after each iteration.
- Key Differences and Use Cases
- For Loop: Use when the number of iterations is known beforehand. It is often used with arrays or collections where the size is fixed.
- While Loop: Use when the number of iterations is not known beforehand. It is often used in situations where the loop termination depends on a condition that changes over time.
A do-while loop is similar to a while loop, but it guarantees that the code inside the loop is executed at least once. The syntax for a do-while loop is as follows:
Syntax:
do {
// Code to be executed
} while (condition);
Example:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
In this example, the do-while loop checks if i
is less than 10. If it is, the code inside the loop is executed, and i
is then incremented by 1. This process repeats until i
is no longer less than 10. The loop then terminates, and the code after the loop is executed.
- Use a for loop when you know the number of iterations in advance
- Use a while loop when you don’t know how many times the loop will execute
- Do-While Loop: Use when the loop body must be executed at least once
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]); // prints 1, 2, 3, 4, 5
}
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
int[] numbers = {1, 5, 3, 9, 7};
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("Max: " + max);
int[] numbers = {1, 2, 3, 4, 5};
int count = 0;
for (int i = 0; i < numbers.length; i++) {
count++;
}
System.out.println("Count: " + count);
int[] numbers = {1, 2, 3, 4, 5};
int index = -1;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 3) {
index = i;
break;
}
}
System.out.println("Index: " + index);
- break: Exits the loop immediately
- continue: Skips the rest of the current iteration and moves to the next
Example:
for (int i = 0; i < 10; i++) {
if (i == 3) {
continue; // Skip printing 3
}
if (i == 5) {
break; // exit the loop
}
System.out.println(i);
}
Output:
0
1
2
4
A) for (int i = 0; i < 10; i++) B) for (int i = 0; i < 10; i–) C) for (int i = 0; i > 10; i++) D) for (int i = 0; i < 10; i = i + 2)
Answer
Answer: A
int i = 0;
while (i < 5) {
System.out.print(i + " ");
i++;
}
A) 0 1 2 3 4
B) 1 2 3 4 5
C) 0 1 2 3 4 5
D) 1 2 3 4
Answer
Answer: A
A) for loop
B) while loop
C) Both are equally appropriate
D) Neither
Answer
Answer: A
A) It requires a known number of iterations.
B) It checks the condition after executing the loop body.
C) It is more flexible when the number of iterations is not known beforehand.
D) It cannot be used to iterate over a collection.
Answer
Answer: C
int i = 0; do { System.out.print(i + " "); i++; } while (i < 5);
A) 0 1 2 3 4
B) 1 2 3 4 5
C) 0 1 2 3 4 5
D) 1 2 3 4
Answer
Answer: A Explanation: The do-while loop executes the block of code at least once before checking the condition. It starts with i equal to 0, prints i, increments i by 1, and continues until i is no longer less than 5, resulting in the output 0 1 2 3 4.
A) for loop
B) while loop
C) do-while loop
D) Any of the above >> <summary>Answer</summary> > Answer: C > Explanation:do-while
loop should be used when you need to ensure that the loop body is executed at least once because it checks the condition after executing the loop body.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.print("(" + i + "," + j + ") ");
}
}
public class FactorCounter {
public static int countFactors(int n) {
// Your code here
}
public static void main(String[] args) {
System.out.println(countFactors(12)); // Should print 6
System.out.println(countFactors(7)); // Should print 2
}
}