关于for循环的问题.......... [英] question about for loop..........

查看:102
本文介绍了关于for循环的问题..........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream.h  >  
void main()
{

for int n = 1 ; n< 4; n ++);
cout<< abc;







我的问题是,当我在循环结束时使用分号然后循环执行循环体仅一次。在这种情况下如何循环工作

解决方案

方式太简单了。



分号表示空语句 。如果你使用它,它就会成为循环的主体;所以循环重复4次但什么都不做。第二行退出循环,所以它只执行一次。



如果你删除这个分号,循环语句的正文是你的第二行;它将被执行4次。



-SA


推荐此链接



'for'循环后分号的影响 [ ^ ]



它声明:

引用:

Semicolon是一个名为null statement *的合法语句,意思是什么都不做。由于for循环执行单个操作(可能是{}中包含的块),分号被视为循环体,导致您观察到的行为。



以下代码



  for (i =  0 ; I&小于5;我++); 
{
printf( hello \ n);
}



解释如下:



重复五次 for (i =  0 ; i< 5; i ++)
... < span class =code-keyword> do nothing(分号)
打开 new 范围 for 局部变量{
...打印 hello
关闭范围}



如果我们查看你的代码

  for  int  n =  1 ; n< 4; n ++ );  //  循环四次,什么都不做。 
cout<< abc; // 执行一次





问候。 。:)


为了理解for循环结构通常有以下格式



  for (< statement> ;;< condition> ;;< statement>;)
< body statement> ;;
< / 声明 > < / 条件 > < / 声明 >





让我们举一个简单的例子,下面的for循环将执行4次主体;



  for  int  i =  1 ; i <   5 ; i ++)
cout<< i;





但在for循环结束时分号指示所述for循环没有任何正文因此不会执行cout指令作为for循环的一部分



  for  int  i =  1 ; i <   5 ; i ++)
cout<< 测试;





For循环参数是可选的,例如这对循环有效,唯一的区别是它没有声明和增量语句;



 For(; i <   5 ;)
cout< < i ++;





同样这也是有效的,但它将是一个无限循环



  for (;;)
cout<< I;


#include <iostream.h>
void main()
{

for(int n=1;n<4;n++);
cout<<"abc";




my question is that when i use semicolon at the end of loop then for loop execute body of loop only once. how for loop works in this case

解决方案

Way too simple.

Semicolon means "empty statement". If you use it, the it becomes the body of the loop; so the loop repeats 4 times but does nothing. The second line goes out of the loop, so it is only executed once.

If you remove this semicolon, the body of the loop statement is your second line; it will be executed 4 times.

—SA


Refer this link

Effect of Semicolon after 'for' loop[^]

It states that:

Quote:

Semicolon is a legitimate statement called null statement * that means "do nothing". Since the for loop executes a single operation (which could be a block enclosed in {}) semicolon is treated as the body of the loop, resulting in the behavior that you observed.

The following code

for (i=0;i<5;i++);
 {
     printf("hello\n");
 }


is interpreted as follows:

Repeat five times for (i=0;i<5;i++)
... do nothing (semicolon)
Open a new scope for local variables {
... Print "hello"
Close the scope }


If we look at your code

for(int n=1;n<4;n++);//loop four times,do nothing.
cout<<"abc";//Executed once



Regards..:)


for the sake of understanding the for loop construct usually have following format

for(<statement>;<condition>;<statement>;)
  <body statement>;
</statement></condition></statement>



Lets take a simple example, the below for loop will execute the body 4 times;

for (int i=1;i < 5;i++)
  cout << i;



but semicolon at the end of for loop instruct that the said for loop does not have any body so will not execute the cout instruction as part of for loop

for (int i=1;i < 5;i++)
  cout << "Test";



For loop parameter are optional for example this is valid for loop, the only difference is that it does not have declaration and increment statements;

For (;i < 5;)
  cout << i++;



similarly this is also valid but it will be an infinite for loop

for (;;)
  cout << i;


这篇关于关于for循环的问题..........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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