Discovering Visual Basic .NET: Making Decisions, Page 2
If...Then statements can use what math professors call inequalities. Inequalities are ways of comparing numbers to see if they are greater than or less than each other. And, to keep it simple, VB.NET uses the same symbols as you did in your sixth-grade math class:
- This symbol means greater than: >
- And this symbol means less than: <
How do you keep them straight? My sixth-grade teacher explained it to me this way: Think of the symbol as an alligator's big mouth opening. And just remember, the alligator always eats the bigger number. Silly? Yes. But I've never forgotten it!
Imports SystemImports Microsoft.VisualBasicModule GetAnA Public Sub Main() Dim Grade As Integer Grade = 95 If Grade > 90 Then Console.WriteLine("You get an A!") End If End SubEnd Module
The happy result:
You get an A!
Because the variable Grade holds a value greater than 90, the message You get an A! appears. Suppose you don't get greater than 90, but you get 90 right on the nose. Shouldn't that be an A, too?
Try changing the preceding code so that Grade = 90. Save, compile and run. Nothing. It doesn't work. How do you fix that? Well, here's one way: Change the If...Then line to look like this:
If Grade > 89 Then
This makes the program work, but sticking with round numbers really makes more sense. A better way would be to use another symbol, >=, which means greater than or equal to.
Change the If...Then line so that it looks like this:
If Grade >= 90 Then
You get an A!
As you may expect, you also can use a <= symbol, which means less than or equal to. These symbols are a little different from the ones you learned in sixth-grade math, but these are easier to remember anyway.
You can use one more symbol to compare variables: <>, which means does not equal. Here's an example:
If Grade <> 100 Then Console.WriteLine("You did not get a perfect score.")End If
Page 2 of 4