Do While循环语法 [英] Do While loop syntax

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

问题描述

快速提问,在

结束时强制分号是什么意思?见下面的例子:


x = 0;

do

{

x = x + 1;

} while(x <3);


在(x <3)之后有分号是什么意思?为什么

编译器不能确定'是while语句的结尾而不需要分号的



谢谢。

Quick question, what is the point for forcing the semicolon at the end of
the while statement? See example below:

x = 0;
do
{
x = x + 1;
}while (x < 3);

What''s the point of having the semicolon after the (x < 3)? Why can''t the
compiler figure out that''s the end of the while statement without the need
for the semicolon?

Thank you.

推荐答案

Rene,


主要是因为声明可以像这样使用:


while(x <3)

{


}


因为语句或语句块不是

do / while语句的最后一部分,所以你需要使用分号。

-

- Nicholas Paldino [.NET / C#MVP]

- mv *@spam.guard.caspershouse.com


" Rene" < a@b.cwrote in message

news:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
Rene,

Mostly because the while statement can be used like so:

while (x < 3)
{

}

Because the statement or statement block is not the last part of the
do/while statement, you need the semi-colon.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

快速提问,在

结束时强制分号是什么意思?见下面的例子:


x = 0;

do

{

x = x + 1;

} while(x <3);


在(x <3)之后有分号是什么意思?为什么

编译器不能确定'是while语句的结尾而不需要分号的



谢谢。
Quick question, what is the point for forcing the semicolon at the end of
the while statement? See example below:

x = 0;
do
{
x = x + 1;
}while (x < 3);

What''s the point of having the semicolon after the (x < 3)? Why can''t the
compiler figure out that''s the end of the while statement without the need
for the semicolon?

Thank you.



谢谢Nicholas,


所以你是说那个是不是真的有必要,但语言规则

要求你应该这样,所以你必须这样做?或类似的东西?


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:%2 **************** @ TK2MSFTNGP06.phx.gbl .. 。
Thanks Nicholas,

So are you saying that is not really necessary but the language rules
dictate that you should so therefore you have to? or something like that?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...

Rene,


主要是因为while语句可以像这样使用:


while(x <3)

{


}


因为声明或声明block不是

do / while语句的最后一部分,你需要使用分号。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Rene" < a@b.cwrote in message

news:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
Rene,

Mostly because the while statement can be used like so:

while (x < 3)
{

}

Because the statement or statement block is not the last part of the
do/while statement, you need the semi-colon.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

>快速问题,在
while语句结束时强制分号有什么意义?见下面的例子:

x = 0;
执行
{
x = x + 1;
} while(x <3);

在(x <3)之后有分号是什么意思?为什么
编译器不能确定'没有分号需要的while语句结束?

谢谢。
>Quick question, what is the point for forcing the semicolon at the end of
the while statement? See example below:

x = 0;
do
{
x = x + 1;
}while (x < 3);

What''s the point of having the semicolon after the (x < 3)? Why can''t the
compiler figure out that''s the end of the while statement without the
need for the semicolon?

Thank you.




Rene,


嗯,我觉得这是必要的,因为如果你没有终止,你这么做是b $ b做了这样的事情:


x = 0;



{

x = x + 1;

}而(x <3)


//模糊代码块

{

Console.ReadLine();

}


编译器假设的是什么做?它是否将模糊的

代码块与上面的while语句相关联?


这就是需要分号的原因。
-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Rene < a@b.cwrote in message

news:eI ************** @ TK2MSFTNGP03.phx.gbl ...
Rene,

Well, I feel it is necessary because if you didn''t terminate, and you
did something like this:

x = 0;
do
{
x = x + 1;
} while (x < 3)

// Ambiguous code block
{
Console.ReadLine();
}

What is the compiler suppose to do? Does it associate the ambiguous
code block with the while statement above it?

That''s why the semi-colon is needed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...

谢谢Nicholas,


所以你说这不是真的有必要但是语言规则

要求你应该如此所以你必须?或类似的东西?


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.comwrote
消息新闻中的
:%2 **************** @ TK2MSFTNGP06.phx.gbl .. 。
Thanks Nicholas,

So are you saying that is not really necessary but the language rules
dictate that you should so therefore you have to? or something like that?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP06.phx.gbl...

> Rene,

主要是因为while语句可以这样使用:

while( x< 3)



因为语句或语句块不是
do / while语句的最后一部分,所以需要分号。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

Rene < a@b.cwrote in message
新闻:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
>Rene,

Mostly because the while statement can be used like so:

while (x < 3)
{

}

Because the statement or statement block is not the last part of the
do/while statement, you need the semi-colon.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rene" <a@b.cwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

>>快速问题,在while语句的末尾强制分号有什么意义?见下面的例子:

x = 0;
执行
{
x = x + 1;
} while(x <3);

在(x <3)之后有分号是什么意思?为什么不能编译器弄清楚'没有
需要分号的'while语句的结束?

谢谢。
>>Quick question, what is the point for forcing the semicolon at the end
of the while statement? See example below:

x = 0;
do
{
x = x + 1;
}while (x < 3);

What''s the point of having the semicolon after the (x < 3)? Why can''t
the compiler figure out that''s the end of the while statement without
the need for the semicolon?

Thank you.





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

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