250 likes | 400 Views
Introduction to Programming Lecture 2. Msury Mahunnah , Department of Informatics, T allinn University of Technology. Microsoft Visual Studio and Visual Basic.
E N D
IntroductiontoProgrammingLecture 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 varietyof 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 • DefineWindows application name, OK • Addcontrol(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)
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 AsInteger, b AsLong // 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 AsString // 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 AsDate 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: IfconditionThen statement Multiple-line syntax: IfconditionThen ‘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: IfconditionThen statementblock1 Else statementblock2 Multiple-line syntax: IfconditionThen 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: Ifcondition1Then statementblock1 ElseIfcondition2Then statementblock2 ElseIfcondition3Then statementblock3 Else statementblock4 EndIf
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 Forcounter = startToend [Stepincrement] ‘statements Next [counter] • In executing a For...Next loop, VB does the following: • Sets the counter variable equal to start variable • Tests to see whether counter is greater than end. If so, it exits the loop without executing the statements in then loop’s body. • Executes the statements in the block • Increases the counter variable by amount specified with the increment argument following the Step keyword. • 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 ... IfconditionThen Exit Do ... Loop While 1=1 ... IfconditionThen Exit While ... End While While True ... IfconditionThen Exit While ... End While
References • “Mastering Visual Basic 2010”, Evangelos Petroustus