Write a program that executes based on a given condition

Write a program that executes based on a given condition

Write a program that executes based on a given condition

SOLUTION

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.")

Console.WriteLine()

'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

'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()

The next step is to solve our expression. in this program we will collect the answer using 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)

Else

'If the condition is FALSE this block of code will run

Console.WriteLine("FALSE. The correct 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 in true the program runs the truth part of the code and skips the false block of code and exits the program, but if the code results in False then the Truth block of code is skipped to the False block (else) and exits the program.

Subscribe for the full source code.

Latst Updated: 08 May 2022

Previous Post
Next Post