for c循环编程 [英] for loop in c programming
问题描述
如果我们在for循环中交换循环条件和增量表达式的位置将会发生什么(i = 0; i ++; i< 5)
和
for(i = 0; i< 5; i ++)
这两者之间有什么区别。
嘿,这是你应该知道的东西:头部用';'字符分隔的那些东西for循环是表达式,所有这三个!两个for循环编译,但这应该给出一个警告:for(i = 0; i ++; i< 5)
因为最后一个表达式没有副作用(即:它没有改变任何东西)你的编译器很聪明!正如我所说,所有三个都是表达式,但第一个用于初始化,第二个用于在每次执行循环体之前进行预测试,第三个表达式在每次执行循环体之后执行。
此c代码:
for (expr1 ; expr2; expr3)
{
code_in_the_body_of_for_loop
}
可以这样写:
expr1 // 初始化,如i = 0;编译器应该警告这个
// 没有副作用(如果它没有改变任何东西)喜欢我< 5)
而( 1 )< span class =code-comment> // 无限循环
{
if (!expr2) // 执行循环体之前的预测试
break ;
// 这是for循环中的代码,请注意
// for循环体中的continue语句
< span class =code-comment> // 跳转到expr3而不是for循环的开头!!!
code_in_the_body_of_for_loop
expr3; // 通常是一些改变循环计数器的表达式
// 如i ++或i + = 5或ptr = ptr-> parent;
// 如果没有副作用,编译器应该发出警告。
}
在你的情况下,当预测试表达式(expr2)是i ++时,for循环从不执行正文代码,因为i ++的值为零,所以它立即从你的for循环中突破!如果expr3只是i< 5
,你编译器也应该发出警告,因为你通常不会写一个像i <5;
这样的语句在你的代码中,你呢? :D
区别在于一个是有效的C而另一个根本不会编译。你为什么要问这个问题,而不仅仅是输入它,看看会发生什么?为什么在不试图让自己看的情况下问什么东西呢?
这是for循环的正确格式。
for(i = 0; i< 5; i ++)
如果您尝试使用其他提到的..
for(i = 0; i ++; i< 5)
编译器不会编译它并在此处指出错误。因为它与for循环的语法冲突。
what will happen if we exchange the postion of loop condition and increment expression in "for loop" in c
for(i=0;i++;i<5)
and
for(i=0;i<5;i++)
what is the difference betwwen these two.
Hey, here is something you should know: Those thingies separated by ';' characters in the head of the for loop are expressions, all three of them! Both for loops compile but this should give a warning:for(i=0;i++;i<5)
because the last expression doesn't have a side effect (ie: it does not change anything) and your compiler is smart! As I said all three thingies are expressions but the first is used for initialization, the second is for pre-testing before every execution of the loop-body, and the third expression is executed after each execution of the loop-body.
This c code:
for (expr1; expr2; expr3) { code_in_the_body_of_for_loop }
Can be written this way:
expr1 // initialization, like i=0; the compiler should warning if this // has no side effect (if it doesnt change anything like i<5) while (1) // infinite loop { if (!expr2) // pre testing before execution of the loop-body break; // this is the code inside the for loop, note that // a "continue" statement in the body of the for loop // jumps to expr3 and not to the beginning of the for loop!!! code_in_the_body_of_for_loop expr3; // usually some expressions that change the loop-counter // like i++ or i+=5 or ptr = ptr->parent; // the compiler should warning if this has no side effect. }
In your case when the pre-test expression (expr2) is i++ the for loop never executes the body code because the value of i++ is zero so it breaks out from your for loop immediately! You compiler should also warning if expr3 is justi<5
because you normally never write a statement likei<5;
in your code, do you? :D
The difference is that one is valid C and the other is not going to compile at all. Why would you ask this, and not just type it in and see what happens ? Why ask what something does without trying it for yourself to see ?
This is the right format in case of a for loop.
for(i=0;i<5;i++)
If you try with other one you mentioned..
for(i=0;i++;i<5)
The compiler will not compile it and point error here.Because it conflicts the grammar of a for loop.
这篇关于for c循环编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!