for循环是如何完成的 [英] How does the for loop exactly work out

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

问题描述

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



$ b我知道它是如何工作的,但是我不明白 i ++ 在最后工作:它应该加1,如果我是正确的,但是当它打印出 i ,它打印0和1。

为什么不是刚刚从1开始,因为 i ++ ?为什么它仍然只是打印出原来的值而不是 i ++ 的值?

解决方案循环,作用如下:
$ b $ ol
  • 初始化完成 int i = 0 在你的情况下,只执行一次)

  • 检查条件( i <= 100 这里),如果条件是假的离开循环

  • 大括号内的代码被执行( System.out.println(i);

  • 执行更新语句( i ++

  • 转到2。


  • This is a very simple for loop:

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

    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.

    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?

    解决方案

    A for loop works as follows:

    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天全站免登陆