Are multiple conditions allowed in a for loop?

前端 未结 5 977
無奈伤痛
無奈伤痛 2020-12-14 13:26

The following code runs without giving any errors or warnings

#include

int main(){
    int i, j;
    int p = 0, q = 2;
    for(i = 0, j = 0;          


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 14:18

    You can link them together with boolean and (&&)

    for(i = 0, j = 0; (i < p) && (j < q); i++, j++){
    

    The above won't print out anything in the loop because the (i < p) condition fails immediately as i & p are both the same value (0).

    Update: Your example is valid (but silly) C because if you start i=30 your loop will still execute 2 times, because the first result in a comma separated list is ignored.

提交回复
热议问题