The Student Room Group

Pre and post-Conditional Loops VBA

Can anyone please explain pre-and post-conditional loops for me please in relation to Visual Basic?
Reply 1
Original post by Hectic
Can anyone please explain pre-and post-conditional loops for me please in relation to Visual Basic?


They're no different from pre- and post-conditional loops in any other language (if you've programmed in other languages).

When you set up a loop whose execution is dependent on some condition you can either test that condition before entering the body of the loop or at the end of the loop. In the latter case the loop is guaranteed to be executed at least once.
Reply 2
Original post by davros
They're no different from pre- and post-conditional loops in any other language (if you've programmed in other languages).

When you set up a loop whose execution is dependent on some condition you can either test that condition before entering the body of the loop or at the end of the loop. In the latter case the loop is guaranteed to be executed at least once.

+1, thanks!

Can you give me examples please? :smile:
Reply 3
Original post by Hectic
+1, thanks!

Can you give me examples please? :smile:


Here's a couple of very basic examples in a subroutine:

Sub loops()

Dim i As Integer
Dim j As Integer

i = 1
' Precondition loop
Do While (i < 3)
i = i + 1
Loop
MsgBox "i = " & i, vbInformation

j = 1
' post-condition loop
Do
j = j - 1
Loop While (j > 4)
MsgBox "j = " & j, vbInformation
End Sub



Note that in the post-condition example the loop is always executed at least once even though j fails the test because the condition is not tested until after the loop is run!
Reply 4
Thank you so much!

Quick Reply