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

前端 未结 6 2168
猫巷女王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条回答
  •  -上瘾入骨i
    2020-12-04 04:38

    The semicolon makes the body of the for loop empty. It is equivalent to:

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

提交回复
热议问题