Double break

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Johansen

    #1

    Double break

    Hello group.

    Are there any efficient way to double 'break' a 2-dimensional loop
    without using a jump, an extra variable or midifying i and j?

    e.g.

    for(i = 0; i < x; i++)
    for(j = 0; j < y; j++){
    z;
    break(2); /* i know the 2 does not help */
    }
  • those who know me have no need of my name

    #2
    Re: Double break

    in comp.lang.c i read:
    [color=blue]
    >Are there any efficient way to double 'break' a 2-dimensional loop
    >without using a jump, an extra variable or midifying i and j?[/color]

    nope.

    --
    a signature

    Comment

    • Richard Tobin

      #3
      Re: Double break

      In article <IQ4zc.8162$RL3 .137477@news2.e .nsc.no>,
      Martin Johansen <mfag@online.no > wrote:
      [color=blue]
      >Are there any efficient way to double 'break' a 2-dimensional loop
      >without using a jump, an extra variable or midifying i and j?[/color]

      Why do you care, since you can use a goto?

      -- Richard

      Comment

      • Trevor Fancher

        #4
        Re: Double break

        Martin Johansen wrote:
        [color=blue]
        > Hello group.
        >
        > Are there any efficient way to double 'break' a 2-dimensional loop
        > without using a jump, an extra variable or midifying i and j?
        >
        > e.g.
        >
        > for(i = 0; i < x; i++)
        > for(j = 0; j < y; j++){
        > z;
        > break(2); /* i know the 2 does not help */
        > }[/color]

        K&R suggests using goto in section 3.8 . They say it is about the only
        time it is okay to use goto.

        -Trevor

        Comment

        • kal

          #5
          Re: Double break

          Martin Johansen <mfag@online.no > wrote in message news:<IQ4zc.816 2$RL3.137477@ne ws2.e.nsc.no>.. .
          [color=blue]
          > Are there any efficient way to double 'break' a 2-dimensional loop
          > without using a jump, an extra variable or midifying i and j?
          >
          > e.g.
          >
          > for(i = 0; i < x; i++)
          > for(j = 0; j < y; j++){
          > z;
          > break(2); /* i know the 2 does not help */
          > }[/color]

          No.

          Sometimes you can use the inner loop's condition immediately
          after that loop. e.g. "If j < y break;"

          <OT>
          Which is preferable: "are there any efficient way", "Are there
          any efficient ways", "Is there any efficient way", "Is there
          any efficient ways" or "Is there an efficient way?"
          </OT>

          Comment

          • CBFalconer

            #6
            Re: Double break

            Martin Johansen wrote:[color=blue]
            >
            > Are there any efficient way to double 'break' a 2-dimensional loop
            > without using a jump, an extra variable or midifying i and j?
            >
            > e.g.
            >
            > for(i = 0; i < x; i++)
            > for(j = 0; j < y; j++){
            > z;
            > break(2); /* i know the 2 does not help */
            > }[/color]

            Well, the most efficient code for that snippet is:

            i = 0; j = 0; z;

            assuming x and y both >= 0. <g,d,&r>

            (We can work out the appropriate actions for other x and y)

            --
            Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
            Available for consulting/temporary embedded and systems.
            <http://cbfalconer.home .att.net> USE worldnet address!


            Comment

            • Yu SONG

              #7
              Re: Double break

              Trevor Fancher wrote:[color=blue]
              > Martin Johansen wrote:
              >
              >[color=green]
              >>Hello group.
              >>
              >>Are there any efficient way to double 'break' a 2-dimensional loop
              >>without using a jump, an extra variable or midifying i and j?
              >>
              >>e.g.
              >>
              >>for(i = 0; i < x; i++)
              >> for(j = 0; j < y; j++){
              >> z;
              >> break(2); /* i know the 2 does not help */
              >> }[/color]
              >
              >
              > K&R suggests using goto in section 3.8 . They say it is about the only
              > time it is okay to use goto.
              >
              > -Trevor
              >[/color]

              Or you can put them in a function,

              Replace the "break(2)" by a "return ABC" statement;


              --
              Song

              /* E-mail.c */
              #define User "Y.Song"
              #define Warwick "dcs.warwick.ac .uk"
              int main() {
              printf("Yu Song's E-mail: %s@%s", User, Warwick);
              return 0;}

              Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
              _______________ _______________ _______________ __________

              Comment

              • Darrell Grainger

                #8
                Re: Double break

                On Mon, 14 Jun 2004, Martin Johansen wrote:
                [color=blue]
                > Hello group.
                >
                > Are there any efficient way to double 'break' a 2-dimensional loop
                > without using a jump, an extra variable or midifying i and j?[/color]

                By 'using a jump' go you mean using a goto? This is the most efficient way
                of exiting nested loops. Even using a flag (extra variable) or modifying i
                and j will not be as efficient as using a goto.

                Additionally, you might want to retain the last value of i and j when you
                exit the loops. A goto will accomplish this.
                [color=blue]
                > e.g.
                >
                > for(i = 0; i < x; i++)
                > for(j = 0; j < y; j++){
                > z;
                > break(2); /* i know the 2 does not help */
                > }[/color]

                --
                Send e-mail to: darrell at cs dot toronto dot edu
                Don't send e-mail to vice.president@ whitehouse.gov

                Comment

                • Thomas Matthews

                  #9
                  Re: Double break

                  Yu SONG wrote:
                  [color=blue]
                  > Trevor Fancher wrote:
                  >[color=green]
                  >> Martin Johansen wrote:
                  >>
                  >>[color=darkred]
                  >>> Hello group.
                  >>>
                  >>> Are there any efficient way to double 'break' a 2-dimensional loop
                  >>> without using a jump, an extra variable or midifying i and j?
                  >>>
                  >>> e.g.
                  >>>
                  >>> for(i = 0; i < x; i++)
                  >>> for(j = 0; j < y; j++){
                  >>> z;
                  >>> break(2); /* i know the 2 does not help */
                  >>> }[/color]
                  >>
                  >>
                  >>
                  >> K&R suggests using goto in section 3.8 . They say it is about the only
                  >> time it is okay to use goto.
                  >>
                  >> -Trevor
                  >>[/color]
                  >
                  > Or you can put them in a function,
                  >
                  > Replace the "break(2)" by a "return ABC" statement;
                  >
                  >[/color]

                  The problem is that a return will exit the function.
                  If there is more processing after the for loops, your
                  solution will not allow the execution of stuff after
                  the for loops.


                  --
                  Thomas Matthews

                  C++ newsgroup welcome message:

                  C++ Faq: http://www.parashift.com/c++-faq-lite
                  C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                  alt.comp.lang.l earn.c-c++ faq:

                  Other sites:
                  http://www.josuttis.com -- C++ STL Library book

                  Comment

                  • Mike Wahler

                    #10
                    Re: Double break


                    "Martin Johansen" <mfag@online.no > wrote in message
                    news:IQ4zc.8162 $RL3.137477@new s2.e.nsc.no...[color=blue]
                    > Hello group.
                    >
                    > Are there any efficient[/color]

                    Define 'efficient'.
                    [color=blue]
                    >way to double 'break' a 2-dimensional loop
                    > without using a jump, an extra variable or midifying i and j?
                    >
                    > e.g.
                    >
                    > for(i = 0; i < x; i++)
                    > for(j = 0; j < y; j++){
                    > z;
                    > break(2); /* i know the 2 does not help */
                    > }[/color]

                    The *only* way that meets your above constraints is
                    to modify 'i' (set it greater or equal to 'y') during
                    the inner loop. Had you required 'without modifying i or j',
                    then afaik, there's no way at all.

                    -Mike


                    Comment

                    • Alan Balmer

                      #11
                      Re: Double break

                      On 13 Jun 2004 20:40:56 -0700, k_amir7@yahoo.c om (kal) wrote:
                      [color=blue]
                      >Martin Johansen <mfag@online.no > wrote in message news:<IQ4zc.816 2$RL3.137477@ne ws2.e.nsc.no>.. .
                      >[color=green]
                      >> Are there any efficient way to double 'break' a 2-dimensional loop
                      >> without using a jump, an extra variable or midifying i and j?
                      >>
                      >> e.g.
                      >>
                      >> for(i = 0; i < x; i++)
                      >> for(j = 0; j < y; j++){
                      >> z;
                      >> break(2); /* i know the 2 does not help */
                      >> }[/color]
                      >
                      >No.
                      >
                      >Sometimes you can use the inner loop's condition immediately
                      >after that loop. e.g. "If j < y break;"
                      >
                      ><OT>
                      >Which is preferable:[/color]

                      (1) "are there any efficient way",
                      (2) "Are there any efficient ways",
                      (3) "Is there any efficient way",
                      (4) "Is there any efficient ways" or
                      (5) "Is there an efficient way?"[color=blue]
                      ></OT>[/color]

                      Choices 2, 3 and 5 are all grammatically correct. Choices 3 and 5 have
                      essentially the same meaning, and I would not have a preference.
                      Choice 2 differs only in that it allows for multiple efficient ways.

                      --
                      Al Balmer
                      Balmer Consulting
                      removebalmercon sultingthis@att .net

                      Comment

                      • Mark McIntyre

                        #12
                        Re: Double break

                        On 14 Jun 2004 00:42:15 GMT, in comp.lang.c , richard@cogsci. ed.ac.uk
                        (Richard Tobin) wrote:
                        [color=blue]
                        >In article <IQ4zc.8162$RL3 .137477@news2.e .nsc.no>,
                        >Martin Johansen <mfag@online.no > wrote:
                        >[color=green]
                        >>Are there any efficient way to double 'break' a 2-dimensional loop
                        >>without using a jump, an extra variable or midifying i and j?[/color]
                        >
                        >Why do you care, since you can use a goto?[/color]

                        blech.
                        --
                        Mark McIntyre
                        CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                        CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


                        ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                        http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                        ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


                        ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                        http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                        ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                        Comment

                        • Richard Tobin

                          #13
                          Re: Double break

                          In article <nq9sc05jds5q5j ue3976ocna7ncbh qk45f@4ax.com>,
                          Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                          [color=blue][color=green][color=darkred]
                          >>>Are there any efficient way to double 'break' a 2-dimensional loop
                          >>>without using a jump, an extra variable or midifying i and j?[/color][/color][/color]
                          [color=blue][color=green]
                          >>Why do you care, since you can use a goto?[/color][/color]
                          [color=blue]
                          >blech.[/color]

                          So what's your point? An explicit break construct would be slightly
                          nicer for readability, but it needs to identify which loop it's
                          breaking from, so it needs some kind of label. (Having to count how
                          many loops to break from would be like a goto where you have to say
                          how many lines to skip.) If you use a sensible label ("a_loop_end "
                          for example) a goto conveys the intention quite clearly.

                          -- Richard

                          Comment

                          • Neil Kurzman

                            #14
                            Re: Double break



                            Martin Johansen wrote:
                            [color=blue]
                            > Hello group.
                            >
                            > Are there any efficient way to double 'break' a 2-dimensional loop
                            > without using a jump, an extra variable or midifying i and j?
                            >
                            > e.g.
                            >
                            > for(i = 0; i < x; i++)
                            > for(j = 0; j < y; j++){
                            > z;
                            > break(2); /* i know the 2 does not help */
                            > }[/color]

                            how about?

                            done =0;
                            for(i = 0; i < x; i++)
                            {
                            for(j = 0; j < y; j++)
                            {
                            z;
                            if(?????)
                            {
                            done =1;
                            break;
                            }
                            }
                            if(done)break;
                            }

                            My first teacher said gotos are evil, and if I used one I would be
                            condemned to an infinite loop.

                            Comment

                            • Yu SONG

                              #15
                              Re: Double break

                              Thomas Matthews wrote:[color=blue]
                              > Yu SONG wrote:
                              >[color=green]
                              >> Trevor Fancher wrote:
                              >>[color=darkred]
                              >>> Martin Johansen wrote:
                              >>>
                              >>>
                              >>>> Hello group.
                              >>>>
                              >>>> Are there any efficient way to double 'break' a 2-dimensional loop
                              >>>> without using a jump, an extra variable or midifying i and j?
                              >>>>
                              >>>> e.g.
                              >>>>
                              >>>> for(i = 0; i < x; i++)
                              >>>> for(j = 0; j < y; j++){
                              >>>> z;
                              >>>> break(2); /* i know the 2 does not help */
                              >>>> }
                              >>>
                              >>>
                              >>>
                              >>>
                              >>> K&R suggests using goto in section 3.8 . They say it is about the only
                              >>> time it is okay to use goto.
                              >>>
                              >>> -Trevor
                              >>>[/color]
                              >>
                              >> Or you can put them in a function,
                              >>
                              >> Replace the "break(2)" by a "return ABC" statement;
                              >>
                              >>[/color]
                              >
                              > The problem is that a return will exit the function.
                              > If there is more processing after the for loops, your
                              > solution will not allow the execution of stuff after
                              > the for loops.
                              >
                              >[/color]

                              The code after the "for" loops can be placed in the routine where this
                              function is called. See this simple program below,

                              int forloop() {
                              for ...
                              for ...
                              return 1;
                              return 0;
                              }

                              int main() {
                              if (forloop() == 0)
                              /* processing for expected output */
                              else
                              /* processing for unexpected output */
                              }



                              --
                              Song

                              /* E-mail.c */
                              #define User "Y.Song"
                              #define Warwick "dcs.warwick.ac .uk"
                              int main() {
                              printf("Yu Song's E-mail: %s@%s", User, Warwick);
                              return 0;}

                              Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
                              _______________ _______________ _______________ __________

                              Comment

                              Working...