Flow control
In this part of the Visual Basic tutorial, we will talk about the flow control. We will define several keywords that enable us to control the flow of the Visual Basic program.In Visual Basic language there are several keywords that are used to alter the flow of the program. When the program is run, the statements are executed from the top of the source file to the bottom. One by one. This flow can be altered by specific keywords. Statements can be executed multiple times. Some statements are called conditional statements. They are executed only if a specific condition is met.
The If statement
TheIf
statement has the following general form: If (expression)The
statement
End If
If
keyword is used to check if an expression is true. If it is true, a statement is then executed. The statement can be a single statement or a compound statement. A compound statement consists of multiple statements enclosed by the If/End If block. Option Strict OnWe have a num variable. It is assigned 31. The
Module Example
Dim num As Byte = 31
Sub Main()
If (num > 0)
Console.WriteLine("num variable is positive")
End If
End Sub
End Module
If
keyword checks for a boolean expression. The expression is put between square brackets. 31 > 0 is true, so the statement inside the block is executed. $ ./ifstatement.exeThe condition is met and the message is written to the console.
num variable is positive
Option Strict OnMore statements can be executed inside the block, created by the
Module Example
Dim num As Byte = 31
Sub Main()
If (num > 0)
Console.WriteLine("num variable is positive")
Console.WriteLine("num variable equals {0}", num)
End If
End Sub
End Module
If
, End If
keywords. We can use the
Else
keyword to create a simple branch. If the expression inside the square brackets following the If keyword evaluates to false, the statement following the Else
keyword is automatically executed. Option Strict OnWe have a sex variable. It has "female" string. The boolean expression evaluates to false and we get "It is a girl" in the console.
Module Example
Dim sex As String
Sub Main()
sex = "female"
If (sex = "male")
Console.WriteLine("It is a boy")
Else
Console.WriteLine("It is a girl")
End If
End Sub
End Module
$ ./branch.exeWe can create multiple branches using the
It is a girl
Else If
keyword. The Else If
keyword tests for another condition, if and only if the previous condition was not met. Note, that we can use multiple Else If
keywords in our tests. Option Strict OnWe have a numerical variable and we test it, if it is a negative number or positive or if it equals to zero. The first expression evaluates to false. The second condition is met. The program prints 'a equals to zero' to the console. The rest of the branch is skipped.
Module Example
Dim a As Byte = 0
Sub Main()
If (a < 0)
Console.WriteLine("a is negative")
Else If (a = 0)
Console.WriteLine("a equals to zero")
Else
Console.WriteLine("a is a positive number")
End If
End Sub
End Module
Select statement
TheSelect
statement is a selection control flow statement. It allows the value of a variable or expression to control the flow of program execution via a multi way branch. It creates multiple branches in a simpler way than using the combination of If
, Else If
statements. We have a variable or an expression. The
Select
keyword is used to test a value from the variable or the expression against a list of values. The list of values is presented with the Case
keyword. If the values match, the statement following the Case
is executed. There is an optional Case Else
statement. It is executed, if no other match is found. Option Strict OnIn our program, we have a domain variable. We read a value for the variable from the command line. We use the
Module Example
Dim domain As String
Sub Main()
domain = Console.ReadLine()
Select domain
Case "us"
Console.WriteLine("United States")
Case "de"
Console.WriteLine("Germany")
Case "sk"
Console.WriteLine("Slovakia")
Case "hu"
Console.WriteLine("Hungary")
Case Else
Console.WriteLine("Unknown")
End Select
End Sub
End Module
Case
statement to test for the value of the variable. There are several options. If the value equals for example to "us" the "United States" string is printed to the console. $ ./selectcase.exeWe have entered "hu" string to the console and the program responded with "Hungary".
hu
Hungary
The
Select
keyword enables to validate a range of numerical cases. Option Strict OnThe preceding program uses range of numerical values to identify an age group of a person.
Module Example
Dim age As Byte
Sub Main()
Try
age = Console.ReadLine()
Catch
Console.WriteLine("Invalid value")
End
End Try
Select age
Case 0 To 21
Console.WriteLine("Junior")
Case 22 To 60
Console.WriteLine("Adult")
Case Else
Console.WriteLine("Senior")
End Select
End Sub
End Module
TryA value is read from the console. We can use only numerical data. The
age = Console.ReadLine()
Catch
Console.WriteLine("Invalid value")
End
End Try
Try
, Catch
, End Try
keywords are used for exception handling. If an exception is thrown, the statements following the Catch
keyword are executed. The End
statement terminates the program. Case 0 To 21Here we specify a range of values. If the value entered by the user is in between 0 and 21, inclusive, then the program prints "Junior" to the console.
Console.WriteLine("Junior")
$ ./agerange.exeWe have entered 43 and the program responded with the "Adult" string.
43
Adult
The While statement
TheWhile
statement is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. This is the general form of the
While
loop: While (expression):The
statement
End While
While
keyword executes the statements inside the block enclosed by the While
, End While
keywords. The statements are executed each time the expression is evaluated to true. Option Strict OnIn the code example, calculate the sum of values from a range of numbers.
Module Example
Sub Main()
Dim i As Integer = 0
Dim sum As Integer = 0
While i < 10
i = i + 1
sum += i
End While
Console.WriteLine(sum)
End Sub
End Module
The
While
loop has three parts. Initialization, testing and updating. Each execution of the statement is called a cycle. Dim i As Integer = 0We initiate the i variable. It is used as a counter.
While i < 10The expression following the
...
End While
While
keyword is the second phase, the testing. The statements in the body are executed, until the expression is evaluated to false. i = i + 1The last, third phase of the
While
loop. The updating. We increment the counter. Note that improper handling of the While
loops may lead to endless cycles. It is possible to run the statement at least once. Even if the condition is not met. For this, we can use the
Do
, Loop While
keywords. Option Strict OnFirst the iteration is executed and then the truth expression is evaluated.
Module Example
Sub Main()
Dim count As Integer = 0
Do
Console.WriteLine(count)
Loop While (count <> 0)
End Sub
End Module
The For Next statements
When the number of cycles is know before the loop is initiated, we can use theFor Next
statements. In this construct we declare a counter variable, which is automatically increased or decreased in value during each repetition of the loop. Option Strict OnIn this example, we print numbers 0..9 to the console.
Module Example
Sub Main()
For i As Integer = 0 To 9
Console.WriteLine(i)
Next
End Sub
End Module
For i As Integer = 0 To 9We initiate the counter i to zero. The
Console.WriteLine(i)
Next
Next
statement increases the counter by one until the counter equals to 9. Visual Basic has an optional
Step
keyword. It controls how the counter variable is going to be increased or decreased. Option Strict OnIn the above example, we print numbers 0..9 in the reverse order.
Module Example
Sub Main()
For i As Integer = 9 To 0 Step -1
Console.WriteLine(i)
Next
End Sub
End Module
For i As Integer = 9 To 0 Step -1The step may be a negative number too. We initiate the counter to 9. Each iteration the counter is decreased by the step value.
Console.WriteLine(i)
Next
The For Each statement
TheFor Each
construct simplifies traversing over collections of data. It has no explicit counter. The For Each
statement goes through the array or collection one by one and the current value is copied to a variable defined in the construct. Option Strict OnIn this example, we use the
Module Example
Sub Main()
Dim planets() As String = { "Mercury", "Venus", _
"Earth", "Mars", "Jupiter", "Saturn", _
"Uranus", "Neptune" }
For Each planet As String In planets
Console.WriteLine(planet)
Next
End Sub
End Module
For Each
statement to go through an array of planets. For Each planet As String In planetsThe usage of the
Console.WriteLine(planet)
Next
For Each
statement is straightforward. The planets is the array, that we iterate through. The planet is the temporary variable, that has the current value from the array. The For Each
statement goes through all the planets and prints them to the console. $ ./planets.exeRunning the above Visual Basic program gives this output.
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
The Exit, Continue statements
TheExit
statement can be used to terminate block defined by While
, For
or Select
statements. Option Strict OnWe define an endless
Module Example
Dim val As Integer
Sub Main
While (True)
val = CType((30 * Rnd), Integer) + 1
Console.Write(val.ToString & " ")
If (val = 22)
Exit While
End If
End While
Console.Write(vbNewLine)
End Sub
End Module
While
loop. There is only one way to jump out of a such loop. We must use the Exit While
statement. We choose a random value from 1 to 30. We print the value. If the value equals to 22, we finish the endless while loop. $ ./exitstm.exeWe might get something like this.
30 12 13 20 19 4 2 9 6 9 22
The
Continue
statement is used to skip a part of the loop and continue with the next iteration of the loop. It can be used in combination with Do
, For
and While
statements. In the following example, we will print a list of numbers, that cannot be divided by 2 without a remainder.
Option Strict OnWe iterate through numbers 1..999 with the
Module Example
Dim num As Integer = 0
Sub Main()
While (num < 1000)
num = num + 1
If ((num Mod 2) = 0)
Continue While
End If
Console.Write(num.ToString() + " ")
End While
Console.Write(vbNewLine)
End Sub
End Module
While
loop. If ((num Mod 2) = 0)If the expression num Mod 2 returns 0, the number in question can be divided by 2. The
Continue While
End If
Continue
statement is executed and the rest of the cycle is skipped. In our case, the last statement of the loop is skipped and the number is not printed to the console. The next iteration is started. In this part of the Visual Basic tutorial, we were talking about control flow structures.
0 comments:
Post a Comment