开发者

When does the while loop check for its condition

开发者 https://www.devze.com 2023-03-10 11:43 出处:网络
Hi I have something like: Version 1: bool noErrors = 开发者_运维技巧true; while(noErrors) { noErrors = ValidateControl(txtName);

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.

bool noErrors = true;
while(noErrors) 
{    
    noErrors = ValidateControl(txtName);     
    // other code 
}

Checks before each iteration.

bool noErrors = true;
do 
{    
    noErrors = ValidateControl(txtName);     
    // other code 
} while(noErrors);

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 txtName likely to change during the execution of the loop? Would some other limiting condition be more useful?

while (ValidateControl(txtName)) 
{
    // other code 
} 

If the validity of txtName will not change, consider,

if (ValidateControl(txtName))
{
  while(/*Some other condition*/)
  {
      // other code
  }
}


The condition is only ever checked at the start of any possible iteration. So in version 1 the "other code" would be executed even if noErrors has been set to false in the first line of the body... whereas in version 2 it wouldn't... but noErrors looks like it's somewhat useless in version 2.

Could you change it to:

while (ValidateControl(txtName))
{
    // other code
}

?


A while loop evaluates its condition before the first iteration, and inbetween each subsequent iteration. The condition is never evaluated inside the loop body.


It checks it before running again (first time, after first run and so on). You have to break or whole chunk of code will run.


The while loop checks the condition before it iterates over the block of code that it precedes. You can also make it check the condition at the end by using a do-while construct. Your version #2 will produce the result you desire.


The while loop checks the condition before executing the entire code block. If you want to break execution before the other code is executed, use Version 2.


The loop condition is only evaluated at the start of each loop, so in your first version, the "other code" will still be executed, even if ValidateControl returns false.

Your second version works better, and will not run the "other code" if ValidateControl returns false, however it also does not set noErrors to false if the validation fails. If that's not important, and noErrors is only the loop condition, then you might as well change your while loop to while(true), if it is used later in the code, then you'll need to change version 2 slightly:

bool noErrors = true;
while(noErrors)
{
    if(!ValidateControl(txtName))
    {
        noErrors = false;
        break;
    }

    // other code
}


If this is in a validation routine, I wouldn't even BOTHER with a WHILE() construct... In the past, I would typically test each specific validation routine that did just that... no looping involved such as

Function bool IsAllDataValid()
{
    if ( ! (ValidateControl(txtName) )
        return false;

    if ( ! (ValidateControl(OtherField ))
        return false;

    etc...

    return true;
}

Then you don't have to worry about where the mix is of include or bypass certain blocks of code... You could just have...

if IsAllDataValid()
{
   Do Your Other Code
}
0

精彩评论

暂无评论...
验证码 换一张
取 消