in a loop

This is a discussion on in a loop within the C++ Programming forums, part of the General Programming Boards category; Code: for (i=...) { . . if() "Iwant to take next i here" for (j=..) { . if() "I want ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    in a loop

    Code:
    for (i=...)
      {
        .
        .
        if()
          "Iwant to take next i here"
        for (j=..)
        {
          .
           if()
            "I want also  next i here"
              .
              .
        }
      }
    I want to get out of i and j loop when certain if is fulfilled but I want to avoid using goto command and I want to avoid restructuring loops.
    Thank you

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    bool check;
    for (i=...)
    {
       check = false;
       if()
          //"I want to take next i here"
          continue;
       for (j=...;... && !check;)
       {
          if()
          {
             //"I want also  next i here"
             check = true;
             continue; 
          }
       }
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Here's another way..

    Code:
    for (i=...)
      {
        .
        .
        if()
          continue;  // "Iwant to take next i here"
    
        for (j=..)
        {
          .
           if()
            break;  // "I want also  next i here"
              .
              .
        }
      }
    You'd need a little more work if there was more code after the j loop (while still inside i loop), but since you didn't list any, I will assume there isn't any, so this will work fine.

    One thing I think the language really should have is an optional number after the break and continue statements, so you could have:

    Code:
    for (i=...) {
       for (j=...) {
          if (...)
             continue 2;  // go to next iteration of i
       }
    }
    So far, no one in charge of language design has ever thought of this idea though I guess. I put it in the scripting language I wrote, though.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    continue just begins another iteration of the current loop. break breaks you out of the loop completely. Your wanting to use continue 2 would be the same as a break.

    Additionally, people tend to forget that you can modify the sentinel from within the body of a loop. In the above examples, try coupling the continue/break statements with setting i or j to their sentinel values within the body of the loops, and see how well you can manipulate execution.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Scribbler
    Additionally, people tend to forget that you can modify the sentinel from within the body of a loop.
    It's not so much as a "forget" as a "prefer not to". Just as a matter of personal preference, I try to modify my loop control variables in only one place. It's just one less thing to think about when debugging long, complicated loops.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    It's not so much as a "forget" as a "prefer not to". Just as a matter of personal preference, I try to modify my loop control variables in only one place. It's just one less thing to think about when debugging long, complicated loops.
    I have to agree there.

    Isn't break 2; a valid statement? continue 2 would just move execution to the outer loop again. I've never actually tried using break 2 before but as far as I know it's valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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