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...Then...Else Structure
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:

Math Quiz Validation
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:

InputBox for first number

Figure 38.1: Enter the first number

InputBox for second number

Figure 38.2: Enter the second number

InputBox for answer

Figure 38.3: Enter your answer

The program will then display whether your answer was correct or incorrect:

Correct answer message

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 Structure
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:

Grade Evaluator
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:

Grade input dialog

Figure 38.5: Entering a grade

The program will then display the corresponding result:

Result message

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

VB2019 Control Structures

Comprehensive guide to decision-making in VB2019

Explore Guide

Console App Samples

Practical VB2019 console application examples

View Examples

VB.NET Docs

Official Microsoft VB.NET documentation

View Documentation

Control Flow

Complete reference for VB2019 control structures

Learn More