Guide to Game Development/The Programming Language/VB.NET/If Statement
If statement
editAn 'if' statement can be used to check the value of a variable and compare it to something else. Here's a list of some comparison/conditional operators:
Conditional Operator | Description |
---|---|
= | Is equal to |
<> | Is not equal to |
> | Is greater than |
< | Is less than |
>= | Is greater than or equal to |
<= | Is less than or equal to |
Example of an if-statement in use
editDim Num1 as Integer = Console.ReadLine()
If Num1 = 5 Then
Console.WriteLine("They chose the number 5!")
End If
'Stopping the program from closing
Console.ReadLine()
First run of the program
editInput:
5
Output:
They have chose the number 5!
Second run of the program
editInput:
4
Output:
Example of a greater than being used
editDim Num1 as Integer = Console.ReadLine()
If Num1 > 5 Then
Console.WriteLine("Their number was greater than 5!")
End If
'Stopping the console from closing
Console.ReadLine()
First run of the program
editInput:
8
Output:
Their number was greater than 5!
Second run of the program
editInput:
3
Output:
Third run of the program
editInput:
5
Output:
Notice that if it was equal to 5, there was no output, to get an output for 5 as well the code could be either changed to > 4 or >= 5
Else
editThis is to used to active when the if statement isn't true.
Example of Else Statement
editExample code:
Dim Num1 as Integer = Console.ReadLine()
If Num1 >= 7 Then
Console.WriteLine("Their number is greater than or equal to 7.")
Else
Console.WriteLine("Their number is less than 7.")
End If
First run of the program
editInput:
8
Output:
Their number is greater than or equal to 7.
Second run of the program
editInput:
7
Output:
Their number is greater than or equal to 7.
Third run of the program
editInput:
6
Output:
Their number is less than 7.
Else If
editThese can be used to check for other things other than the first if statement.
Example of Else If statement
editDim Num1 as Integer = Console.ReadLine()
If Num1 = 8 Then
Console.Writeline("Equal to 8.")
Else If Num1 >= 7 Then
Console.WriteLine("7 and more.")
Else
Console.WriteLine("Everything else (6 or less).")
End If
'Stopping the console from closing
Console.ReadLine()
First run of the program
editInput:
9
Output:
7 and more.
Second run of the program
editInput:
7
Output:
7 and more.
Third run of the program
editInput:
6
Output:
Everything else (6 or less).
Forth run of the program
editInput:
8
Output:
Equal to 8.
Notice that the first is accepted, but the second statement is still true, but it didn't run that piece of code, this is because with if statement it goes from top to bottom, when it finds one statement that is true, it doesn't bother checking others.
A way to make both active when they're both true would be to do this:
If Num1 = 8 Then
Console.Writeline("Equal to 8.")
End If
If Num1 >= 7 Then
Console.WriteLine("7 and more.")
Else
Console.WriteLine("Everything else (6 or less).")
End If
Example Input and Output for this new algorithm
editInput:
8
Output:
Equal to 8. 7 and more.
You can have many Else If statements in a row
editExample:
Dim Num1 as Integer = Console.ReadLine()
If Num1 = 5 Then
Console.WriteLine("Num1 is 5")
Else If Num1 = 6 Then
Console.WriteLine("Num1 is 6")
Else If Num1 = 7 Then
Console.WriteLine("Num1 is 7")
Else If Num1 = 8 Then
Console.WriteLine("Num1 is 8")
Else
Console.WriteLine("Num1 isn't 5, 6, 7 or 8")
End IF
'Stopping the console from closing
Console.ReadLine()
Of course this is an inefficient way of coding, a better way would be to write:
Console.WriteLine("Num1 is " & Num1)
However the code above doesn't allow for the else part, for this we'll need to learn about boolean logic operators as seen below.
Boolean Logic Operators
editThere are 4 main boolean logic operators available: AND, OR and XOR, NOT. Boolean logical operators compare the two boolean statements either side of it and then convert them both into another boolean statement (With the exception of NOT which only affects the boolean statement after it). They are defined as such:
Operator | Description | Truth table | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
And | Is only true if both the statements are true, else it's false. |
| |||||||||||||||
Or | Is true if either one of the statements are true, or if they are both true. Is only false when both are false. |
| |||||||||||||||
Xor | Is true when either one are true. Is false if they are both true, or if they are both false (Only true if they are different). |
| |||||||||||||||
Not | Inverts the boolean value of the statement that follows it. |
|