for文
- 1. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10. ____
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
while文のループが終わった状態から説明を始めます
j = 3
変数
Hello while: 0
Hello while: 1
Hello while: 2
出力
- 2. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の初期化を行います
int型のカウンタ変数iを0で初期化します
j = 3
i = 0
変数
Hello while: 0
Hello while: 1
Hello while: 2
出力
- 3. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の条件判定を行います
i < 3 は 0 < 3 でtrueなのでループの処理に入ります
j = 3
i = 0
変数
Hello while: 0
Hello while: 1
Hello while: 2
出力
- 4. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
Hello for: 0と出力します
j = 3
i = 0
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
出力
- 5. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の}に到達したので更新処理に進みます
j = 3
i = 0
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
出力
- 6. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の更新処理を行います
iの値がインクリメントされ1になります
j = 3
i = 1
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
出力
- 7. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の条件判定を行います
i < 3 は 1 < 3 でtrueなのでループの処理に入ります
j = 3
i = 1
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
出力
- 8. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
Hello for: 1と出力します
j = 3
i = 1
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
出力
- 9. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の}に到達したので更新処理に進みます
j = 3
i = 1
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
出力
- 10. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の更新処理を行います
iの値がインクリメントされ2になります
j = 3
i = 2
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
出力
- 11. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の条件判定を行います
i < 3 は 2 < 3 でtrueなのでループの処理に入ります
j = 3
i = 2
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
出力
- 12. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
Hello for: 2と出力します
j = 3
i = 2
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
Hello for: 2
出力
- 13. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の}に到達したので更新処理に進みます
j = 3
i = 2
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
Hello for: 2
出力
- 14. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の更新処理を行います
iの値がインクリメントされ3になります
j = 3
i = 3
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
Hello for: 2
出力
- 15. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
for文の条件判定を行います
i < 3 は 3 < 3 でfalseなのでループの処理を抜けます
j = 3
i = 3
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
Hello for: 2
出力
- 16. 01. #include <bits/stdc++.h>
02. using namespace std;
03.
04. int main() {
05. int j = 0;
06. while (j < 3) {
07. cout << "Hello while: " << j << endl;
08. j++;
09. }
10.
11. for (int i = 0; i < 3; i++) {
12. cout << "Hello for: " << i << endl;
13. }
14.
15. }
main関数の}に到達したのでプログラムを終了します
j = 3
変数
Hello while: 0
Hello while: 1
Hello while: 2
Hello for: 0
Hello for: 1
Hello for: 2
出力