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

前端 未结 6 2156
猫巷女王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: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);
         }
       }
    
    0 讨论(0)
  • 2020-12-04 04:41

    This is not a bug. The semicolon becomes the only "statement" in the body of your for loop.

    Write this another way to make it easier to see:

    for (int i = 0; i < 100; i++)
        ;
    
    {
        count++;
    }
    

    The block with count++ becomes a bare block with a single statement, which isn't associated with the for loop at all, because of the semicolon. So this block, and the count++ within it, is only executed once.

    This is syntactically valid java. for (int i = 0; i < 100; i++); is equivalent to:

    for (int i = 0; i < 100; i++)
    { ; } // no statement in the body of the loop.
    

    for loops of this form can be useful because of the side-effects within the loop increment statement or termination condition. For instance, if you wanted to write your own indexOfSpace to find the first index of a space character in a String:

    int idx;
    
    // for loop with no body, just incrementing idx:
    for (idx = 0; string.charAt(idx) != ' '; idx++);
    
    // now idx will point to the index of the ' '
    
    0 讨论(0)
  • 2020-12-04 04:47

    Semicolon is an empty statement and its a valid statement. Your current loop is acting on just that "empty statement (;) therefore you don't see any change in count with respect to the loop.

    After the loop the statement count++ gets executed once and hence you see the result '1'. If the body of the loop is not specified using { } then the first statement of after the loop is considered part of the loop. In your second case its the empty statement.

    Its equivalent to:

    for (int i = 0; i < 100; i++) 
          { 
           ; //any empty valid statement
          }
    
    count++;
    
    0 讨论(0)
  • 2020-12-04 04:50

    By adding that semicolon, you are declaring the for statement without a block to loop.

    This causes count++ to only be executed once when it's passed, rather than 100 times when it's looped over.

    0 讨论(0)
  • 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);
         }
       }
    
    0 讨论(0)
  • 2020-12-04 04:57

    after iteration ending you are incrementing count value

    After for loop you are using semi colon so your for loop is end

    for (int i = 0; i < 100; i++);

    { count++; }

    count incremental is not in the for loop. it is outside the loop;

    0 讨论(0)
提交回复
热议问题