Why does adding a semicolon after 'for (…)' change the meaning of my program so dramatically?

前端 未结 6 2173
猫巷女王i
猫巷女王i 2020-12-04 04:30

I wrote the following class:

  public class TestOne {
     public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < 100         


        
6条回答
  •  旧巷少年郎
    2020-12-04 04:55

    With that semicolon you are saying that the for loop block is ending and there is no instructions inside.

    Then you increment count just once and this is why the output is 1

    public class TestOne {
         public static void main(String[] args) {
            int count = 0;
            for (int i = 0; i < 100; i++)  ; 
    
            {
              count++;
            }
            System.out.println(count);
         }
       }
    

提交回复
热议问题