# 课程E-循环1-循环和动画

• 年龄 9+
• 65+ 分钟
• MakeCode Editor

## 介绍

### 背景知识

• 提前体验起点
• 课程 E. CS 基础这节课简短地回顾了一下之前在课程C和D之中讲授的概念。学生们将会用算法、循环、条件、事件和函数来进行编程练习。

## 课堂活动

• 1块micro:bit
• 1根USB线
• 1个AAA电池组（可选）
• 2节AAA电池（可选）

### 编写文本

``````basic.forever(() => {})
``````

### forever

``````
for(let i = 0; i < 5; ++i) {
basic.showNumber(i)
}
``````

### for

``````
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
``````

### showLeds

``````
basic.showString("Hello!")
``````

### showString

``````
basic.showNumber(0)
``````

### showNumber

``````
let item = 0
item = 0
``````

3个步骤

## 指导练习

``````
let Number_of_Times_Through_Loop = 0
let PassThroughLoop = 0
PassThroughLoop = 0
Number_of_Times_Through_Loop = 4
basic.forever(() => {
for (let PassThroughLoop = 0; PassThroughLoop
<= Number_of_Times_Through_Loop; PassThroughLoop++) {
basic.showNumber(PassThroughLoop)
}
})
``````

`Number_of_Times_Through_Loop`被设置为4. 然而，我们将会有5次循环，因为我们是从0开始的。 这虽然令人困惑，但是程序里面的很多东西都是以0为起点。了解这一点很重要。

## 挑战

### 挑战1

``````
let Number_of_Times_Through_Loop = 0
Number_of_Times_Through_Loop = 7
basic.forever(() => {
for (let PassThroughLoop = 0; PassThroughLoop <= Number_of_Times_Through_Loop; PassThroughLoop++) {
basic.showNumber(PassThroughLoop)
}
})
``````

### 挑战2

``````
let Number_of_Times_Through_Loop = 0
Number_of_Times_Through_Loop = 9
basic.forever(() => {
for (let PassThroughLoop = 0; PassThroughLoop <= Number_of_Times_Through_Loop; PassThroughLoop++) {
basic.showNumber(PassThroughLoop)
}
})
``````

### 挑战3

``````
let Number_of_Times_Through_Loop = 0
let PassThroughLoop = 0
Number_of_Times_Through_Loop = 3
basic.forever(() => {
for (let PassThroughLoop = 0; PassThroughLoop <= Number_of_Times_Through_Loop; PassThroughLoop++) {
basic.showNumber(PassThroughLoop)
}
for (let PassThroughLoop = 0; PassThroughLoop <= Number_of_Times_Through_Loop; PassThroughLoop++) {
basic.showIcon(IconNames.Heart)
}
})
``````

### 挑战4

``````
let Number_of_Times_Through_Loop = 0
let PassThroughLoop = 0
Number_of_Times_Through_Loop = 2
basic.forever(() => {
for (let PassThroughLoop = 0; PassThroughLoop <= Number_of_Times_Through_Loop; PassThroughLoop++) {
basic.showNumber(PassThroughLoop)
}
for (let PassThroughLoop = 0; PassThroughLoop <= 4; PassThroughLoop++) {
basic.showIcon(IconNames.Heart)
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
for (let PassThroughLoop = 0; PassThroughLoop <= 2; PassThroughLoop++) {
basic.showString("Go")
}
})
``````

## 问答

### 问题 2

``````
let index = 0
index = 0
basic.forever(() => {
for (let index = 0; index <= 4; index++) {
basic.showNumber(index)
}
})
``````

### 问题 3

``````
let index = 0
index = 0
basic.forever(() => {
for (let index = 0; index <= 6; index++) {
basic.showNumber(index)
}
})
``````