-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgrammingTutorial05_Loop.ino
58 lines (47 loc) · 1.52 KB
/
ProgrammingTutorial05_Loop.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void setup()
{
Serial.begin(9600);
Serial.println("#### FOR ####");
// for (初期化; 継続条件; 変化式)
// つまり,
// 最初に実行される; ループを続けるかの条件; ループの最後に毎回実行される文
// ↓は,変数 "num" に,最初に 0 を代入し,ループごとに 1 を加え, 10 以下の間 {} の中を繰り返す.
for (int num = 0; num <= 10; num = num + 1) {
Serial.print("Loop1: ");
Serial.println(num);
delay(100); // 100 ms 待つ
}
Serial.println("#### FOR BREAK ####");
for (int num = 0; num <= 10; num = num + 1) {
if (num == 5) { // もし, "num" が 5 なら
break; // ループから抜ける
}
Serial.print("Loop2: ");
Serial.println(num);
delay(100);
}
Serial.println("#### WHILE ####");
int count = 0;
// while (継続条件)
// ↓は,変数 "count" が 10 未満の間 {} の中を繰り返す.
while (count < 10) {
Serial.print("Loop3: ");
Serial.println(count);
delay(100);
count = count + 1; // count に 1 を足して, count に代入する
}
Serial.println("#### WHILE BREAK ####");
count = 0;
while (1) { // 無限ループ
Serial.print("Loop4: ");
Serial.println(count);
delay(100);
count = count + 1;
if (count >= 10) { // もし, "count" が 10 以上なら
break; // ループから抜ける
}
}
}
void loop()
{
}