RE: Creating a Do While loop with multiple conditions is half working

Answered

Hi,

I have a script that needs to search through a table to find the right row for a certain bit of data, and return the row number.  To do this I have included a Do While loop in my script.

I want the loop to iterate no more than 12 rows of data in the table.  I am also using a boolean to identify if the script has found the text.  In my while loop I want the script to loop until the boolean is true OR the iterations have reached 12.  So I came up with the following code.


int xPathVar = 0;

boolean correctOne = false;

do {

if (text is found in the table) {

correctOne = true;

} else {

xPathVar = xPathVar+1;

}
} while (correctOne == false || xPathVar < 12);

When the script runs, and the text is found in the table (enter the If statement to make correctOne = true), the loop exits as expected.

However, when the text is not found in the first 12 rows, the script does not exit when xPathVar reaches 12 and the script keeps running in an infinite loop.  I have walked through the code in debug mode and have confirmed that the xPathVar is incrementing correctly.

I am using || in the While statement as I am of the understanding that it represents OR.

Is my logic correct?  Is there another way that I could have written the While statement to say
“Continue loop until ‘correctOne’ = true OR xPathVar is less than 12”

Thanks

Simon

SimonM Train Asked on September 18, 2014 in Selenium WebDriver.

Please mark your question as ANSWERED, if your query is resolved.

Regards,
Lakshay Sharma

on September 18, 2014.
Add Comment
1 Answers
Best answer

Hi Simon,

What is happening is the while checks for the condition from left to right. It means first it will check if correctOne == false, If this condition is true, it will not bother to check the next condition.

If your case what is happening is that your program did not find the text in first 12 iterations. As a result for remaining iteration correctOne will always be false. This means that the loop will never check the second condition and it will keep executing.

What you want to do is that correctOne == true and XPathVar < 12.

Thanks
Virender

Professor Answered on September 18, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.