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#
  4. How to exit a while loop inside of a while loop

How to exit a while loop inside of a while loop

Scheduled Pinned Locked Moved C#
helpquestioncsharpdatabasetutorial
8 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.
  • T Offline
    T Offline
    TheJudeDude
    wrote on last edited by
    #1

    For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database

    reader.ExecuteReader()

    method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!

    Jude

    A L 3 Replies Last reply
    0
    • T TheJudeDude

      For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database

      reader.ExecuteReader()

      method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!

      Jude

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Why Repost ?

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      1 Reply Last reply
      0
      • T TheJudeDude

        For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database

        reader.ExecuteReader()

        method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!

        Jude

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        break is the usual way to exit a while loop prematurely, what is inelegant about it ? The only thing that you should try to avoid is goto and there is no gosub in C#.

        L 1 Reply Last reply
        0
        • L Lost User

          break is the usual way to exit a while loop prematurely, what is inelegant about it ? The only thing that you should try to avoid is goto and there is no gosub in C#.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Shameel wrote:

          break is the only way to exit a while loop prematurely

          really? what about goto label, return, throw new Exception(...), Application.Exit() and then some? :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          L 1 Reply Last reply
          0
          • L Luc Pattyn

            Shameel wrote:

            break is the only way to exit a while loop prematurely

            really? what about goto label, return, throw new Exception(...), Application.Exit() and then some? :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Yes, I am aware of them, but: goto should be avoided as it leads to some hard to debug code paths. return takes you out of the method itself, what if you want to do something after the while loop ? throw should be used to signal an abnormal state and not to transfer control And you know what the problem is with Application.Exit(), the whole process is brought down and you're thrown out of your app. I did not want to confuse the OP with all these, he/she was asking for a simple method to prematurely exit a while loop.

            L 1 Reply Last reply
            0
            • L Lost User

              Yes, I am aware of them, but: goto should be avoided as it leads to some hard to debug code paths. return takes you out of the method itself, what if you want to do something after the while loop ? throw should be used to signal an abnormal state and not to transfer control And you know what the problem is with Application.Exit(), the whole process is brought down and you're thrown out of your app. I did not want to confuse the OP with all these, he/she was asking for a simple method to prematurely exit a while loop.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Shameel wrote:

              I did not want to confuse the OP

              then don't issue a statement that is completely wrong. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              L 1 Reply Last reply
              0
              • L Luc Pattyn

                Shameel wrote:

                I did not want to confuse the OP

                then don't issue a statement that is completely wrong. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You are probably correct, I have rephrased my answer. Thanks for pointing it out :)

                1 Reply Last reply
                0
                • T TheJudeDude

                  For all, this is a question on the best/most elegant way to do this. I am reading records from a database. I start the read with a database

                  reader.ExecuteReader()

                  method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method. The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null. I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works! I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide. The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here. Thanx in advance!

                  Jude

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I don't quite understand your double while loop scenerio, but regardless, as far as I'm concerned, break is a Goto and they both should be burned at the stake. Why not set a flag and include it in your while conditional? I find it much easier to follow code that tells you outright what the conditions are for continuing the loop:

                  salesTotalLoopFinished=false;
                  while (!salesTotalLoopFinished && dbreader.read())

                  Perhaps a better route to take, though, is to let the db handle the calculation for you:

                  SELECT SUM(daily_sales) AS total_sales FROM storedata WHERE storeid=25

                  Then you can avoid all of this looping in the first place. Cheers, Drew.

                  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