Thread: break in for loop not working

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Smile break in for loop not working

    I previously asked this on another thread but no one replied. My question is how to stop a for loop. I have a for loop with counter 'i' with a maximum of 10. Then I have an inner for loop with counter 'j' with a maximum value entered by the user. I asked for some input and then checked if the user enters a letter then the loop breaks. My problem is that even thought the for loop breaks, the 'i' counter keeps going all the way the its maximum value. How can I stop it from doing that.
    I put a printf() statement for 'i' and it goes all the way

    Here is the peace of code
    Code:
    for(i = 0; i <10; i++){      
            for(j = 0; j <cols; j++){    
              scanf("%d", &matrix[i][j]);
              if(isalpha(matrix[i][j]))
              break;
    
             }
         }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Simple: a "break" breaks only the loop that is in, no other. Here you have two loops!

    EDIT: Use something like:
    Code:
    for (i = 0; i < 10; i++){
        for (j = 0; j < 10; j++){
            // Break however you want in here
        }
    
        if (j < 10)
            break;
    }
    That is of course only one of the ways to do it.
    Last edited by GReaper; 09-17-2011 at 08:13 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Just before the break set the i to 10 (or greater).
    Then the first for loop will stop.

    Dylan

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by dylan View Post
    Just before the break set the i to 10 (or greater).
    Then the first for loop will stop.

    Dylan
    Nice trick. But remember that it's a good programming practice to not change the value of counters inside the loops.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sipher View Post
    Nice trick. But remember that it's a good programming practice to not change the value of counters inside the loops.
    Like most things described as "good practice", there are multiple views on what "good" really means.

    A technique, if you want to avoid changing counters inside their loops, is
    Code:
    for(i = 0, barrel_on = true; barrel_on && i <10; i++)
    {      
            for(j = 0; barrel_on && j <cols; j++)
            {    
              scanf("%d", &matrix[i][j]);
              if(isalpha(matrix[i][j])) barrel_on = false;
             }
    }
    Or (at the risk of encouraging cries of horror from the "goto is evil" crowd)
    Code:
    for(i = 0; i <10; i++)
    {      
            for(j = 0; j <cols; j++)
            {    
              scanf("%d", &matrix[i][j]);
              if(isalpha(matrix[i][j]))  goto break_out;
             }
    }
    break_out:
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How is isalpha ever going to be true for something that was read in using %d inside scanf!
    It's never going to break because an int is never an alphabetical character. Try checking the return value of scanf instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    How is isalpha ever going to be true for something that was read in using %d inside scanf!
    Easily. Assuming your machine has an ASCII character set, the letter 'A' has a value of 65. isalpha() accepts a value of type int, and it is fairly easy for a user to enter 65 into an int via scanf's %d format.

    I suspect the actual question you wanted to ask is: why you would want to check integral values entered by the user with isalpha()?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ahem ... %d doesn't read alphabetical characters ... ahem ( Damm this cold! )
    Devoted my life to programming...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Ahem ... %d doesn't read alphabetical characters ... ahem ( Damm this cold! )
    try this on for size Sipher...

    Code:
    #include <stdio.h>
    
    int main (void)
      {  int num;
    
          printf("Enter 65 : ");
          scanf(" %d", &num);  // type 65<enter> when prompted
          if (isalpha(num) )
            printf("Well Ahll Be Darned!");
       
          return 0;
    }

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ohh the number is a letter trick, e? What if my PC does run on ASCII?!
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Smile

    Ok, I see many suggestions. I tried putting a break just for the 'i' counter but it didn't work. What I want is just to stop the i from going all the way to 10. I can't define it as other number since i would depend from the total number of inputs and 'j'(which is defined by the user. If I put a break on the for loop I get out which is good, but the 'i' counter doesn't stop counting. Why does the 'i' keep increasing if I exit the for loop?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    What if my PC does run on ASCII?!
    Most do...

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by torquemada View Post
    Ok, I see many suggestions. I tried putting a break just for the 'i' counter but it didn't work. What I want is just to stop the i from going all the way to 10. I can't define it as other number since i would depend from the total number of inputs and 'j'(which is defined by the user. If I put a break on the for loop I get out which is good, but the 'i' counter doesn't stop counting. Why does the 'i' keep increasing if I exit the for loop?
    Make it a function....
    Code:
    void FillErUp( int *matrix )
      { for(i = 0; i <10; i++){      
            for(j = 0; j <cols; j++){    
              scanf("%d", &matrix[i][j]);
              if(isalpha(matrix[i][j]))
                return;
             }
         }
    However... as already indicated this method is prone to a lot of false positivies ... any number from 65 to 90 or 97 to 122 will return positive from your test.

    A far better method is to test the return value of scanf() (Go ahead, look it up in your Library Documentation, it's ok to use help files!)

    Code:
    void FillErUp( int *matrix )
      { for(i = 0; i <10; i++){      
            for(j = 0; j <cols; j++){    
              if (scanf("%d", &matrix[i][j]) < 1)
                 return;
             }
         }

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by CommonTater View Post
    Most do...
    Sorry, I meant "doesn't".

    Anyway, what I said earlier is that because scanf("%d", ...) doesn't accept letters such as 'a' or 'Z', there is no point at checking for them with isalpha afterwards.
    Devoted my life to programming...

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Sorry, I meant "doesn't".

    Anyway, what I said earlier is that because scanf("%d", ...) doesn't accept letters such as 'a' or 'Z', there is no point at checking for them with isalpha afterwards.
    Then it would false trigger on different values...

    I agree using isalpha() in that situation is wrong... what if the value 65 (continuing the example) is a valid entry for his int matrix[][] array? The isalpha() function would falsely terminate the loop...
    Last edited by CommonTater; 09-17-2011 at 09:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to break out of loop
    By Ducky in forum C++ Programming
    Replies: 31
    Last Post: 09-07-2010, 11:22 PM
  2. break from for loop
    By taurus in forum C Programming
    Replies: 3
    Last Post: 09-24-2009, 03:54 PM
  3. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  4. Loop will not break.
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 03-20-2008, 06:51 PM
  5. Need help with break in do while loop
    By JoelearningC in forum C Programming
    Replies: 3
    Last Post: 03-19-2008, 11:12 PM