for 循环究竟是如何工作的 [英] How does the for loop exactly work out

查看:37
本文介绍了for 循环究竟是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的 for 循环:

This is a very simple for loop:

for(int i=0;i<=100;i++)
{
    System.out.println(i);
}

我知道它主要是如何工作的,但我不明白 i++ 最后是如何工作的:它应该加 1,如果我是正确的,但是当它打印出 i,它打印出0然后1.

I know how it mostly works, but I don't understand how the i++ works at the end: its supposed to add 1, if I'm correct, but when it prints out the i, it prints out 0 and then 1.

由于 i++,为什么不从 1 开始?为什么它仍然只是打印出原始值而不是 i++ 值?

Why doesn't it just start out with 1 because of the i++? Why does it still just print out the original value instead of the i++ value?

推荐答案

for 循环的工作原理如下:

A for loop works as follows:

  1. 初始化完成(int i=0 在你的情况下;只执行一次)
  2. 检查条件(i<=100 此处),如果条件为假则退出循环
  3. 大括号内的代码被执行(System.out.println(i); 在你的情况下)
  4. 执行更新语句(i++)
  5. 转到 2.
  1. Initialization is done (int i=0 in your case; only executed once)
  2. Condition is checked (i<=100 here), if condition is false leave the loop
  3. Code within the braces is executed (System.out.println(i); in your case)
  4. Update statement is executed (i++)
  5. Goto 2.

这篇关于for 循环究竟是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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