Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
循环以
i = 0
开始。if
和else
完全相同。增加i
并继续。如果使用一点逻辑,整个块可以简化为
i++
(i = i % 3
没有任何效果,因为i < 2
>)。使用您发布的代码不可能获得
4
。The loop starts with
i = 0
. Both theif
and theelse
to exactly the same thing. Incrementi
and continue.If you use a bit of logic, the whole block can be reduced to
i++
(i = i % 3
has no effect sincei < 2
).It's not possible to get
4
with the code you posted.对于您发布的程序,输出不能为
4
,因为当循环中断时,i
的值将是2
,而不是 < code>4 并且循环将恰好运行一次。此外,您的代码永远不会进入
if
块,因为条件是i==2
在for
循环中永远不可能为真,如下所示到那时循环就会退出。所以你的代码相当于这样:
The output cannot be
4
for the program you posted, because by the time the loop breaks, the value ofi
would be2
, not4
and the loop will run exactly once.Also, your code never enters the
if
block, because the condition isi==2
which can never be true inside thefor
loop, as by that time the loop would be exited.So your code is equivalent to this: