Solved

Breaking from a do loop when a condition is not met

Posted on 2014-01-26
4
285 Views
Last Modified: 2014-01-26
I need to continue a do loop until a condition is met:

          // Get random Flop1 card
            string strImageName = "";
            int intCardNumber = 0;
            do
            {
                Random random = new Random();
                intCardNumber = random.Next(1, 27);
                strImageName = arrCards[intCardNumber, 0] + "Full";
 
// if condition 1 is true or condition 2 is true, then break out of the loop.
// condition 1 (txtHole1.Text != strImageName)
// condition 2  (txtHole2.Text != strImageName)

             
 if ((txtHole1.Text != strImageName) || (txtHole2.Text != strImageName)) // Make sure that Hole1 or Hole2 and strImageName are not the same
                {
                    break;  // do not get a new random number.
                }
            } while ((txtHole1.Text != strImageName) || (txtHole2.Text != strImageName));

// Does not break when Condition 1 or Condition 2 is false.

Please help.

Thanks,
0
Comment
Question by:Dovberman
  • 3
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39810360
You must think like a computer.

What is OR for you should often be translated to AND for the computer.

What you are doing is to loop as long as one of the conditions is true. So if only one is false, the loop goes on.

What your need to do is to loop as long as both are true. So when one is false, the loop will stop.

Replace || by &&.
0
 

Author Comment

by:Dovberman
ID: 39810384
if ((txtHole1.Text != strImageName) && (txtHole2.Text != strImageName)) // Make sure that Hole1 or Hole2 and strImageName are not the same
                {
                    break;  // do not get a new random number.
                }
            } while ((txtHole1.Text != strImageName) && (txtHole2.Text != strImageName));

Yes, this makes sense.  I will try it and give you feedback.

Thanks,
0
 

Author Comment

by:Dovberman
ID: 39810409
Yes, that works.

Thanks,
0
 

Author Closing Comment

by:Dovberman
ID: 39810410
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now