break or continue out of nested loops

Discussion in 'C Programming' started by viza, Jul 16, 2003.

  1. viza

    viza Guest

    Hi!

    Suppose I have

    int i,j,k;

    for(i=0;i<I;++i){ /* loop 1 */
    for(j=0;j<J;++j){ /* loop 2 */
    for(k=0;k<K;++k){ /* loop 3 */
    if(test){
    break;
    }
    }
    }
    }

    then when test comes to be true, it will break out of loop 3, increment j
    and continue in loop 2. How can I break out of all three? Is there a way
    to break out of say loop 3 and 2 and continue the top level loop? This
    would be break(2); in php. I vaguely remember in javascript you can label
    a loop to break out of a paticular one.

    Is there a way to control these, structures (and others, like switch etc)
    like this in C, or do I need to use temporary variables, like

    int i,j,k;

    for(i=0;i<I&&!broken;++i){ /* loop 1 */
    for(j=0;j<J&&!broken;++j){ /* loop 2 */
    for(k=0;k<K&&!broken;++k){ /* loop 3 */
    if(test){
    broken=1;
    break;
    }
    }
    }
    }

    ?

    Thanks

    Tom Viza
     
    viza, Jul 16, 2003
    #1
    1. Advertising

  2. viza wrote:

    > Is there a way to control these, structures (and others, like switch etc)
    > like this in C, or do I need to use temporary variables, like


    Use a goto. You won't be sent to hell for it (unless you choose 'hell'
    as the label. :) ).

    Paul
     
    Paul F. Dietz, Jul 16, 2003
    #2
    1. Advertising

  3. viza

    pete Guest

    viza wrote:
    >
    > Hi!
    >
    > Suppose I have
    >
    > int i,j,k;
    >
    > for(i=0;i<I;++i){ /* loop 1 */
    > for(j=0;j<J;++j){ /* loop 2 */
    > for(k=0;k<K;++k){ /* loop 3 */
    > if(test){
    > break;
    > }
    > }
    > }
    > }
    >
    > then when test comes to be true, it will break out of loop 3, increment j
    > and continue in loop 2. How can I break out of all three? Is there a way
    > to break out of say loop 3 and 2 and continue the top level loop? This
    > would be break(2); in php. I vaguely remember in javascript you can label
    > a loop to break out of a paticular one.
    >
    > Is there a way to control these, structures (and others, like switch etc)
    > like this in C, or do I need to use temporary variables, like
    >
    > int i,j,k;
    >
    > for(i=0;i<I&&!broken;++i){ /* loop 1 */
    > for(j=0;j<J&&!broken;++j){ /* loop 2 */
    > for(k=0;k<K&&!broken;++k){ /* loop 3 */
    > if(test){
    > broken=1;
    > break;
    > }
    > }
    > }
    > }



    /* BEGIN new.c */

    #include <stdio.h>

    #define I 5
    #define J 6
    #define K 7
    #define test (i == 1 && j == 2 && k ==3)

    int main(void)
    {
    int i,j,k;

    for(i=0;i<I;++i){ /* loop 1 */
    for(j=0;j<J;++j){ /* loop 2 */
    for(k=0;k<K;++k){ /* loop 3 */
    if(test){
    goto After_nest;
    }
    }
    }
    }

    After_nest:

    printf("i is %d\nj is %d\nk is %d\n", i, j, k);
    return 0;
    }

    /* END new.c */


    --
    pete
     
    pete, Jul 16, 2003
    #3
  4. viza

    Ema Guest

    "viza" <> ha scritto nel messaggio
    news:habRa.659$...
    > Hi!
    >
    > Suppose I have
    >
    > int i,j,k;
    >
    > for(i=0;i<I;++i){ /* loop 1 */
    > for(j=0;j<J;++j){ /* loop 2 */
    > for(k=0;k<K;++k){ /* loop 3 */
    > if(test){
    > break;
    > }
    > }
    > }
    > }
    >


    Don't know if you will like it: it's not elegant but it should run.

    for(i=0;i<I;++i){ /* loop 1 */
    for(j=0;j<J;++j){ /* loop 2 */
    for(k=0;k<K;++k){ /* loop 3 */
    if(test) break;
    }
    if (test) break;
    }
    }

    Bye,
    Ema
     
    Ema, Jul 16, 2003
    #4
  5. viza wrote:
    >
    > Hi!
    >
    > Suppose I have
    >
    > int i,j,k;
    >
    > for(i=0;i<I;++i){ /* loop 1 */
    > for(j=0;j<J;++j){ /* loop 2 */
    > for(k=0;k<K;++k){ /* loop 3 */
    > if(test){
    > break;
    > }
    > }
    > }
    > }
    >
    > then when test comes to be true, it will break out of loop 3, increment j
    > and continue in loop 2. How can I break out of all three?


    void Operate()
    {
    int i,j,k;

    for(i=0;i<I;++i){ /* loop 1 */
    for(j=0;j<J;++j){ /* loop 2 */
    for(k=0;k<K;++k){ /* loop 3 */
    if(test){
    return;
    }
    }
    }
    }
    }


    > Is there a way
    > to break out of say loop 3 and 2 and continue the top level loop?


    void OperateInner()
    {
    int j,k;

    for(j=0;j<J;++j){ /* loop 2 */
    for(k=0;k<K;++k){ /* loop 3 */
    if(test){
    return;
    }
    }
    }
    }

    void Operate()
    {
    int i;

    for(i=0;i<I;++i){ /* loop 1 */
    OperateInner();
    }

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 16, 2003
    #5
  6. viza

    sunil Guest

    viza <> wrote in message news:<habRa.659$>...
    Hi!
    One more idea. May be this is not exactly what you are looking for
    (but without using gotos/multiple functions/temporary variables)

    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    for(k=0;k<10;k++)
    {
    if(i== 6 && j== 5 && k == 5) /*Complelte
    condition*/
    break;
    }
    if(k == 5) /*Test if we executed break from k
    loop*/
    break;
    }
    if(j == 5) /*Text if we executed break from j loop*/
    break;
    }
    Regards,
    Sunil.
    > Hi!
    >
    > Suppose I have
    >
    > int i,j,k;
    >
    > for(i=0;i<I;++i){ /* loop 1 */
    > for(j=0;j<J;++j){ /* loop 2 */
    > for(k=0;k<K;++k){ /* loop 3 */
    > if(test){
    > break;
    > }
    > }
    > }
    > }
    >
    > then when test comes to be true, it will break out of loop 3, increment j
    > and continue in loop 2. How can I break out of all three? Is there a way
    > to break out of say loop 3 and 2 and continue the top level loop? This
    > would be break(2); in php. I vaguely remember in javascript you can label
    > a loop to break out of a paticular one.
    >
    > Is there a way to control these, structures (and others, like switch etc)
    > like this in C, or do I need to use temporary variables, like
    >
    > int i,j,k;
    >
    > for(i=0;i<I&&!broken;++i){ /* loop 1 */
    > for(j=0;j<J&&!broken;++j){ /* loop 2 */
    > for(k=0;k<K&&!broken;++k){ /* loop 3 */
    > if(test){
    > broken=1;
    > break;
    > }
    > }
    > }
    > }
    >
    > ?
    >
    > Thanks
    >
    > Tom Viza
     
    sunil, Jul 17, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    3
    Views:
    2,081
  2. Glen Wheeler

    continue and break frustration

    Glen Wheeler, Dec 14, 2003, in forum: Python
    Replies:
    5
    Views:
    429
    Rene Pijlman
    Dec 16, 2003
  3. Ann

    break/continue - newbe

    Ann, Dec 21, 2004, in forum: Python
    Replies:
    5
    Views:
    353
    John Machin
    Dec 21, 2004
  4. Shawn
    Replies:
    2
    Views:
    292
  5. Talha Oktay
    Replies:
    3
    Views:
    204
    Simon Kröger
    Mar 30, 2006
Loading...

Share This Page