# ¿What is happening with this If statment?

This is a discussion on ¿What is happening with this If statment? within the C Programming forums, part of the General Programming Boards category; Hi again! This is my code. Code: for( i = 0; i < DIM; i++){ for( j = 0; j ...

1. ## ¿What is happening with this If statment?

Hi again!

This is my code.

Code:
```  for( i = 0; i < DIM; i++){
for( j = 0; j < DIM; j++){
for ( k = 1; k <= cont; k++){

if(  i == s_c1[k][0] && j == s_c1[k][1] ) {
fprintf( fp[1], " &#37;d ",5);
break;
}
else {
fprintf (fp[1], " %d ",0);
break;
}
}
}
fprintf(fp[1],"\n");
}```
My problem is this: s_c1 are integer numbers. I put the "break" because they may be the same number for differents "k" values, and without it, my matrix array will fail (because of the dimension). So, for each "i" and "j", and for all "k", if the condicional is satisfied, only one "5" can be recorded; if not, only one "0" have to be recorded.

But... altough I have 15 s_c1, my recorded file only has ONE "5", and it has to have fiveteen. &#191;Is the "break" doing something wrong there?

Thanks for reading!

2. Well the break is going to break out of the innermost loop.
And since both the if and else parts contain break, the innermost loop only executes once.

3. Perhaps you want to look into continue;

4. Edit: Perhalps you want something like this:

Code:
```for( i = 0; i < DIM; i++){
for( j = 0; j < DIM; j++){
for ( k = 1; k <= cont; k++){
if(  i != s_c1[k][0] || j != s_c1[k][1] ) {
fprintf (fp[1], " &#37;d ",0);
break;
}
}
if(k==cont+1)/*if the loop finished without printing 0;*/
fprintf( fp[1], " %d ",5);
}
fprintf(fp[1],"\n");
}```

5. YEs!!!

I had done this

Code:
```for( i = 0; i < DIM; i++){
for( j = 0; j < DIM; j++){
for ( k = 1; k <= cont; k++){
p=0;
if(  i == s_c1[k][0] && j == s_c1[k][1] ) {

fprintf( fp[1], " &#37;d ",5);
p=1;
break;
}
}
if( p == 1) continue;
else fprintf (fp[1], " %d ",0);

}
fprintf(fp[1],"\n");
}```
But obviusly your idea is more efficient!!!

Thanks a lot!

Popular pages Recent additions