Control Structures III: Repetition (Part 2) 控制结构三:循环结构(下)
Learning Objectives 学习目标
- Design counter-controlled loops using standard for statements.
- Construct nested loops to generate multi-dimensional shapes or coordinate grids.
- Control loop executions using break and continue jump statements.
- 使用标准 for 语句设计计数控制的循环。
- 构建嵌套循环来生成多维图形或坐标网格。
- 使用 break 和 continue 跳转语句控制循环执行。
2 · Counter-Controlled Loops (for)
The for statement is an efficient way to write a counter-controlled loop when the exact number of iterations is known before entry. It structures initialization, condition check, and loop variable update in a single line: 当在进入循环之前就已经确定了循环的精确次数时,for 语句是一种编写计数控制循环的有效方法。它在一行中结构化了初始化、条件检查和循环变量更新:
for (int i = 1; i <= 5; i++) {{ System.out.println("Iteration: " + i); }}
For Loop ComponentsFor 循环组成部分
| Loop Component循环组件 | Purpose作用 | Execution Frequency执行频率 |
|---|---|---|
| Initialization / 初始化 | Sets starting value for loop counter variable设置循环计数变量的起始值 | Once, before first iteration checks仅一次,在首次迭代检查前 |
| Condition Check / 条件检查 | Evaluates loop continuation criteria评估循环继续的布尔标准 | Before every single loop iteration runs每次循环迭代体运行前 |
| Update (Increment) / 变量更新 | Modifies loop variable (adds/subtracts value)修改循环变量(增加/减少值) | After every single loop iteration completes每次循环迭代体运行完毕后 |
3 · Nested Loops Logic
A nested loop is a loop defined inside the body of another loop. The inner loop executes all its iterations for every single iteration of the outer loop. This is useful for grid layouts: 嵌套循环是定义在另一个循环体内部的循环。外层循环每执行一次,内层循环都会完整地运行其所有迭代。这在网格布局中非常有用:
for (int row = 1; row <= 3; row++) {{ for (int col = 1; col <= 4; col++) {{ System.out.print("* "); }} System.out.println(); // Newline after each row }}
4 · Loop Control (break & continue)
Java provides keywords to alter standard loop executions: Java 提供了修改标准循环执行流程的关键字:
- break: Immediately terminates loop execution and jumps to code following the loop.立即终止循环执行,并跳转到循环体后面的代码。
- continue: Skips the remaining statements in the current iteration and jumps to the update/condition check.跳过当前循环中剩余的语句,并直接跳转到下一次循环的更新/条件检查。
Applied Case Study: Bureau Aggregators应用案例研究:统计局数据聚合器
Company: Kompas Statistics Bureau.
We design mathematical statistics engines that process inputs dynamically. Let's inspect calculations in the tabs.
公司: Kompas Statistics Bureau。
我们设计动态处理输入的数理统计引擎。让我们在选项卡中检查计算逻辑。
Summing Odd Integers from 10 Inputs计算10个输入中的奇数和
import java.util.Scanner; public class OddSum {{ public static void main(String[] args) {{ Scanner input = new Scanner(System.in); int sum = 0; System.out.println("Enter 10 integers:"); for (int i = 1; i <= 10; i++) {{ System.out.print("Input " + i + ": "); int num = input.nextInt(); if (num % 2 != 0) {{ sum += num; // Sum only odd }} }} System.out.println("Sum of odd integers: " + sum); input.close(); }} }}
Summing Positive Even Integers from 15 Inputs计算15个输入中的正偶数和
import java.util.Scanner; public class PositiveEvenSum {{ public static void main(String[] args) {{ Scanner input = new Scanner(System.in); int sum = 0; System.out.println("Enter 15 integers:"); for (int i = 1; i <= 15; i++) {{ System.out.print("Input " + i + ": "); int num = input.nextInt(); if (num > 0 && num % 2 == 0) {{ sum += num; // Sum positive evens only }} }} System.out.println("Sum of positive even integers: " + sum); input.close(); }} }}
Check Your Understanding自我检测
Q1. Which statement causes loop execution to skip remaining code inside the current iteration and proceed to the next update?哪个语句使循环执行跳过当前迭代中的剩余代码并继续下一次更新?
Q2. How many times will the inner print execute in this segment: for(int i=0; i<3; i++) for(int j=0; j<4; j++)?在以下片段中,内层打印将执行多少次:for(int i=0; i<3; i++) for(int j=0; j<4; j++)?
Q3. In Kompas Statistics Bureau OddSum program, what remainder calculation determines if value is odd?在 Kompas Statistics Bureau 的 OddSum 程序中,什么余数计算确定该值是否为奇数?