for(;;)的解释
来源:互联网 发布:学术ppt知乎 编辑:程序博客网 时间:2024/05/16 10:10
通常死循环会这样写
for(;;)
无限循环
理论上讲,任何一个循环语句都可以达到死循环的目的,比如 (代码取自udk2014)
/** Executes an infinite loop. Forces the CPU to execute an infinite loop. A debugger may be used to skip past the loop and the code that follows the loop must execute properly. This implies that the infinite loop must not cause the code that follow it to be optimized away.**/CpuDeadLoop ( VOID ){ volatile UINTN Index; for (Index = 0; Index == 0;);}
一般我们是用for 来达到这个目的,当for 循环的判断条件(conditional expression)为空的时候,就是无限循环。
就像:
for(; ; ) printf("列个循环会永远跑下克.\n");
when the conditional expression is absent. it is assumed to be true.
原因是这样子的,当判断条件没有时,C语言认为这个条件一直为真,当然你可能还有
初始化的语句以及自增语句,但是C程序员通常使用for(;;) 来做成一个无限循环。
事实上,for(;;) 并不能保证无限循环,因为有break 语句,任何时候,遇到break语
句,都会立即跳出循环。
举个例子:
#include <stdio.h>main(){char ch = '\0';for(;;)for (; ; ) {ch = getchar(); /* get a character */if (ch == 'A') break; /* exit the loop */}printf("you typed an A");}
这个程序会一直跑下去,直到你在键盘上按了一个A。
for 语句是这样的
for (initialization; condition; increment)
statement;
initialization 语句只会执行一次,condition 在每一次循环之前都会去执行一次,当条件不成立的时候,for 循环结束,
increment 语句每次在每一次cicle 结束的时候执行一次,如果statement 里面没有continue, 那么
for (initialization; condition; increment) statement
相当于
initialization;
while (condition) {
statement
increment;
}
在最开始的例子里面, 因为Index 始终等于0,相当于while (1) {}
所以,做到了DeapLoop.
- for(;;)的解释
- select … for update 的解释概念
- 黑马程序员 for循环的条件的解释
- 一个简单的双重for循环的汇编语句解释
- 简单的双重for循环的汇编解释
- DJango 中 {% for %}解释
- u-boot for s5pv210中copy_uboot_to_ram的一点解释
- C++ for循环语句解释
- for (Element e : elementList) 解释
- for(Object obj : objs) 解释
- Java中foreach用法,增强型的for循环的样例和解释
- 关于Tomcat: The value for the useBean class attribute is invalid 问题的另类解释
- Java中增强型for循环的样例和解释
- Java中增强型for循环的样例和解释
- Java中增强型for循环的样例和解释
- Java中增强型for循环的样例和解释
- 关于No result defined for action and result input的错误解释
- select ......for update 语句解释 (文章中有些地方描述的不是很准确)
- 工业产品的颜色设计
- How to kill Active SQL Connections
- python先进先出Queue
- MFC中的运行时类型识别(第二篇)
- java web 跑例子的点滴
- for(;;)的解释
- Linux命令(四)——文件查看与文件权限、属性设置
- 【机房重构】——存储过程和触发器
- Hibernate中使用Threadlocal创建线程安全的Session
- UFT12.02安装包
- [leetcode 64] Minimum Path Sum
- Ubuntu(Linux)系统下发热及电源管理得问题
- WSAEventSelect模型的端口映射。 另外一种WSAEventSelect模型的端口映射。WSAEventSelect模型的端口映射。 另外一种WSAEventSelect模型的端口映射。
- LEETCODE: Valid Palindrome