Write a program that executes based on a given condition

Web Design and Digital Marketing

Write a program that executes based on a given condition.

NOTE: To solve this problem we will be using VB.NET as our programming language.


Before you start writing code for this question, I identify all that is needed to come up with a solution. 
In this question we need,

1. Variables

        'Program title
        Console.WriteLine("Conditional Program Flow.")
        'Our variables
        Dim firstNumber As Integer = 10
        Dim secondNumber As Integer = 5

Now that we have our 2 variables, we need an expression

2 . Expression

        Dim answer = firstNumber + secondNumber
        Console.WriteLine("What is {0} + {1}?", firstNumber, secondNumber)

        'User's answer. If it is correct the TRUE block will run otherwise The FALSE block will run
        Dim myAnswer = Console.ReadLine()

Next step is to is to solve our expression. in this program we will collect the answer using an input from the user.(already in the code snippet above)

3 . Condition

Now that we have an expression and our answer hold in memory, let us set our condition to check if we get our answer correct.

 If answer = myAnswer Then
            'If the condition is TRUE this block of code will run
            Console.WriteLine("TRUE. Your answer is correct. ({0}).", answer)
            'If the condition is FALSE this block of code will run
            Console.WriteLine("FALSE. The corect answer is {0}.", answer)
  End If

On the above block of code, we are comparing the correct answer with the user-provided answer to check if it is correct (True) or if it is wrong (False)

4. Final answer

If the above line of code results to true the program runs the truth part of code and skips the false block of code and exits the program, but if the code results to False then the Truth block of code is skipped to the False block (else) and exits the program.

Subscribe for full source code.

Leave a Comment

Your email address will not be published.