# C语言循环的实现

`在C语言中采用3中语法来实现循环，它们分别是while、for、do while，本文将分别说明这三种循环的实现，并对它们的运行效率进行比较。`

# do while

`首先来看do while的实现：下面是简单的代码：`
```int nCount = 0;
int nMax = 10;
do
{
nCount++;
} while (nCount < nMax);
return 0;   ```

```9:        int nCount = 0;
00401268   mov         dword ptr [ebp-4],0
10:       int nMax = 10;
0040126F   mov         dword ptr [ebp-8],0Ah
11:       do
12:       {
13:           nCount++;
00401276   mov         eax,dword ptr [ebp-4]
0040127C   mov         dword ptr [ebp-4],eax
14:       } while (nCount < nMax);
0040127F   mov         ecx,dword ptr [ebp-4];exc = nCount
00401282   cmp         ecx,dword ptr [ebp-8];比较nCount 和 nMax的值
00401285   jl          main+26h (00401276);跳转到循环体中
15:       return 0;
00401287   xor         eax,eax```

```    int nCount = 0;
int nMax = 10;
__WHILE:
nCount++;
if(nCount < nMax)
goto __WHILE;```

# while循环

```    int nCount = 0;
int nMax = 10;
while (nCount < nMax)
{
nCount++;
}```
```00401268   mov         dword ptr [ebp-4],0
10:       int nMax = 10;
0040126F   mov         dword ptr [ebp-8],0Ah
11:       while (nCount < nMax)
00401276   mov         eax,dword ptr [ebp-4]
00401279   cmp         eax,dword ptr [ebp-8]
0040127C   jge         main+39h (00401289)
12:       {
13:           nCount++;
0040127E   mov         ecx,dword ptr [ebp-4]
00401284   mov         dword ptr [ebp-4],ecx
14:       }
00401287   jmp         main+26h (00401276)
15:       return 0;
00401289   xor         eax,eax```

# for 循环

for循环是首先进行初始化操作然后进行比较，条件满足时执行循环，再将循环变量递增，最后再次比较，执行循环或者跳出。下面是for的简单例子：

```    int nMax = 10;
for (int i = 0; i < nMax; i++)
{
printf("%d\n", i);
}```

```9:        int nMax = 10;
00401268   mov         dword ptr [ebp-4],0Ah
10:       for (int i = 0; i < nMax; i++)
0040126F   mov         dword ptr [ebp-8],0 ;初始化循环变量
00401276   jmp         main+31h (00401281);跳转到比较操作处
00401278   mov         eax,dword ptr [ebp-8]
0040127E   mov         dword ptr [ebp-8],eax;这三句话实现的是循环变量自增操作
00401281   mov         ecx,dword ptr [ebp-8];ecx = i
00401284   cmp         ecx,dword ptr [ebp-4];比较ecx与i
00401287   jge         main+4Ch (0040129c);跳转到循环体外
11:       {
12:           printf("%d\n", i);
00401289   mov         edx,dword ptr [ebp-8]
0040128C   push        edx
0040128D   push        offset string "%d\n" (0042e01c)
00401292   call        printf (00401540)
13:       }
0040129A   jmp         main+28h (00401278);跳转到i++位置
14:       return 0;
0040129C   xor         eax,eax```

```    int nMax = 10;
int i = 0;
goto __CMP;
i++;
__CMP:
if (i >= nMax)
{
goto __RETURN;
}

__LOOP:
printf("%d\n", i);
__RETURN:
return 0;```

# continue语句

`continue用于结束这次循环进入下一次循环，下面采用最复杂的for循环来说明continue语句：`
```int nMax = 10;
int i = 0;
for(;i < nMax; i++)
{
if (i == 6)
{
continue;
}
}```

```00401268   mov         dword ptr [ebp-4],0Ah
10:       int i = 0;
0040126F   mov         dword ptr [ebp-8],0
11:       for(;i < nMax; i++)
00401276   jmp         main+31h (00401281)
00401278   mov         eax,dword ptr [ebp-8]
0040127E   mov         dword ptr [ebp-8],eax
00401281   mov         ecx,dword ptr [ebp-8]
00401284   cmp         ecx,dword ptr [ebp-4]
00401287   jge         main+43h (00401293)
12:       {
13:           if (i == 6)
00401289   cmp         dword ptr [ebp-8],6;
0040128D   jne         main+41h (00401291);条件不满足组跳转到循环结束处
14:           {
15:               continue;
0040128F   jmp         main+28h (00401278)
16:           }
17:       }
00401291   jmp         main+28h (00401278)
18:       return 0;
00401293   xor         eax,eax```

164 篇文章27 人订阅

0 条评论

## 相关文章

### C++的new和delete详解

C++中如果要在堆内存中创建和销毁对象需要借助关键字new和delete来完成。比如下面的代码

10340

17220

19130

11040

22010

17830

12630

18220

22420

13820