c - Breaking out of a for-loop -
i'm supposed use code
for(i=1;i<4;i++) { for(j=1;j,4;j++) { printf("running i=%d j=%d\n", i, j); } }
... code break out of loop
if(i==2&&j ==1){ printf("break inner loop when i=%d , j=%d\n", i, j); break; }
my textbook said insert break
statement @ beginning of inner loop block. don't know is! i've tried lot of places already, , still can't figure out. here whole program:
#include <stdio.h> int main() { int i,j; for(i=1;i<4;i++) { for(j=1;j,4;j++) if(i==2&&j ==1){ printf("break inner loop when i=%d , j=%d\n", i, j); break; } printf("running i=%d j=%d\n", i, j); } } return 0; }
figured out: had typo
the beginning of inner-loop block after {
on second for
:
for(i=1;i<4;i++) { for(j=1;j,4;j++) { // <<<--- mean here. printf("running i=%d j=%d\n", i, j); } }
note break
break out of inner loop. outer loop go around again. if need break out of both loops need add flag of sort.
just noticed have typo in inner loop valid c (the comma operator). should have been <
:
for(j=1;j<4;j++)
Comments
Post a Comment