Working With IntegersThe process of creating variable called "Declaring"
To Declare (=create) Integer variable simply write:
Dim MyTest As Integer
What does the line above do?
It creates an Integer variable with the name MyTest.
Dim = Declare
MyTest = the name of the new variable
As Integer = The new variable type will be Integer.
Now you can put a number inside this variable.
You can do that by simple write:
MyTest = 10
The line above will insert the number 10 into the MyTest variable.
Now the MyTest variable stores the Number 10, but how
can you access this value from your program?
You can do that using the variable name. Example:
Print MyTest
The line above will write 10 on the form.
Pay attention to the differents between
Print MyTest
and
Print "MyTest"
Where you putting a text inside quotes (Like in the bottom line),
Visual Basic treat it as a Text, and will print it As-Is.
The Print "MyTest" Line will print MyTest on the form.
Where you putting a text Without quotes (Like in the upper line),
Visual Basic treat it as a variable name, and will print the value that found
in the variable.
The Print MyTest Line will print the value that found in
the MyTest variable, therefore it will print 10 on the form.
Question: What will be printed on the form
after executing the following code:
Dim Blah As Integer
Print Blah
Blah = 10
Blah = 20
Print Blah
Blah = 30
Print "Blah"
Print Blah
After you've thinking about the answer, you can
check it by inserting the code above into a Command Button's
Click event, and press the button at run-time.
Answer:
0
20
Blah
30
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
Print Blah
Will print the Value that found in the Blah variable.
But there is nothing in the Blah variable!
The Blah variable has just been declared, and we
didn't put inside it any value yet.
The default value of any integer variable is 0.
When you write :
Dim Blah As Integer
It's like you've written:
Dim Blah As Integer
Blah = 0
So it will print the value that found right now
in the Blah variable - 0
Blah = 10
Now the Blah variable holds the number 10
Blah = 20
Now the Blah variable holds the number 20
What's happened to the 10 that was inside it?
It has been deleted!
A variable can holds only one value, and when
you put in it a value, the old value is being immediately deleted.
So what is the differents between
Blah = 20
and
Blah = 10
Blah = 20
?
There is no differents!
In both cases the Blah variable will hold the number 20
Print Blah
Will print the value that found right now
in the Blah variable - 20
Blah = 30
Now the Blah variable holds the number 30
Print "Blah"
Will print the Text that found between the quotes - Blah
Print Blah
Will print the value that found right now
in the Blah variable - 30
Question: What will be printed on the form
after executing the following code:Dim Blah As Integer
Blah = 2
Print 2 + 3
Print "2 + 3"
Print Blah + 3
Print Blah
Blah = Blah + 1
Print Blah
Blah = Blah + Blah
Print Blah
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 5So 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
Monday, 8 August 2011
Subscribe to:
Post Comments (Atom)
Subscribe to email feed



