i++ & 和有什么不一样?++i 在 for 循环中? [英] What is the difference between i++ & ++i in a for loop?

查看:27
本文介绍了i++ & 和有什么不一样?++i 在 for 循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 Java,现在我开始学习 for 循环语句.我不明白 ++ii++ 如何在 for 循环中工作.

I've just started learning Java and now I'm into for loop statements. I don't understand how ++i and i++ works in a for-loop.

他们在加减法等数学运算中是如何工作的?

How do they work in mathematics operations like addition and subtraction?

推荐答案

它们都增加了数字.++i 等价于 i = i + 1.

They both increment the number. ++i is equivalent to i = i + 1.

i++++i 非常相似但不完全相同.两者都增加数字,但 ++i 在计算当前表达式之前增加数字,而 i++ 在计算表达式之后增加数字.

i++ and ++i are very similar but not exactly the same. Both increment the number, but ++i increments the number before the current expression is evaluted, whereas i++ increments the number after the expression is evaluated.

int i = 3;
int a = i++; // a = 3, i = 4
int b = ++a; // b = 4, a = 4

这篇关于i++ & 和有什么不一样?++i 在 for 循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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