~ Programming in VB.Net


Selection

An important part of programming is the use of selection, that is the ability to do something if certain criteria is met. This may be as simple as increasing your health bar in a computer game if you eat a chicken drumstick or inserting the cooling rods into the nuclear reactor if the temperature exceeds a certain value.

IF Statement

If-Then-Else-diagram.svg

The most common selection statement is the IF statement, the idea is that you compare a value to some criteria, IF the value and criteria match then you proceed in a certain way, otherwise you do something else. For example:

If It is the queen Then
 Salute her
Else
 Treat them like a commoner
End

 

VB.NETPython
1  If name = "Queen" Then
2 	console.writeline("Hello your Majesty")
3  Else
4 	console.writeline("Get to the back of the queue!")
5  End If
1  if name == "Queen":
2     print ("Hello your Majesty")
3  else:
4     print ("Get to the back of the queue!")

The Else part is optional, you can just ignore the commoner! (and dump the Else)

VB.NETPython
1  If name = "Queen" Then
2 	console.writeline("Hello your Majesty")
3  End If
1  if name == "Queen":
2     print ("Hello your Majesty")

You might also want to test multiple things in the If statement. For example:

VB.NET
1  If name = "Queen" And age >= 18 Then
2 	console.writeline("Hello your Majesty, I can serve you beer")
3  Else
4 	console.writeline("Get out of my bar!")
5  End If
Python
1  if name == "Queen" and age >= 18:
2     print ("Hello your Majesty, I can serve you beer")
3  else:
4     print ("Get out of my bar!")

Relational operators

We often want to write IF statements that do some kind of comparison or test. We just did exactly that in the example above with age >= 18 which tests if the value of the age variable is greater or equal to 18.

Most of these operators you will recognise from learning maths but some are slightly different in computing. The following operators are straightforward:

OperatorMeaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

The most important operator that is different in computing is one that you have already used many, many times probably without even noticing, which is the = operator. In most programming languages the = operator is used for assignment, for example if we want to assign the value "bunny" to a variable called animal we write animal = "bunny" which is the same in both VB.NET and Python. These two languages are different when it comes to equals which we saw in the example above testing if the value of the name variable was equal to "Queen". In VB.NET the equals operator is just = whereas Python uses == instead. This can lead to a very common programming mistake when writing Python - if we try to write an IF statement which uses = by mistake:

 if name = "Queen" and age >= 18:
    print("Hello your Majesty, I can serve you beer")

we will get an error message similar to this:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Traceback (most recent call last):
  File "python", line 4
      if name = "Queen" and age >= 18 then:
                   ^
SyntaxError: invalid syntax


Finally we need an operator for not equals. In VB.NET we use <> whereas in Python we use !=. Here's an example in the form of a really quite rubbish game:

VB.NETPython
1 	Dim secret_word, your_guess As String
2 	secret_word = "unlikely"
3 	console.WriteLine("Try to guess the secret word:")
4 	your_guess = console.ReadLine()
5 	If your_guess <> secret_word Then
6 		console.WriteLine("You lose")
7 	End If
1 secret_word = "unlikely"
2 print("Try to guess the secret word:")
3 your_guess = input()
4 if your_guess != secret_word:
5 	print("You lose")
Exercise: IF statements