Ask Your Question
0

Can I have variable assignement in While Loop condition?

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

Lets have simple loop:

value=srcSheet.getCellByPosition(0,srcRowNo).String
DO While value<>"" 
    srcRowNo=srcRowNo+1
    value=srcSheet.getCellByPosition(0,srcRowNo).String
Loop

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<>"" 
    srcRowNo=srcRowNo+1
Loop
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

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
0

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

pierre-yves samyn gravatar image

Hi

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 <> ""
    srcRowNo=srcRowNo+1
wend

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

Regards

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 68 times

Last updated: Jan 26 '17