In this lesson, we shall learn how to create Visual Basic 2017 console applications using the If..Then...Else control structure and the Select Case control structure.
The If...Then....Else structure is as follows:
If conditions Then vb expressions Else vb expressions End If
The following code uses If....Then....Else control structure to evaluate the answer entered by the user.
Sub Main() Dim x, y, z, total As Single Dim firstnum As String Dim secondnum As String Dim sum As String firstnum = InputBox("Enter a Number") secondnum = InputBox("Enter a number") sum = InputBox("The answer is") total = Val(firstnum) + Val(secondnum) If total = Val(sum) Then MsgBox("Correct") Else MsgBox("Wrong") End If End Sub
When we run the application, you will be presented with an input box as shown in Figure 38.1
After entering a number, you will be presented a second input box where you are required to enter the second number. After that, you will be presented with a third input box that prompts you to enter the answer which supposes to be the sum of the two numbers entered, as shown in Figure 38.2
If the answer is correct, a message box will display the word 'Correct', otherwise, it will show the word 'Wrong', as shown in Figure 38.3
The Select Case control structure evaluates one expression for multiple values. Select Case is preferred when there exist multiple conditions.
The Select Case control structure is shown below:
Select Case expression Case value1 Block of one or more vb statements Case value2 Block of one or more vb Statements . Case Else Block of one or more vb Statements End Select
This application allows the user to input an examination grade and it will display the result in a message box, as shown in the figures below: