Lesson 38: Console Applications Part 2
Master If...Then...Else and Select Case structures in VB2019 console applications
Key Takeaway
Control structures like If...Then...Else and Select Case are essential for implementing decision-making logic in console applications.
In this lesson, you'll learn how to implement decision-making structures in Visual Basic 2019 console applications. We'll explore the If...Then...Else structure and the Select Case structure, which allow your programs to make decisions based on different conditions.
Conditional Logic
Execute different code based on conditions
Flow Control
Direct program execution paths
Multiple Conditions
Handle complex decision scenarios
Structured Code
Organize logic for better readability
38.1 Using If...Then...Else
The If...Then...Else structure allows your program to execute different blocks of code based on whether a condition is true or false. The basic structure is:
If condition Then ' Code to execute if condition is True Else ' Code to execute if condition is False End If
Example 38.1: Math Quiz Validation
This program asks the user to solve a simple addition problem and validates the answer using If...Then...Else:
Module Module1 Sub Main() Dim x, y, z, total As Single Dim firstnum As String Dim secondnum As String Dim sum As String firstnum = InputBox("Enter the first number") secondnum = InputBox("Enter the second number") sum = InputBox("Enter the sum of these numbers") total = Val(firstnum) + Val(secondnum) If total = Val(sum) Then MsgBox("Correct!") Else MsgBox("Wrong! The correct answer is " & total) End If End Sub End Module
Output
When you run this program:
Figure 38.1: Enter the first number
Figure 38.2: Enter the second number
Figure 38.3: Enter your answer
The program will then display whether your answer was correct or incorrect:
Figure 38.4: Correct answer message
38.2 Using Select Case
The Select Case structure is ideal when you need to evaluate one expression against multiple possible values. It provides a cleaner alternative to multiple If...Then...Else statements.
Select Case expression Case value1 ' Code for value1 Case value2 ' Code for value2 Case value3 ' Code for value3 Case Else ' Code for all other cases End Select
Example 38.2: Grade Evaluator
This program evaluates an examination grade and displays the corresponding result:
Module Module1 Sub Main() Dim grade As String grade = InputBox("Enter Grade (A, A-, B, C, D)") Select Case grade Case "A" MsgBox("High Distinction") Case "A-" MsgBox("Distinction") Case "B" MsgBox("Credit") Case "C" MsgBox("Pass") Case Else MsgBox("Fail") End Select End Sub End Module
Output
When you run this program, you'll be prompted to enter a grade:
Figure 38.5: Entering a grade
The program will then display the corresponding result:
Figure 38.6: Result for grade "B"
Lesson Summary
In this lesson, you've learned how to implement decision-making structures in Visual Basic 2019 console applications:
If...Then...Else
Implemented conditional logic for binary decisions
Select Case
Created efficient multi-path decision structures
User Input
Used InputBox to capture user input
Output
Displayed results with MsgBox
These control structures are fundamental for creating interactive console applications that can respond differently based on user input or other conditions.
Next Steps
Continue your learning with VB2019 sample codes and practical projects.
Related Resources
Visual Basic 2019 Made Easy
Master Visual Basic 2019 with this comprehensive guide that includes detailed coverage of console application development. Learn to create efficient command-line tools and utilities.
Key Control Structures:
- If...Then...Else implementations
- Select Case patterns
- Complex decision-making logic
- Error handling techniques
- Practical examples with source code
Control Structures in VB.NET
Deep dive into decision-making structures and flow control in VB.NET, with practical examples and best practices for console application development.
Key Coverage:
- Advanced If...Then...Else patterns
- Select Case optimization
- Nested decision structures
- Real-world application examples