In this part of my Visual Basic Tutorial series we’ll look at If, Else, ElseIf, Select Case, Ternary Operator, For, For Each, Do Until, Do While, Arrays, Comparison Operators, Logical operators and much more.
All of the code and a transcript follows below.If you missed the first part of this series watch it first.
If you value videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
' Create a TextBox named TextBox1 and change Multiline to True ' ScrollBars to Vertical and WordWrap to True ' Create a Button named BtnUpdate ' Change Text size to 18 for both Public Class Form1 Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click ' ---------- CONDITIONALS ---------- Dim txtOutput As String = "" ' The If Statement is used to perform different actions ' depending on different conditions Dim intAge As Integer = 7 ' You can compare values with =, <>, >, <, >=, <= ' Single line If statement ' If you want multiple statements separate them with a : If intAge < 18 Then txtOutput &= "You can't vote" & Environment.NewLine : txtOutput &= "You can vote in " & 18 - intAge & " years" & Environment.NewLine If intAge < 5 Then txtOutput &= "To young for school" & Environment.NewLine ElseIf intAge = 5 Then txtOutput &= "Go to Kindergarten" & Environment.NewLine ' With logical operators Or and And you can check for ' multiple conditions ElseIf intAge > 5 And intAge < 18 Then Dim intGrade = intAge - 5 txtOutput &= "Go to grade " & intGrade & Environment.NewLine Else txtOutput &= "Go to college" & Environment.NewLine End If ' You can compare strings and ignore case Dim strName As String = "Derek" ' Compare returns 0 if they match If String.Compare("DEREK", strName, True) = 0 Then txtOutput &= "These Strings are Equal" & Environment.NewLine End If ' Select Case comparison operator Select Case intAge ' You can type single digits or multiple Case 1, 2, 3, 4 txtOutput &= "To young for school" & Environment.NewLine Case 5 txtOutput &= "Go to Kindergarten" & Environment.NewLine ' You can define a range Case 6 To 18 Dim intGrade = intAge - 5 txtOutput &= "Go to grade " & intGrade & Environment.NewLine Case Else ' This is the default action txtOutput &= "Go to college" & Environment.NewLine End Select ' The Ternary operator returns the 1st value if True ' and the 2nd if False Dim boolCanVote As Boolean = If(intAge >= 18, True, False) txtOutput &= "Can you vote : " & boolCanVote & Environment.NewLine ' ---------- LOOPING ---------- txtOutput = "" ' For loops execute the code within them a set number of times For intNum = 1 To 10 txtOutput &= intNum & Environment.NewLine Next ' The Step operator defines the increment amount ' You can also use a negative Step For intNum = 0 To 50 Step 10 txtOutput &= intNum & Environment.NewLine ' Exit For allows you to exit early If intNum = 40 Then Exit For Next ' For Each can cycle through each item in an array ' Create and fill an array with Strings Dim employees = New String() {"Paul", "Sally", "Jack"} For Each employee As String In employees txtOutput &= employee & Environment.NewLine Next Dim numberGuessed = 0 ' Generate Random Value Dim upperbound = 10 Dim lowerbound = 1 Dim randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound ' Do Until loops are used when you must have the code ' execute at least once ' The Do While loop continues looping as long as a condition ' is True Do Until numberGuessed = randomValue txtOutput &= "Computer Guessed " & numberGuessed & Environment.NewLine numberGuessed += 1 ' You can exit a Do loop early If numberGuessed = 10 Then Exit Do Loop txtOutput &= "The Computer Guessed it" & Environment.NewLine ' ---------- ARRAYS ---------- txtOutput = "" ' Define a fixed size array that can contain 3 elements Dim customers() As String = {"Bob", "Sally", "Manny"} ' Assign a value to the 3rd element customers(2) = "Paul" ' Get the value in an array txtOutput &= "#1 Customer " & customers(0) & Environment.NewLine ' Sort an array Array.Sort(customers) ' Reverse the array order Array.Reverse(customers) ' Change the size of the array and preserve values ' Without Preserve the original values would be deleted ReDim Preserve customers(5) ' Cycle through values in array For Each customer As String In customers txtOutput &= customer & Environment.NewLine Next 'Declare a multi-dimensional array and set values Dim matrix(4, 4) As String For i = 0 To 3 For j = 1 To 3 matrix(i, j) = i & j Next Next For i = 0 To 3 For j = 1 To 3 txtOutput &= matrix(i, j) & " " Next txtOutput &= Environment.NewLine Next TextBox1.Text = txtOutput End Sub End Class |
Leave a Reply