n之间的差异 [英] Difference between for n while

查看:65
本文介绍了n之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁可以告诉我循环和while循环的区别

执行?

____________________

for(i = 0; i< 10; i ++)

; / *什么都不做* /


打印i;


___________________

i = 0;

while(i< 10){

i ++;

}

打印i;

____________________


wat将是输出?

请说明你的答案

解决方案

ro********@gmail.com 写道:
< blockquote class =post_quotes>
谁能告诉我循环和while循环的区别

执行?

____________________

for(i = 0; i< 10; i ++)

; / *什么都不做* /


打印i;


___________________

i = 0;

while(i< 10){

i ++;

}

打印i;

____________________


wat将是输出?

请表示你的答案






您的代码不能在c中工作。


请尝试以下代码。


#include < stdio.h>


int main(void){

int i = 0,ii = 0;

while (i< 10){

i ++;

}

printf("%d \ n",i);


for(ii = 0; ii< 10; ii ++)

; / *什么都不做* /


printf("%d \ n",ii);

返回0;

}


对于两个循环,上面的答案是10。


原因是它达到9然后在循环中递增并且当它返回时它是
然后循环停止因为它

不小于10.


亲切的问候,

Anthony Irwin


Anthony Irwin写道:

ro ******** @ gmail.com 写道:


>有人能告诉我循环和while循环执行的区别吗?
____________________
for(i = 0; i< 10; i ++)
; / *什么都不做* /

打印i;

___________________
i = 0;
while(i< 10){
i ++ ;
}
打印i;
____________________

wat会输出吗?
请表示你的答案



实际上这个程序可能会告诉你更多。


#include< stdio.h>


main(){

int i = 0,ii = 0;

while(i< 10){

printf(" Inside循环:%d \ n",i);

i ++;

}


printf(" Outside Loop: %d \ n",i);


for(ii = 0; ii< 10; ii ++)

printf(" Inside Loop: %d \ n",ii);


printf(外环:%d \ n,ii);

返回0;

}


以下输出:


内循环:0

内循环:1

内循环:2

内循环:3

内循环:4

内循环:5
内循环:6

内循环:7

内循环:8

内循环:9
外循环:10

内循环:0

内循环:1

内循环:2

内循环:3

内循环:4

内循环:5

内循环:6

内循环:7

内循环:8

内循环:9

外循环:10


看看循环外部循环永远不会达到10循环。

循环从不执行时为10。


ii ++增量循环在for循环中完成之后。


只需玩一些代码并编译它,你会学到很多东西。


亲切的问候,

Anthony Irwin


非常感谢


Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/

print i;

___________________
i=0;
while (i<10) {
i++;
}
print i;
____________________

wat will be the output?
please expain ur ans

解决方案

ro********@gmail.com wrote:

Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/

print i;

___________________
i=0;
while (i<10) {
i++;
}
print i;
____________________

wat will be the output?
please expain ur ans

Hi,

You code won''t work in c.

Try the following instead.

#include <stdio.h>

int main(void) {
int i=0, ii = 0;
while (i<10) {
i++;
}
printf("%d\n", i);

for (ii=0 ; ii <10 ; ii++)
; /* Do nothing*/

printf("%d\n", ii);
return 0;
}

The answer to the above is 10 for both loops.

The reason is that it reaches 9 and then increments in the loop and
when it comes back around it is 10 and then the loop stops because it
is not less then 10.

Kind Regards,
Anthony Irwin


Anthony Irwin wrote:

ro********@gmail.com wrote:

>Can anyone tell me the difference bet for loop and while loop
execution?
____________________
for (i=0 ; i<10 ; i++)
; /* Do nothing*/

print i;

___________________
i=0;
while (i<10) {
i++;
}
print i;
____________________

wat will be the output?
please expain ur ans

Actually this program will probably show you more.

#include <stdio.h>

main() {
int i=0, ii = 0;
while (i<10) {
printf("Inside Loop: %d\n", i);
i++;
}

printf("Outside Loop: %d\n", i);

for (ii=0 ; ii <10 ; ii++)
printf("Inside Loop: %d\n", ii);

printf("Outside Loop: %d\n", ii);
return 0;
}

Output below:

Inside Loop: 0
Inside Loop: 1
Inside Loop: 2
Inside Loop: 3
Inside Loop: 4
Inside Loop: 5
Inside Loop: 6
Inside Loop: 7
Inside Loop: 8
Inside Loop: 9
Outside Loop: 10
Inside Loop: 0
Inside Loop: 1
Inside Loop: 2
Inside Loop: 3
Inside Loop: 4
Inside Loop: 5
Inside Loop: 6
Inside Loop: 7
Inside Loop: 8
Inside Loop: 9
Outside Loop: 10

See how the loop never reaches 10 on the inside only outside the loop.
The loop never executes when it is 10.

the ii++ increments after the loop has complete in the for loop.

Just play with some code and compile it any you will learn a fair bit.

Kind Regards,
Anthony Irwin


Thank you so much


这篇关于n之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆