for (; ; ) { }

config_man 2014-05-11 01:33:19
A: for (; ; ) { }
B:for (; ; ) {; }
A、B两段代码执行的话,都是一个死循环。想请教下各位大神,圆括号(A、B)和花括号(B)里都是什么情况。
3ks.
...全文
484 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
config_man 2014-05-11
  • 打赏
  • 举报
回复
引用 8 楼 sp1234 的回复:
其实如果原本就是一个直接的无条件循环,而反编译工具给你生成 while (true) { } 这种语句,你还非要纠结于这里的条件部分一个true表达式是不是“空对空的对比”(你比原本画蛇添足的一个true还要复杂10倍),这就是比较尴尬的事情。是“只会一点高级语言”的程序员染上的一点病症,你如果学点低级的编程语言就可以治好这个病的。
了解了。多谢,结贴。
  • 打赏
  • 举报
回复
其实如果原本就是一个直接的无条件循环,而反编译工具给你生成 while (true) { } 这种语句,你还非要纠结于这里的条件部分一个true表达式是不是“空对空的对比”(你比原本画蛇添足的一个true还要复杂10倍),这就是比较尴尬的事情。是“只会一点高级语言”的程序员染上的一点病症,你如果学点低级的编程语言就可以治好这个病的。
  • 打赏
  • 举报
回复
引用 6 楼 config_man 的回复:
您误会了,不是说那样不对,程序能执行,怎么会不对呢?不清楚的是它是怎么进行条件判断的,是否就是用空与空进行对比的。
#3楼已经说了,在汇编层面根本没有什么while语句、for语句之类的,只有基本的if、goto(jumpto)语句。而这里,编译器并不会产生if语句,只会无条件循环。
config_man 2014-05-11
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
这是语言规范定义的。 你的困惑是,你的老师教你的不是语言规范,而是通常的写法,你认为你老师没教你的就是不对的。实际上不是这么回事。
您误会了,不是说那样不对,程序能执行,怎么会不对呢?不清楚的是它是怎么进行条件判断的,是否就是用空与空进行对比的。
threenewbee 2014-05-11
  • 打赏
  • 举报
回复
这是语言规范定义的。 你的困惑是,你的老师教你的不是语言规范,而是通常的写法,你认为你老师没教你的就是不对的。实际上不是这么回事。
config_man 2014-05-11
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
[quote=引用 2 楼 config_man 的回复:] for循环本身就是和while等价的。 for (a;b;c) d; 也可以写成 a; while (b) { d; c; }
while(b)应该是一个表达式吧,如a<b。 其实关于for我不明白的是,圆括号里啥都没有,它是怎么进行条件判断的呢(条件成立,进入for的大括号里)?比如a是空,b也是空,它会判断成空与空相等,然后再进入for的大括号里的么?
threenewbee 2014-05-11
  • 打赏
  • 举报
回复
引用 2 楼 config_man 的回复:
运行后的exe用Reflect看的话,代码为: private static void Main(string[] args) { while (true) { } } 看来编译器将其弄成了while循环。不过不知道到底是个怎么理解法
这是反编译器把它弄成了while循环,而不是编译器。 编译器就是转换为跳转指令而已。IL层面没有循环语句这一说。 ;代表语句结束,这里是一句空语句,编译器不会为它产生任何代码,所以 ; ;; ;;; ... 都是一样的 for循环本身就是和while等价的。 for (a;b;c) d; 也可以写成 a; while (b) { d; c; } 但是反编译工具稍微有些智能,它会猜测你的源代码是写的for还是while 但是如果你写 for (;;),这个它就猜错了。 类似的,你写 int i = 0; while (i < 10) i++; 这个它也会猜错,猜成 for (int i = 0; i < 10; i++);
config_man 2014-05-11
  • 打赏
  • 举报
回复
运行后的exe用Reflect看的话,代码为: private static void Main(string[] args) { while (true) { } } 看来编译器将其弄成了while循环。不过不知道到底是个怎么理解法
TensorFlow For Machine Intelligence: A hands-on introduction to learning algorithms by Sam Abrahams English | 23 July 2016 | ASIN: B01IZ43JV4 | 322 Pages | AZW3/MOBI/EPUB/PDF (conv) | 26.87 MB This book is a hands-on introduction to learning algorithms. It is for people who may know a little machine learning (or not) and who may have heard about TensorFlow, but found the documentation too daunting to approach. The learning curve is gentle and you always have some code to illustrate the math step-by-step. TensorFlow, a popular library for machine learning, embraces the innovation and community-engagement of open source, but has the support, guidance, and stability of a large corporation. Because of its multitude of strengths, TensorFlow is appropriate for individuals and businesses ranging from startups to companies as large as, well, Google. TensorFlow is currently being used for natural language processing, artificial intelligence, computer vision, and predictive analytics. TensorFlow, open sourced to the public by Google in November 2015, was made to be flexible, efficient, extensible, and portable. Computers of any shape and size can run it, from smartphones all the way up to huge computing clusters. This book starts with the absolute basics of TensorFlow. We found that most tutorials on TensorFlow start by attempting to teach both machine learning concepts and TensorFlow terminology at the same time. Here we first make sure you've had the opportunity to become comfortable with TensorFlow's mechanics and core API before covering machine learning concepts.

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧