以下为个人学习笔记整理。参考书籍《C++ Primer Plus》
# 循环和关系表达式
# for 循环
for(int i = 0; i < 100; i++){ | |
// do something... | |
} |
- 基于范围的 for 循环
int lst[100] = {1,2,3,4,5,6,7,8,9,0}; | |
// 变量元素 | |
for (int x: lst){ | |
cout << x <<endl; | |
// do something... | |
} | |
// 修改元素 | |
for (int &x: lst){ | |
x = 1; | |
// do something... | |
} | |
for (int x:{1,2,3,4,5,6,7,8,9,0}){ | |
// do something... | |
} |
# while 循环
int i = 0; | |
while(i++ < 100){ | |
// do something... | |
} |
- 常见的获取输入的做法:
char ch; | |
cin.get(ch); | |
while(!cin.fail()){ | |
// do something... | |
cin.get(ch); | |
} |
# do while 循环
int i = 0; | |
do{ | |
// do something... | |
}while(i++ < 100) |