对于循环和停止条件 [英] For Loops and stopping conditions
问题描述
任何人都可以解释为什么答案是他们想要的吗?我猜第一个是因为已经达到停止条件,所以它跳过了该语句,但问题13为什么它不打印4321?
Can anyone explain why the answers are what they are please? The first one i guess its because the stopping condition is already reached so it skips the statement but question 13 why would it not print 4321?
12.以下代码产生什么输出:
int n;
for (n = 1; n > 4; n++)
System.out.print(n);
a)12345
b)1234
c)0 ---> for循环永远不会运行,因为已经满足停止条件.
c) 0 ---> for loop never runs because stopping condition already met.
d)它不产生输出*
d) It produces no output*
13.以下代码产生什么输出:
int n;
for (n = 4; n > 0; n--);
System.out.print(n);
a)43210
b)4321
c)0 * ---> for循环后为半冒号,导致print语句在循环结束后运行
c) 0* ---> semi colon after for loop, causing print statement to run after loop is finished
d)它不产生输出
推荐答案
关于问题13:for循环后面有分号.因此,循环将首先执行,直到n不再> 0 =>然后为零为止.然后,打印结果将显示为零.
Regarding question 13: there's a semicolon behind the for-loop. Because of that, the loop will first be execuetd until n no longer is > 0 => it is zero then. Then, the print will show a zero.
这篇关于对于循环和停止条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!