4.1.1.2, 4.1.1.4, 4.1.1.5 - Selection and Boolean conditions (VB.NET)
Learn to turn requirements into comparisons and Boolean expressions, then select the right path with correct boundary behaviour. This is native written-code learning; the route is incomplete: runnable support pending.
Relational comparisons and boundaries
A relational comparison produces a Boolean value by comparing two values. VB.NET uses = for equal to and <> for not equal to; which meaning = has depends on its position. In score = 10, it assigns. In If score = 10 Then, it compares.
| VB.NET comparison | Meaning | Result when score is 10 |
|---|---|---|
score = 10 | Equal to | True |
score <> 10 | Not equal to | False |
score < 10 | Less than | False |
score > 10 | Greater than | False |
score <= 10 | Less than or equal to | True |
score >= 10 | Greater than or equal to | True |
A boundary is a value where required behaviour changes. If a pass requires at least 50, the condition is score >= 50, not score > 50. Check just below, exactly on and just above a boundary: 49, 50 and 51 reveal an off-by-one error that a score of 90 would miss. Compare values of suitable types; text "10" should not be confused with integer 10.
If score >= 50 Then
Console.WriteLine("Pass")
Else
Console.WriteLine("Retry")
End If
Selection evaluates the condition and executes one branch, then continues after End If. A branch may contain a sequence of several statements, so assignment and output can be combined in one choice.
Boolean operators and compound conditions
Boolean operators combine true/false values to express a rule involving several requirements. Use brackets to show the intended grouping instead of relying on remembered precedence.
| A | B | A And B | A Or B | A Xor B |
|---|---|---|---|---|
| False | False | False | False | False |
| False | True | False | True | True |
| True | False | False | True | True |
| True | True | True | True | False |
Not A reverses A's truth value. AND requires both conditions; inclusive OR requires at least one and allows both; XOR requires exactly one. For Boolean operands, VB.NET's Not, And, Or and Xor implement these operations. Do not use Xor for “one or both”.
Dim withinRange As Boolean = (age >= 12) And (age <= 18)
Dim hasPermission As Boolean = parentSigned Or guardianSigned
Dim oneTeamOnly As Boolean = redTeam Xor blueTeam
Dim canEnter As Boolean = withinRange And hasPermission And (Not banned)
For age 18, parentSigned = False, guardianSigned = True and banned = False, canEnter is True. Age 19 makes withinRange False regardless of the permission flags. A range requires two complete comparisons; write (age >= 12) And (age <= 18), not a chained mathematical comparison.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account