“做什么"是什么意思在这里吗?(java) [英] what does "do" do here? (java)
问题描述
我在某处的Interent上看到了这段代码.我想知道 do
是做什么的.
I saw this bit of code on the interents somewhere. I'm wondering what the do
is for.
public class LoopControl {
public static void main(String[] args) {
int count = 0;
do {
if (count % 2 == 0) {
for (int j = 0; j < count; j++) {
System.out.print(j+1);
if (j < count-1) {
System.out.print(", ");
}
}
System.out.println();
}
count++;
}
while (count <= 5);
}
}
我的意思是的确切含义
是什么?它的作用是什么?任何其他信息也将有用.
By which I mean what exactly does do
mean? What's its function? Any other information would be useful, too.
推荐答案
这是一个do-while循环.因此,当count小于或等于5时,它将在接下来的块中执行所有操作.此计数与正常的while循环之间的区别是,条件是在循环结束而不是开始时求值的.因此确保该循环至少执行一次.
It is a do-while loop. So it will do everything in the following block while count is less than or equal to 5. The difference between this and a normal while loop is that the condition is evaluated at the end of the loop not the start. So the loop is guarenteed to execute at least once.
哦,在这种情况下,它将打印:
Oh, and in this case it will print:
1, 2
1, 2, 3, 4
只是您知道一开始还会有新行,但是格式似乎没有让我证明这一点.
just so you know there will also be a new line at the start, but the formatting doesn't seem to let me show that.
这篇关于“做什么"是什么意思在这里吗?(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!