← AP CSP Course← AP CSP 课程 ← Back to Study Guide← 返回学习指南

Traverse a list by index按下标遍历列表

Run one AP CSP pseudocode statement or condition check at a time. Watch nums, i, and total change.每次执行一条 AP CSP 伪代码语句或检查一次条件,观察 nums、i、total 的变化。

Code代码

  1. nums ← [5, 8, 2, 7]
  2. i ← 1
  3. total ← 0
  4. REPEAT LENGTH(nums) TIMES {
  5. total ← total + nums[i]
  6. i ← i + 1
  7. }
  8. DISPLAY(total)

State状态

Variable变量Value值
nums[5, 8, 2, 7]
i—
total—
Condition条件—
Output输出—

Use ← and → to step. The note explains each comparison and change.使用 ← 和 → 逐步运行。每次比较和变化见下方说明。