Unit 4: Iteration
AP Computer Science AWhat you'll master in this unit
- •
- •
- •
- •
- •
The three types of loops
Key loop patterns
Counting loop
java
for (int i = 0; i < 10; i++) {
System.out.println(i); // prints 0 through 9
}
Accumulator loop
java
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i; // sum = 5050
}
Sentinel loop
java
while (input != -1) {
// process input
input = scanner.nextInt();
}
String traversal
java
String word = "Hello";
for (int i = 0; i < word.length(); i++) {
String ch = word.substring(i, i + 1);
System.out.println(ch);
}