Write Your First Visual Basic Program

Lesson 2
Tutorials - Page 1 - Page 2 - Page 3 - Page 4 - Page 5 - Page 6 - Page 7 - Page 8 - Page 9 - Page 10 - Page 11 - Page 12 - Page 13

Working With Integers (Continue)
Answer:

5
2 + 3
5
2
3
6


Why is that?
Lets pass over the code line after line:

Dim Blah As Integer
A new Integer with the name Blah has been declared

Blah = 2
Now Blah holds the value 2

Print 2 + 3
When you execute command (the Print command in the case)
on expression, Visual Basic will evaluate the expression first,
and then will execute the command on the evaluation result.

In this case we execute the command Print on the expression 2 + 3.
The expression will be evaluated: 2 + 3 = 5.
The evaluation result is 5, and then Visual Basic will
execute the command  Print 5
So in other words, Print 2 + 3 is equivalent to Print 5
after executing the Command Print 5 , 5 is been printed on the form.

Print "2 + 3"
As I said before, Everything that found inside quotes is being
treated as a string. So the string 2 + 3 will be printed on the form.
       Print 2 + 3 will print the value of the expression 2 + 3,
       Print "2 + 3" will print the text string 2 + 3

Print Blah + 3
Now the expression is Blah + 3.
when a variable is found inside expression, it's being
replaced with its value.
In this case the value of the variable Blah is 2.
So the Blah in the expression is being replaced with 2.
After the replacement the new command is Print 2 + 3
As we saw earlier, after executing this command
the value 5 will be printed on the form.

Print Blah
Will replace the Blah with its value.
Because the Blah value is 2, After the replacement
the new command will be Print 2
After executing this command, the value 2
will be printed on the form.

Blah = Blah + 1
The line above simply says:
Put in the Blah variable, the value of the expression Blah + 1
The computer is first evaluate the expression Blah + 1
Blah is being replaced with its value: 2.
After the replacement the computer evaluates the expression 2 + 1.
The expression value is 3.
So now, after the "Blah + 1" expression evaluation,
the command is: Blah = 3
As you know by now, this command will
put the value 3 in the Blah variable.
Summary:
After executing the command Blah = Blah + 1,
the value 3 will be inserted into the Blah variable.

Print Blah
Will replace the Blah variable with its value: 3.
So the command that will be eventually executed is  Print 3

Blah = Blah + Blah
Will Put in the Blah variable, the value of the expression Blah + Blah
The computer is first evaluate the expression Blah + Blah
Blah is being replaced with its value: 3.
After the replacement the computer evaluates the expression 3 + 3.
The expression value is 6.
So now, after the "Blah + Blah" expression evaluation,
the command is: Blah = 6
After executing the command Blah = 6,
the value 6 is being inserted into the Blah variable.

Print Blah
Will replace the Blah variable with its value: 6.
So the command that will be eventually executed is  Print 6

Back  Forward