CS 285 Programming in VB Statements in Visual

CS 285 Programming in VB Statements in Visual Basic • A statement is the fundamental syntactical element of a program • smallest piece of code that the language will accept in its own right • A statement can be used to • • • set aside space for data storage (variables) assign a value to a variable perform a calculation and assign its result to a variable execute a previously-defined operation control other statements CS 285 Visual Basic 2 1 Department of Computing Uni. S

Visual Basic - Example Statements Dim x As Integer, y As Single Declares variables Assigns a value to the variable x=2 x=x+4 Assigns the result of an expression y = Sqr(x) Print y Uses an in-built function Executes a predefined operation Prints values on the current form from the top down CS 285 Visual Basic 2 2 Department of Computing Uni. S

Visual Basic - Input & Output Forms are Windows but are also VB Objects which contain other objects - Interaction Objects e. g. input, output and command controls • Input - Input. Box, Text Box, Check Box, Lists etc. • Output - Text Box, Message Box, Print, Picture Box • Command - Buttons, Menus etc Private Sub Button 1_Click() Dim Data As Integer Data = Input. Box(“Enter a number”) Text 1. Text = “The number is “ & Data Msg. Box (“The number is “ & Data) Print “The number is “ & Data End Sub CS 285 Visual Basic 2 3 Department of Computing Uni. S

Visual Basic - Design Issues • A Form should allow user-input in any order • Processing triggered when a command is issued • Users should be kept informed of appropriate actions required and results of actions • Make sure inappropriate actions are disabled Private Sub Command 1_Click() Command 2. Enabled = False Command 3. Visible = True End Sub _Click() CS 285 Visual Basic 2 4 Department of Computing Uni. S

VB - Structures within Software • Structure is apparent at a number of levels • A project can involve a number of modules, each occupying a separate file • Each module will contain a number of procedures (Subs and Functions) and variable declarations • Within a Sub or Function, certain standard code constructs are used to define whether actions happen or not, or how often they are executed • Data variables in a module can be grouped in several ways to form data structures • The last two categories are central to the idea of ‘Structured Programming’ CS 285 Visual Basic 2 5 Department of Computing Uni. S

VB - Structured Programming • Born out of chaotic nature of software designs before late 1960 s • Idea is to limit the power of the programmer to direct a program with no restrictions • Code statements are organised in a small number of standard forms • Structure of data should mirror real-life information structure wherever possible CS 285 Visual Basic 2 6 Department of Computing Uni. S

VB - Program Control Constructs Three main structuring principles • Code statements should be grouped into functional units • Subs, Functions • Possible to select whether one or more statements will execute or not, based on a simple logical criterion • If. . . Then, If. . . Then. . . Else, Select Case • Possible to repeat a statement or group of statements either a given number of times or until a specific condition is true For. . . Next, Do. . . Loop, CS 285 Visual Basic 2 7 While. . . WEnd Department of Computing Uni. S

Visual Basic - Conditions • Central to the principles of programming • The flow of execution depends on prevailing conditions within a program • A condition is an expression that evaluates to True or False. Can be… • • A single Boolean variable A combination of Boolean variables The result of a comparison for equality or relative size A combination of such comparisons CS 285 Visual Basic 2 8 Department of Computing Uni. S

Visual Basic - Logical Conditions X=0 ‘ True if X is zero, False otherwise Y=X ‘ True if Y equals X X<Y ‘ True is X is bigger than Y X = 0 And Y >2 ‘ True if both are true X = 0 Or Y > 2 ‘ True if either is true These are not assignments: CS 285 Visual Basic 2 9 Department of Computing Uni. S

VB - Conditions to control flow • Using a condition, can direct the flow of a program… If Time > “ 12: 00” Then Call Afternoon() Else Call Morning() End If CS 285 Visual Basic 2 10 Department of Computing Uni. S

VB - If. . . Then flexible control If Time < “ 12: 00” Then Msg. Box “Good morning” Else. If Time > “ 12: 00” And Time < “ 18: 00” Then Msg. Box “Good afternoon” Else Msg. Box “Good evening” End If CS 285 Visual Basic 2 11 Department of Computing Uni. S

Visual Basic - Case Structure • When dealing with many possible conditions use a Case Construct to Select one of a number of possible Cases • Select Case Variable Select Case Month. No Case 4, 6, 9, 11 Msg. Box “This month has 30 days” Case 2 Msg. Box “This month has 28 days” Case Else Msg. Box “This month has 31 days” End Select CS 285 Visual Basic 2 12 Department of Computing Uni. S

Visual Basic - Iteration is repetition of code • Can execute one or more statements • A given number of times • Until a condition becomes True • While a condition remains True • This allows the same code to be reused • For a number of similar variables • For the same variable(s) containing a succession of values e. g. • Print a number of pages • Get input from a user until a valid entry • Calculate a succession of loan payments CS 285 Visual Basic 2 13 Department of Computing Uni. S

Visual Basic - Iteration For index = 1 To 12 ‘ Print a table of squares Print index, index * index Next Do ‘Repeat until user enters something Name = Input. Box (“Enter your name”) Loop Until Name <> “” CS 285 Visual Basic 2 14 Department of Computing Uni. S

Visual Basic - Iteration While Time < “ 18: 00” Do. Work () ‘Sub Do. Work() defined elsewhere Wend Note: This code will not be executed unless the initial condition is met. Compare with Do…Loop which executes the code at least once. CS 285 Visual Basic 2 15 Department of Computing Uni. S

Visual Basic - Structured Data • May need to work with sets of data • Classes of students • Lists of library books • Normally, information is complex and has structure • Items are related by. . • Similarity (e. g. class of students) • Hierarchy (e. g. a book has several chapters) • Grouping (e. g. a person has a name, address, phone number, national insurance number, credit cards, etc. . ) • We use data structures to group information together CS 285 Visual Basic 2 16 Department of Computing Uni. S

Visual Basic - Arrays • Simplest form of data structure • A number of variables of the same type, related in some way • A list of names • A Table of profit figures • All elements share the same name • Each element has an index indicating its position in the array CS 285 Visual Basic 2 17 Department of Computing Uni. S

Visual Basic Arrays Dim Profits(1998 To 2000, 1 To 4) As Single Dim Students(1 To 6) As String Index Students 1 2 3 4 5 6 John Smith Jane Green Mary Brown Mike Stone Ashok Din Profits Quarters 1998 2000 1 1240. 00 1450. 25 1603. 33 2 1775. 50 1825. 23 1733. 24 3 4 1602. 45 1743. 10 1679. 95 1100. 70 1250. 50 1432. 55 1 -Dimensional Array Index CS 285 Visual Basic 2 1999 18 2 -Dimensional Array Element Department of Computing Uni. S

VB - Programming with Arrays Use For. . Next construct to loop through an array Dim ID As Integer, Year As Integer, Quarter As Integer …… For ID = 1 To 6 Print Students(ID) Next For Year = 1998 To 2000 ‘Note nested For loops For Quarter = 1 To 4 Total. Profit = Total. Profit + Profits(Year, Quarter) Next CS 285 Visual Basic 2 19 Department of Computing Uni. S

VB - Programming with Arrays Use of a Control Array An array of Control Objects: Create the first Control Object and name it Create the second Control Object and give it the same name. VB asks if this is an array. Reference by Name(0), Name(1), Name(2) etc. CS 285 Visual Basic 2 20 Department of Computing Uni. S

VB - Use of Control Arrays For i = 0 To 3 Text. Box(i). Text = "Text Box " & i Next CS 285 Visual Basic 2 21 Department of Computing Uni. S

Visual Basic - Use of List. Box Sub Form_Load() List 1. Add. Item "Red" List 1. Add. Item "Blue" List 1. Add. Item "Green" End Sub CS 285 Visual Basic 2 Sub List 1_Click() Select Case List 1. List. Index Case 0 Text. Box(0). Back. Color = &HFF Case 1 Text. Box(1). Back. Color = &HFF 00 Case 2 Text. Box(2). Back. Color = &HFF 0000 End Select End Sub 22 Department of Computing Uni. S

Visual Basic - Program in a Module • Use a module to provide code accessible from any where in the project • Add Module, make • Option Explicit - forces declaration of variables - good practice • Data Storage with Private Procedures • Access with Public Procedures CS 285 Visual Basic 2 23 Department of Computing Uni. S

Visual Basic - Program in a Module Option Explicit ‘Forces declaration of variables Private Const Max = 10 Private Names (1 To Max) As String Public Sub Add. Entry() …. . Public Sub Display. List() …. . CS 285 Visual Basic 2 24 Department of Computing Uni. S
- Slides: 24