1、for循环
for循环主要完成以下部分:1、设置初始值 2、判断循环是否继续进行 3、执行循环操作 4、更新用于测试的值
for(初始值;测试条件;更新测试值)//初始值可以 自定义变量 int x = 0
for循环中更新值:i++ i– 也可以选择不同步长 i = i+2 i=i+4
2、递增运算符 ++ 和 递减运算符 —
a++ :先使用a,在a自增 ++a :先a自加一,在使用a
![](http://139.9.1.231/wp-content/uploads/2022/01/image-103-1024x290.png)
for循环语句块:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-104.png)
字符串的比较:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-105-1024x236.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-106-1024x379.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-107-1024x370.png)
string类字符串比较:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-108-1024x276.png)
3、while循环
while(循环条件){
body
}
![](http://139.9.1.231/wp-content/uploads/2022/01/image-109.png)
4、类型别名
方法一:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-110-1024x91.png)
方法二:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-111-1024x164.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-112-1024x117.png)
5、do while 循环
![](http://139.9.1.231/wp-content/uploads/2022/01/image-113-1024x275.png)
基于范围的循环 (C++11 新增)
用于数组或者容器类,如vector 和 array。
for ( int x : array) { body }
如果要在循环体对 x进行修改:for (int &x : arrayname) 需要使用引用。
![](http://139.9.1.231/wp-content/uploads/2022/01/image-114-1024x486.png)
循环和文本输入:
1、使用原始cin输入:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-115.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-116-1024x401.png)
补救:使用cin.get(char):会读取空格,并赋给char
![](http://139.9.1.231/wp-content/uploads/2022/01/image-117-1024x103.png)
文件尾条件 EOF
![](http://139.9.1.231/wp-content/uploads/2022/01/image-126-1024x292.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-119-1024x231.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-120.png)
![](http://139.9.1.231/wp-content/uploads/2022/01/image-121.png)
二维数组
初始化:
![](http://139.9.1.231/wp-content/uploads/2022/01/image-125-1024x327.png)
使用二维数组: maxtemps[1][2]