Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Best way of breaking from for loop

Best way of breaking from for loop

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vipin_nvk
    wrote on last edited by
    #1

    I have an example of for loop like

    for( int I = 0; I < 100; i++)
    {
    //
    //
    //
    If (condition)
    {
    //how to break when this condition is satisfied
    Method1. Write ‘break’ here or
    Method2. I = 100 (make I equal to 100)
    }
    }

    Which method is efficient to use? Why? Thanx

    C R P CPalliniC S 6 Replies Last reply
    0
    • V vipin_nvk

      I have an example of for loop like

      for( int I = 0; I < 100; i++)
      {
      //
      //
      //
      If (condition)
      {
      //how to break when this condition is satisfied
      Method1. Write ‘break’ here or
      Method2. I = 100 (make I equal to 100)
      }
      }

      Which method is efficient to use? Why? Thanx

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Imho break is more efficient because using break will just jump out of the loop while setting the variable 'I' to a value has to perform memory read/write operations, then it will jump to the condition-evaluation part, where it will check the variable against 100 and will then jump out of the loop. Am not sure how smart the compiler is but it might even optimize the later case to a simple break or "value-set" then break itself.

      > The problem with computers is that they do what you tell them to do and not what you want them to do. <

      1 Reply Last reply
      0
      • V vipin_nvk

        I have an example of for loop like

        for( int I = 0; I < 100; i++)
        {
        //
        //
        //
        If (condition)
        {
        //how to break when this condition is satisfied
        Method1. Write ‘break’ here or
        Method2. I = 100 (make I equal to 100)
        }
        }

        Which method is efficient to use? Why? Thanx

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        I would use a control variable. I strongly advise not to set the loop variable, 'I' in your case, to a bogus value just to exit the loop because you might want to use the variable after the loop. Something like this:

        bool bStop = false;
        int nCount;
        for( nCount = 0; (nCount < 100) && !bStop; ++nCount )
        {
        if( condition )
        {
        bSstop = true;
        }
        }

        // Here you'll be able to tell how far the loop has reached if you need to
        if( nCount < 100 )
        {
        // Take proper action, whatever that may be....
        }

        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        1 Reply Last reply
        0
        • V vipin_nvk

          I have an example of for loop like

          for( int I = 0; I < 100; i++)
          {
          //
          //
          //
          If (condition)
          {
          //how to break when this condition is satisfied
          Method1. Write ‘break’ here or
          Method2. I = 100 (make I equal to 100)
          }
          }

          Which method is efficient to use? Why? Thanx

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          Either one is fine. I prefer to go the break; route.

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

          1 Reply Last reply
          0
          • V vipin_nvk

            I have an example of for loop like

            for( int I = 0; I < 100; i++)
            {
            //
            //
            //
            If (condition)
            {
            //how to break when this condition is satisfied
            Method1. Write ‘break’ here or
            Method2. I = 100 (make I equal to 100)
            }
            }

            Which method is efficient to use? Why? Thanx

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            The break keyword was designed just for this. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            K 1 Reply Last reply
            0
            • V vipin_nvk

              I have an example of for loop like

              for( int I = 0; I < 100; i++)
              {
              //
              //
              //
              If (condition)
              {
              //how to break when this condition is satisfied
              Method1. Write ‘break’ here or
              Method2. I = 100 (make I equal to 100)
              }
              }

              Which method is efficient to use? Why? Thanx

              S Offline
              S Offline
              sheshidar
              wrote on last edited by
              #6

              I do agree with cPallini. Use break for your condition.

              CPalliniC 1 Reply Last reply
              0
              • V vipin_nvk

                I have an example of for loop like

                for( int I = 0; I < 100; i++)
                {
                //
                //
                //
                If (condition)
                {
                //how to break when this condition is satisfied
                Method1. Write ‘break’ here or
                Method2. I = 100 (make I equal to 100)
                }
                }

                Which method is efficient to use? Why? Thanx

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Resetting your loop counter variable is the worst thing to do. In this simple example, it might seem all OK to you, but what if there are other routines that will depend on the counter variable after the loop is complete? What if you needed to know how many times the loop executed?

                vipin_nvk wrote:

                Which method is efficient to use? Why?

                Just exiting out of the loop with a break would be the ideal way, IMHO. You may use a control variable, as another user pointed out as well.

                It is a crappy thing, but it's life -^ Carlo Pallini

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  The break keyword was designed just for this. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  K Offline
                  K Offline
                  killabyte
                  wrote on last edited by
                  #8

                  i use the break key word in my virii like so... if(DeployCondition) break ThatGuysPC->HDD;

                  CPalliniC 1 Reply Last reply
                  0
                  • S sheshidar

                    I do agree with cPallini. Use break for your condition.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    I'm glad about your enthusiasm. :-D BTW it is CPallini, since C stands either for Carlo or the C programming language :rolleyes: , both requiring the capital letter. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • K killabyte

                      i use the break key word in my virii like so... if(DeployCondition) break ThatGuysPC->HDD;

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      killabyte wrote:

                      virii

                      Should be viruses (even if my ancestors didn't foresee such a proliferation :rolleyes: of). :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups