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. loop control

loop control

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 4 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.
  • R Offline
    R Offline
    rp_suman
    wrote on last edited by
    #1

    Hi, The code looks as follows: for() //main for loop { for() //for loop1 { if(condition) { //code } } for() //for loop2 { } for() //for loop3 { } } If the condition is false I want to iterate the main loop without running following for loops. How to do this? Best Regards, Suman

    C G 2 Replies Last reply
    0
    • R rp_suman

      Hi, The code looks as follows: for() //main for loop { for() //for loop1 { if(condition) { //code } } for() //for loop2 { } for() //for loop3 { } } If the condition is false I want to iterate the main loop without running following for loops. How to do this? Best Regards, Suman

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Just put the continue keyword if the condition is true:

      if (condition)
      continue;

      This will go to the next iteration of the current loop.

      Cédric Moonen Software developer
      Charting control [v1.3 - Updated]

      R 1 Reply Last reply
      0
      • R rp_suman

        Hi, The code looks as follows: for() //main for loop { for() //for loop1 { if(condition) { //code } } for() //for loop2 { } for() //for loop3 { } } If the condition is false I want to iterate the main loop without running following for loops. How to do this? Best Regards, Suman

        G Offline
        G Offline
        GauranG Shah
        wrote on last edited by
        #3

        You can use also goto in the following manner.

        :loop1 for() //main for loop
        {
        for() //for loop1
        {
        if(condition)
        {
        //code
        goto Loop1;

             }
          }
        

        for() //for loop2
        {
        }
        for() //for loop3
        {
        }
        }

        [ Screen Capture ][ Tool Tip ][ Muliple Desktops ][Greeting Card ]

        R 1 Reply Last reply
        0
        • C Cedric Moonen

          Just put the continue keyword if the condition is true:

          if (condition)
          continue;

          This will go to the next iteration of the current loop.

          Cédric Moonen Software developer
          Charting control [v1.3 - Updated]

          R Offline
          R Offline
          rp_suman
          wrote on last edited by
          #4

          Hi Thanks for the help, continue is iterating the current loop but not the main (outer) for loop. break will just break the current for loop but other loops following it will be executed. The "goto" is working, if its standard way I want use it. Thanks, Suman

          1 Reply Last reply
          0
          • G GauranG Shah

            You can use also goto in the following manner.

            :loop1 for() //main for loop
            {
            for() //for loop1
            {
            if(condition)
            {
            //code
            goto Loop1;

                 }
              }
            

            for() //for loop2
            {
            }
            for() //for loop3
            {
            }
            }

            [ Screen Capture ][ Tool Tip ][ Muliple Desktops ][Greeting Card ]

            R Offline
            R Offline
            rp_suman
            wrote on last edited by
            #5

            Hi, Thanks for the help!! Presently, I am using the "goto". "goto" seems smarter than checking the same condition in all for loops and "break"ing from them. With Thanks, Suman BTW,

            GauranG Shah wrote:

            :loop1 for() //main for loop

            Did you mean "loop1: for()", the colon after label?

            B 1 Reply Last reply
            0
            • R rp_suman

              Hi, Thanks for the help!! Presently, I am using the "goto". "goto" seems smarter than checking the same condition in all for loops and "break"ing from them. With Thanks, Suman BTW,

              GauranG Shah wrote:

              :loop1 for() //main for loop

              Did you mean "loop1: for()", the colon after label?

              B Offline
              B Offline
              BadKarma
              wrote on last edited by
              #6

              rp_suman wrote:

              Presently, I am using the "goto". "goto" seems smarter than checking the same condition in all for loops and "break"ing from them.

              Not really. The goto statement should rarely be used. It could be used when your application is really time critical. In any other case you should try to use condition.

              for() //main for loop
              {
               for() //for loop1
               {
                if(condition)
                {
                 //code
                }
               }
               if(!condition)
               {
                for() //for loop2
                {
                }
                for() //for loop3
                {
                }
               }  // end if(!condition)
              }
              

              codito ergo sum

              R 1 Reply Last reply
              0
              • B BadKarma

                rp_suman wrote:

                Presently, I am using the "goto". "goto" seems smarter than checking the same condition in all for loops and "break"ing from them.

                Not really. The goto statement should rarely be used. It could be used when your application is really time critical. In any other case you should try to use condition.

                for() //main for loop
                {
                 for() //for loop1
                 {
                  if(condition)
                  {
                   //code
                  }
                 }
                 if(!condition)
                 {
                  for() //for loop2
                  {
                  }
                  for() //for loop3
                  {
                  }
                 }  // end if(!condition)
                }
                

                codito ergo sum

                R Offline
                R Offline
                rp_suman
                wrote on last edited by
                #7

                Hi, Thanks for the help!! I will try this method.. Best Regards, Suman

                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