C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
While: In ComputePower we iterate through the exponents. We compute the running total and store it in the local variables.
Yield: With the Yield keyword we "yield" control of ComputePower to Main. When next called, we resume after Yield.
VB.NET program that uses Iterator, Yield
Module Module1
Sub Main()
' Loop over first 10 exponents.
For Each value As Integer In ComputePower(2, 10)
Console.WriteLine(value)
Next
End Sub
Public Iterator Function ComputePower(
ByVal number As Integer,
ByVal exponent As Integer) As IEnumerable(Of Integer)
Dim exponentNum As Integer = 0
Dim numberResult As Integer = 1
' Yield all numbers with exponents up to the argument value.
While exponentNum < exponent
numberResult *= number
exponentNum += 1
Yield numberResult
End While
End Function
End Module
Output
2
4
8
16
32
64
128
256
512
1024
However: It is important to know these are not low-level features. They use keywords and short syntax, but much code is needed.