1 / 25

Introduction to Programming Lecture 2

Introduction to Programming Lecture 2. Msury Mahunnah , Department of Informatics, T allinn University of Technology. Microsoft Visual Studio and Visual Basic.

Download Presentation

Introduction to Programming Lecture 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IntroductiontoProgrammingLecture 2 Msury Mahunnah, Department of Informatics, Tallinn University of Technology

  2. 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

  3. 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

  4. 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)

  5. 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

  6. 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.

  7. 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)

  8. Numeric variables • Integer (there are several Integer data types) • Decimal • Single (floating-point numbers with limited precision) • Double (floating-point numbers with extreme precision)

  9. VB numeric data types

  10. The characters that define the type of a variable 1. % - Integer 2. & - Long 3. ! – Single 4. # - Double 5. $ - String

  11. 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

  12. 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”

  13. 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()

  14. Control Statements or Flow Control Statements • Decision Statements • If ... Then • If ... Then ... Else • Select Case • Loop Statements • For ... Next • Do ... Loop • While ... End While

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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.

  20. 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

  21. Do...Loop • To execute a block of statements until a condition becomes True, use the following syntax: Do Until condition statementblock Loop

  22. 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

  23. How to repeat forever? Do ‘statements-block Loop While 1=1 ‘statements-block End While While True ‘statements-block End While

  24. Exit from “forever-loop”? Do ... IfconditionThen Exit Do ... Loop While 1=1 ... IfconditionThen Exit While ... End While While True ... IfconditionThen Exit While ... End While

  25. References • “Mastering Visual Basic 2010”, Evangelos Petroustus

More Related