Introduction to Programming Lecture 2

Introduction to Programming
Lecture 2
Msury Mahunnah,
Department of Informatics,
Tallinn University of Technology
Microsoft Visual Studio and
Visual Basic
 The Visual Studio is an integrated development
environment (IDE) for building, testing, debugging,
and deploying a variety of applications:





Windows applications,
web applications,
classes and
custom controls, and
even console applications.
 Visual Studio supports different programming
languages: Microsoft Visual Basic, Visual J#, Visual
C#, and Visual C++.
Book of “Mastering Microsoft Visual Basic 2010”
http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_Professional
Getting Started
 Launch Microsoft Visual Basic
 From the File menu, select New Project
 The type of project, select Windows Forms
Application
 Define Windows application name, OK
 Add control(s) from “Common Controls”
 To execute select from menu:

“Debug-> Start Debugging” or press F5
Toolbox
Includes different Windows form items
 Mainly we are using components (objects) from



Common Controls (Button, CheckBox, ...)
Containers (GroupBox, Panel)
Components (Timer)
 Every Windows form components (objects) has
its own:



Properties (= attributes, describing a object)
Methods (= acts)
Events (implemented by procedures)
Using object properties and methods
object_name.property
object_name.method
The examples
Button1.Hide()
Button1 – object name
Hide() – a method
Button1.Width = 50
Width – property
The variable
 A variable is a named location in computer
memory that maintains values.
 A variable is a memory field or a cell to keep
the values, provided with a name.
The types of variables
VB recognizes the following five categories of
variable:
 Numeric (stores numbers)
 String (stores text)
 Boolean (strores values “TRUE” and
“FALSE”)
 Date (stores timevalues and datevalues)
 Object (can stores any type of data)
Numeric variables
 Integer (there are several Integer data types)
 Decimal
 Single (floating-point numbers with limited
precision)
 Double (floating-point numbers with extreme
precision)
VB numeric data types
Data type
Memory
representation
Stores in the range ...
Byte (Byte)
1 byte
0 to 255 (int)
Signed Byte (SByte)
1 byte
-128 to 127 (int)
Short (Int16)
2 bytes
-32,768 to 32,767 (int)
Integer (Int32)
4 bytes
-2,147,483,648 to 2,147,483,647 (int)
Long (Int64)
8 bytes
-9x10^18 to 9x10^18 (int)
Unsigned Short (UShort)
2 bytes
0 to 65,536
Unsigned Integer (UInteger)
4 bytes
0 to 4,294,967,295
Unsigned Long(ULong)
8 bytes
0 to 18,446,744,073,709,551, 615
Single Precision (Single)
4 bytes
-3.40E38 to -1.40E-45 (neg numbers)
1.40E-45 to 3.40E38 (pos numbers)
Double Precision (Double)
8 bytes
-1.79E308 to -4,94E-324 (neg numbers)
4.94E-324 to 1.79E308 (pos numbers)
Decimal (Decimal)
16 bytes
Integer and floating-point numbers scaled by
a factor in the range from 0 to 28.
The characters that define the type of a
variable
1. % - Integer
2. & - Long
3. ! – Single
4. # - Double
5. $ - String
Examples of the declare statements
 The declare statement (DS) defines a variable, the
data type of the variable and optionally, its initial
value.
 DS begins with a keyword Dim.
Dim a As Integer, b As Long // Dim a%, b&
a = 247
b = 52
Dim c As Integer = 32768 // Dim c%= 32768
Dim d As Single = 2.78 // Dim d! = 2.78
Dim e As Double= 3.14 // Dim e# = 3.14
String data type

Type “String” stores 2 billion characters (about
2GB)

Examples of the declaration:
Dim word As String // or Dim word$
word = “disambiguation”
Dim word As String = “disambiguation”
or
Dim word$= “disambiguation”
Date data type
 Store date values that may include a time (or
not), and they are declared with the Date
data type:
Dim expiration As Date
expiration = #01/01/2010#
expiration = #8/27/1999 6:27:11#
expiration = #July 2, 2011#
expiration = Today()
Control Statements or Flow Control
Statements
 Decision Statements



If ... Then
If ... Then ... Else
Select Case
 Loop Statements



For ... Next
Do ... Loop
While ... End While
If ... Then Statement
 The If...Then statement tests an expression, which is known as
a condition. If the condition is True, the program executes the
statement(s) that follow the Then keyword up to the End If
statement, which terminates the conditional statement.
 The If...Then statement can have a single-line or multiple-line
syntax.
Single-line syntax:
If condition Then statement
Multiple-line syntax:
If condition Then
‘Statement(s)
End if
If ... Then ... Else Statement
 A variation of the If ... Then statement is the If ... Then ... Else
statement, which executes one block of statements if the
condition is True and another block of statements if the
condition is False
 The If...Then statement can have (again) a single-line or
multiple-line syntax.
Single-line syntax:
If condition Then statementblock1 Else statementblock2
Multiple-line syntax:
If condition Then
statementblock1
Else
statementblock2
End if
If ... Then ... Else statement . . .
 A third variation of the If ... Then ... Else statement
uses several conditions, with the ElseIf keywords:
If condition1 Then
statementblock1
ElseIf condition2 Then
statementblock2
ElseIf condition3 Then
statementblock3
Else
statementblock4
End If
Select Case Statements
 The Select Case structure evaluates a single expression at the
top of the structure. The result of expression is then compared
with several values; if it matches one of them, the corresponding
block of statements is executed.
Select Case expression (selector)
Case value1
statementblock1
Case value2
statementblock2
.
.
.
Case Else
statementblockN
End Select
For ... Next loops
For counter = start To end [Step increment]
‘statements
Next [counter]
 In executing a For...Next loop, VB does the following:
1. Sets the counter variable equal to start variable
2. Tests to see whether counter is greater than end. If so,
it exits the loop without executing the statements in
then loop’s body.
3. Executes the statements in the block
4. Increases the counter variable by amount specified with
the increment argument following the Step keyword.
5. Continues with step 2.
Do...Loop
 The Do...Loop statement executes a block of
statements for as long as a condition is True
or until a condition becomes True.
 To execute a block of statements until a
condition is False, use the following syntax:
Do
statementblock
Loop While condition
Do...Loop
 To execute a block of statements until a
condition becomes True, use the following
syntax:
Do Until condition
statementblock
Loop
While Loops
 The While...End While loop executes a block
of statements as long as condition is true.
 The loop has the following syntax:
While condition
statementblock
End While
How to repeat forever?
Do
‘statements-block
Loop
While 1=1
‘statements-block
End While
While True
‘statements-block
End While
Exit from “forever-loop”?
Do
...
If condition Then Exit Do
...
Loop
While 1=1
...
If condition Then Exit While
...
End While
While True
...
If condition Then Exit While
...
End While
References
 “Mastering Visual Basic 2010”, Evangelos
Petroustus