Chapter 4: Making Decisions
4.1 Decision Structures
- Decision structures (selection structures) let code choose between alternative actions
- If...Then is the simplest: If condition Then action
- Conditions use relational operators: =, <>, <, >, <=, >=
- Conditions evaluate to True or False β Boolean expressions
- Decisions control program flow at runtime
4.2 If...Then Statements
- Syntax: If condition Then statement
- Single-line: If intTemperature > 40 Then lblMessage.Text = "Warm day"
- Multi-line: If condition Then ... End If
- Use comparison on controls: txtInput.Text, numeric values
- Val() function converts string to number: Val(txtAge.Text)
4.3 If...Then...Else
- Syntax: If condition Then ... Else ... End If
- Handles both true and false branches
- If intScore >= 70 Then lblGrade.Text = "Pass" Else lblGrade.Text = "Fail" End If
- Can contain multiple statements in each block
- Only ONE branch executes β never both
4.4 Nested If Statements
- An If block inside another If block (or Else block)
- Used for multi-level decisions: A β B β C
- If condition1 Then ... If condition2 Then ... End If ... End If
- Be careful with matching End If for each level
- Indentation is critical for readability
4.5 Logical Operators
- And: both conditions must be True
- Or: at least one condition must be True
- Not: reverses TrueβFalse (Not condition)
- Xor: exactly one must be True (rarely used)
- AndAlso / OrElse β short-circuit evaluation (VB.NET, not classic VB)
4.6 Comparing Strings
- Strings compared alphabetically (lexicographically)
- Case-sensitive by default: "Hello" <> "hello"
- Use UCase() or LCase() to normalize: UCase(strA) = UCase(strB)
- String.Compare(strA, strB, True) β True = ignore case
- Option Compare Text changes default to case-insensitive
4.7 Select Case
- Alternative to nested If for multiple distinct values
- Syntax: Select Case expression ... Case value ... Case Else ... End Select
- Case 1 To 5 β range
- Case Is >= 90 β comparison with Is keyword
- Case "A", "B" β multiple values (comma-separated)
- Case Else β optional catch-all (like default)
4.8 Input Validation
- Validate user input before processing
- Check for empty text: txtInput.Text = "" or txtInput.Text.Length = 0
- Check numeric input: IsNumeric(txtInput.Text)
- Use TryParse for type-safe checking: Integer.TryParse(txtInput.Text, num)
- Provide feedback (MessageBox) for invalid input
- Prevent crashes from bad data at runtime
π‘ Key Tip: Practice tracing through If/Then/Else and Select Case blocks by hand. Write down what each line does before running. This is the #1 way to prepare for decision-structure questions.
π Practice Quiz β Chapter 4
10 questions. Select your answer, then check the result. All key concepts from Chapter 4.
π Flashcards β Chapter 4
20 cards. Click the card to flip. Use buttons to navigate.
Press a button to start
Card 1 of 20
π Cheat Sheet β Chapter 4
π Decision Structure Syntax
If condition Then
statements
End If
If condition Then
statements
Else
statements
End If
If condition1 Then
If condition2 Then
statements
End If
End Ifπ Logical Operators
A And B β True only if BOTH are True A Or B β True if EITHER is True Not A β Flips TrueβFalse A Xor B β True if EXACTLY ONE is True
π Select Case
Select Case expression
Case value1
' code
Case 1 To 5 ' range
Case Is >= 90 ' comparison
Case "A", "B" ' multiple values
Case Else ' catch-all
End Selectπ Input Validation
' Check for empty
If txtInput.Text = "" Then
MessageBox.Show("Enter a value")
End If
' Check if numeric
If IsNumeric(txtInput.Text) Then
' safe to use Val()
End If
' Best: TryParse
Dim num As Integer
If Integer.TryParse(txtInput.Text, num) Then
' num has the value
Else
MessageBox.Show("Invalid number")
End Ifπ String Comparison
Case-sensitive: "Hello" = "HELLO" β False
Ignore case: UCase("Hello") = UCase("HELLO") β True
Option Compare Text β makes all comparisons case-insensitiveTip: If the quiz has code traces, write the variable values on scratch paper as you step through each line.