for loop without index declaration

前端 未结 5 1345
误落风尘
误落风尘 2021-01-24 00:11

So I declare a variable some where and initialize it. Now later on I need to use it to loop while its still positive so I need to decrement it. To me looping using a condition a

5条回答
  •  盖世英雄少女心
    2021-01-24 01:15

    another way is this one:

    Integer i = 10;
    while(--i>0) {
        System.out.println(i);
    }
    

    When i is 0 while condition is false... so.. it will print from 9 to 1 (9 items)

    Integer i = 10;
    while(i-->0) {
        System.out.println(i);
    }
    

    Will print from 9 to 0... (10 items);

提交回复
热议问题