Why does a for loop with a semicolon after it still execute?

前端 未结 4 1615
离开以前
离开以前 2020-12-21 13:42

I was having a look at some code a friend did and stumbled across this line which I thought was an error (simplified for example)..

for (g = 0; 10 > g; g+         


        
4条回答
  •  长情又很酷
    2020-12-21 14:31

    The semi colon actually runs the loop without the {} code block. So your code basically has 2 parts,

    1. update variable g to from 0 to 10 in 10 steps,
    2. print out g (now equals to 10) once.

提交回复
热议问题