简单的 Java For 循环逻辑、增量和减量
我这里有一个简单的Java 程序。我写这篇文章是为了逐步了解程序的逻辑,并验证它为什么返回它所执行的输出。如果您对我的解释方式有任何反馈,我将不胜感激。
我将首先显示该程序,然后再解释它是如何工作的。
public class Test9 {
public static void main (String [] args) {
int x = 0;
int y = 10;
for (int z = 0; z < 5; z++) {
if ((y-- > 7) || (++x > 2)) {
y--;
}
}
System.out.println(y + "" + x);
}
}
输出是什么以及为什么?
为了知道该程序的正确输出,有必要在“for”循环过程中对所有三个 int 变量 x、y 和 z 进行运行计数。
在循环开始时,z=0、x=0、y=10。 然后继续执行 if 语句,查看 y 的当前值是否大于 7。需要注意的是,y-- 表示它将后减 1。这意味着在条件语句之后已决定真或假。
因此,由于 y=10,目前 10>7,并且该语句为 True。由于此条件语句为 true 并且它看到下一个 OR 运算符,因此它会跳过 ++x>2,因为它不需要知道它是 TRUE 还是 FALSE。现在执行循环的内容 y--,这使得 y=8。在条件语句中和 if 语句中递减一次。 x 保持为 0。
下一次迭代开始于 z=1,y=8。 x=0。应用相同的规则,因此8>7,y将减2并且x将保持不变。
第三次迭代从 z=2、y=6、x=0 开始。这一次,6>7 为假,但 y 仍然是后递减的。因此 y=5 且 ++x>2 将计算为 1>2,因为 ++x 是前缀版本并在操作发生之前加 1。两个操作都是假的,并且 y 不会第二次递减。
第四次迭代从 z=3、y=5、x=1 开始。 5>7 为假,y 递减至 4。2>2 也为假,因此 y 没有第二次递减。
第五次迭代从 z=4、y=4、x=2 开始。 4>7 为假,y 递减至 3。3>2 为真!因此 y 将第二次递减至 2。
最终值为 y=2,x=3。因此程序输出将是: 2 3
了解增量和减量何时发生以及何时不发生非常重要,尤其是当它们位于 if 语句的条件语句内时,因为这会影响最终输出。
如果我的逻辑中存在术语/行话、表达方式或缺陷,请告诉我。我试图了解 java 的所有基础知识。谢谢。
I have here a simple Java Program. I am writing this to walk through the logic of the program step by step and verify why it returns the output that it does. I would appreciate any feedback on the way I'm interpreting this.
I'll display the program first, and then explain how it works afterwards.
public class Test9 {
public static void main (String [] args) {
int x = 0;
int y = 10;
for (int z = 0; z < 5; z++) {
if ((y-- > 7) || (++x > 2)) {
y--;
}
}
System.out.println(y + "" + x);
}
}
What is the output and why?
In order to know the correct output of this program, it's necessary to keep a running tally of all three int variables, x, y, and z, during the course of the "for" loop.
At the beginning of the loop, z=0, x=0, and y=10.
It then proceeds to the if statement, where it looks to see if the current value of y is greater than 7. It's important to note that y-- means that it will post-decrement y by 1. Which means AFTER the conditional statement has been been decided True or False.
So, since y=10, currently, 10>7, and the statement is True. Since this conditional statement is true and it sees that an OR operator is next, it skips the ++x>2 since it doesn't need to know whether it is TRUE or FALSE. The contents of the loop are now performed, y--, which makes y=8. Decremented once in the conditional statement and once inside the if statement. x remains 0.
The next iteration begins with, z=1, y=8. x=0. The same rules apply, so 8>7, y will be decremented by 2 and x will remain the same.
The third iteration begins with z=2, y=6, x=0. This time around, 6>7 is false, but y is still post decremented. So y=5, and ++x>2, will evaluate as 1>2, since ++x is the prefix version and adds 1 before the operation takes place. Both operations are false, and y will not be decremented a second time.
The forth iteration begins with z=3, y=5, x=1. 5>7 is false, y is decremented to 4. 2>2 is false as well, therefore no 2nd decrement to y.
The fifth iteration begins with z=4, y=4, x=2. 4>7 is false, y is decremented to 3. 3>2 is true! So y will be decremented a 2nd time, to 2.
The ending values are y=2, x=3. Therefore the program output will be:
2 3
It's important to understand when the increments and decrements happen and when they don't, especially when they are inside the conditional statements of an if statement, since it will impact the final output.
If there are terms/jargon, ways of saying things, or flaws in my logic please let me know. I'm trying to understand all the fundamentals of java. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的逻辑是正确的......但实际上问题是什么?
You logic is correct... But what is actually the question?