¿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. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    26

    ¿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!
    Last edited by Isolda_; 09-16-2007 at 01:56 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Perhaps you want to look into continue;

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    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");
    }
    Last edited by King Mir; 09-16-2007 at 04:58 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    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 subscribe to a feed

Similar Threads

  1. Explain me what is happening
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 08:26 AM
  2. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 06:31 AM
  3. if statment question
    By Carsten in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2002, 05:50 PM
  4. Replies: 2
    Last Post: 01-01-2002, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21