How to exit a while loop inside of a while loop
-
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
-
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
Why Repost ?
Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
-
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
-
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 isgoto
and there is nogosub
in C#.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
-
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
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 withApplication.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. -
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 withApplication.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.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
-
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
-
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
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.