### Les boucles dans VBA

Les différentes façons d'exprimer tant que condition répéter.
Les 5 façons sont : While cond ... Wend Do ... while cond Loop Do ... Loop while cond Do until cond ... Loop Do ... Loop until cond

Code source:

Option Explicit

Function f(x As Single) As Single
'f=3*x^3 + 3*x^2 - 9*x
f = x ^ 3 + 3 * x ^ 2 - 9 * x
End Function

Function df(x As Single) As Single
'f'=9*x^2 + 6*x - 9
df = 3 * x ^ 2 + 6 * x - 9
End Function

Function newton(x0 As Single) As Single
Dim pre As Single
Dim cond As Boolean
pre = 0.001
cond = False

If Abs(f(x0)) > pre Then
Do
'Do Until Not cond
'Do
'Do While cond
'While cond
If df(x0) = 0 Then
x0 = x0 + pre
Else
x0 = x0 - f(x0) / df(x0)
cond = Not (IIf(Abs(f(x0)) < pre, False, True))
End If
'Wend
'Loop
'Loop While cond
'Loop
Loop Until cond
End If
newton = x0
End Function

Sub test()
Dim x0 As Single
x0 = 1
Debug.Print "x0=" & x0 & ",f(x0)=" & f(x0)
Debug.Print "x0=" & x0 & ",f'(x0)=" & df(x0)
Debug.Print "Solution=" & newton(x0)
End Sub