+ 1

I have a question...

#include <stdio.h> int main() { int a; int b; for (a=0, b=0; a<5, b<6; a++, b++) { printf("%d %d\n", a, b); } return 0; } /*The output is: 0 0 1 1 2 2 3 3 4 4 5 5 When the variables a and b are incremented to both be 5 by a++ and b++, it will check this condition: a<5, b<6. Why the program print "5 5" because the a<5 is not true? Although the b<6 is true, so both conditions must be false in order to get out of the loop?*/

28th Dec 2018, 1:25 PM
Konybud
Konybud - avatar
2 odpowiedzi
+ 1
Thx
28th Dec 2018, 2:49 PM
Konybud
Konybud - avatar
+ 6
Multiple conditions in for loops with a bit differently; in the for loop, everything but the last condition is "thrown away". So in this case the "a < 5" condition is discarded and only the "b < 6" condition is checked. Read the article below for more info: https://stackoverflow.com/questions/16859029/multiple-conditions-in-a-c-for-loop
28th Dec 2018, 1:46 PM
blackcat1111
blackcat1111 - avatar