Ask Your Question

Can I have variable assignment in While Loop condition? [closed]

asked 2017-01-25 22:06:25 +0200

Antoniossss gravatar image

updated 2020-09-21 19:51:11 +0200

Alex Kemp gravatar image

Lets have simple loop:

DO While value<>"" 

is it possible assign variable in DO WHILE condition check so i dont have to read the value for the initial time?

Something like this does not work:

DO While value=srcSheet.getCellByPosition(0,srcRowNo).String<>"" 
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-09-21 19:49:12.531888

2 Answers

Sort by » oldest newest most voted

answered 2017-01-25 23:19:32 +0200

floris v gravatar image

Your first listing is the standard way to handle do while ... loop. You have to assign an initial value to the variable that you use in the condition for entering or continuing the loop, then it has to be updated. You cannot avoid entering the same code twice. Of course you also have to initialise srcRowNo.

There is an alternative to the do while ... loop, the repeat ... until. In that case the condition is tested at the end of the loop instead of at the start, so the loop will be executed at least once. But you still have to set the initial value for the condition outside the loop.

edit flag offensive delete link more

answered 2017-01-26 11:44:35 +0200

pierre-yves samyn gravatar image


If you use the Value variable only to test if the cell is empty, you can do without it:

while srcSheet.getCellByPosition(0,srcRowNo).String <> ""

Also, if I can afford one tip it is best not to use variable names that may be reserved words.


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-01-25 22:06:25 +0200

Seen: 157 times

Last updated: Sep 21 '20