Hi I have something like:
Version 1:
bool noErrors = true;
while(noErrors)
{
noErrors = ValidateControl(txtName);
// other code
}
Version 2:
bool noErrors = true;
while(noErrors)
{
if(!ValidateControl(txtName)) break;
// other code
}
I use this code to validate a form and if the validation returns false, I want to break before executing “other code”. Since I do not know when the loop checks for its condition, I do not know which makes more sense. Should I use the first or the second version, or maybe a third one?
Thank you for your time
Version 2 will break before running the
//other code. Version 1 will not check until the start of the next iteration.Checks before each iteration.
Checks after each iteration.
Neither check during an iteration. As stated by the other answerers, the following code simplifies the example but makes me ask the question, is the validity of
txtNamelikely to change during the execution of the loop? Would some other limiting condition be more useful?If the validity of
txtNamewill not change, consider,