What does a for loop without curly braces do?

后端 未结 2 1780
旧时难觅i
旧时难觅i 2020-11-30 12:53

Hi so basically my question is what does a for loop without any curly braces around it do? So from what I know, that during an if-statement only the first line of the code i

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 13:45

    If the for-loop does not have braces, it will execute the next statement. The syntax is essentially

    for (;;) ;
    

    The "statement" part can be anything. It could be a simple count++; or it could be an 'if'/'if-else' statement, it could even be another for-loop without the braces!

    So in this case, the code is similar to:

    for (i = 0; i < 5; i++) {
        count++;
    }
    printf("The value of count is: %d\n", count);
    

提交回复
热议问题