CSM18 Visual Basic Section 2 Department of Computing UniS 1 CSM18 Further Constructs in VB A statement is the fundamental syntactical element of a program.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Chapter 16 Graphical User Interfaces
Copyright © 2003 Pearson Education, Inc. Slide 1.
Lists, Loops, Validation, and More
Making Decisions and Working With Strings
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
State of New Jersey Department of Health and Senior Services Patient Safety Reporting System Module 2 – New Event Entry.
DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
1.
Week 2 The Object-Oriented Approach to Requirements
Chapter 7: Arrays In this chapter, you will learn about
Microsoft Access.
1 1 Mechanical Design and Production Dept, Faculty of Engineering, Zagazig University, Egypt. Mechanical Design and Production Dept, Faculty of Engineering,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Working with Intrinsic Controls and ActiveX Controls
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Lecture 6: Software Design (Part I)
Lecture 7: Software Design (Part II)
CSM18 Visual Basic Section1 Department of Computing UniS 1 CSM18 Interactive Computing Introduce Visual Basic language - provides excellent facilities.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
25 seconds left…...
Chapter 10: The Traditional Approach to Design
Systems Analysis and Design in a Changing World, Fifth Edition
Types of selection structures
Pointers and Arrays Chapter 12
Chapter 11 Describing Process Specifications and Structured Decisions
Chapter 8 Improving the User Interface
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VBA Modules, Functions, Variables, and Constants
Visual Basic: An Object Oriented Approach 3 – Making Objects Work.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Visual Basic: An Object Oriented Approach 5: Structured Programming.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
31/01/ Selection If selection construct.
Higher Computing Software Development -So Far- 5/10/10.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Visual Basic Fundamental Concepts
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
3rd prep. – 2nd Term MOE Book Questions.
3rd prep. – 2nd Term MOE Book Questions.
VISUAL BASIC.
Visual Basic..
Chapter (3) - Looping Questions.
CS285 Introduction - Visual Basic
Chapter 15: GUI Applications & Event-Driven Programming
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Prepared By: Deborah Becker
Introduction to Computer Programming IT-104
Presentation transcript:

CSM18 Visual Basic Section 2 Department of Computing UniS 1 CSM18 Further Constructs in VB 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

CSM18 Visual Basic Section 2 Department of Computing UniS 2 CSM18 Example Statements Dim x As Integer, y As Single x = 2 x = x + 4 y = Sqr(x) Print y Declares variables Assigns a value to the variable Assigns the result of an expression Uses an in-built function Executes a pre- defined operation

CSM18 Visual Basic Section 2 Department of Computing UniS 3 CSM18 Forms Forms are Windows but are also VB Objects which contain other objects - Interaction Objects eg input, output and command controls Input - InputBox, Text Box, Check Box, Lists etc. Command - buttons, menus etc Output - Text Box, Message Box, Picture Box etc 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

CSM18 Visual Basic Section 2 Department of Computing UniS 4 CSM18 Event Driven Program A special form of Subroutine (Sub) that Visual Basic associates with an event Mouse operation Key-press Signal from another application An event handler is called automatically when the event happens Program can respond to external stimuli Private Sub Botton1_Click() Statements…. End Sub

CSM18 Visual Basic Section 2 Department of Computing UniS 5 CSM18 Input & Output Input use an InputBox or TextBox Output use a MsgBox or TextBox or Print Private Sub Botton1_Click() Dim Data As Integer Data = InputBox(“Enter a number”) Text1.Text = “The number is “ & Data Print “The number is “ & Data End Sub

CSM18 Visual Basic Section 2 Department of Computing UniS 6 CSM18 Event-Driven User Interfaces Interaction Objects are created on the Form User interacts with objects to input data, control the program and to receive data/information. Objects have a set of Properties that describe the state of the object. Values of Properties can be changed by program statements

CSM18 Visual Basic Section 2 Department of Computing UniS 7 CSM18 Event-Driven User Interfaces To make the button visible set it’s property ‘visible’ to True Button1.Visible = False

CSM18 Visual Basic Section 2 Department of Computing UniS 8 CMS18 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’

CSM18 Visual Basic Section 2 Department of Computing UniS 9 CSM18 Structured Programming Born out of chaotic nature of software designs before late 1960s 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

CSM18 Visual Basic Section 2 Department of Computing UniS 10 CSM18 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, While...WEnd

CSM18 Visual Basic Section 2 Department of Computing UniS 11 CSM18 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

CSM18 Visual Basic Section 2 Department of Computing UniS 12 CSM18 Logical Conditions These are not assignments: 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

CSM18 Visual Basic Section 2 Department of Computing UniS 13 CSM18 Conditions to control flow… Using a condition, can direct the flow of a program… If Time > “12:00” Then Sub Afternoon() Else Sub Morning() End If

CSM18 Visual Basic Section 2 Department of Computing UniS 14 CSM18 If...Then - flexible control If Time < “12:00” Then MsgBox “Good morning” ElseIf Time > “12:00” And Time < “18:00” Then MsgBox “Good afternoon” Else MsgBox “Good evening” End If

CSM18 Visual Basic Section 2 Department of Computing UniS 15 CSM18 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 MonthNo Case 4, 6, 9, 11 MsgBox “This month has 30 days” Case 2 MsgBox “This month has 28 days” Case Else MsgBox “This month has 31 days” End Select

CSM18 Visual Basic Section 2 Department of Computing UniS 16 CSM18 Iteration 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 eg Print a number of pages Get input from a user until a valid entry Calculate a succession of loan payments

CSM18 Visual Basic Section 2 Department of Computing UniS 17 CSM18 Iteration For index = 1 To 12 ‘ Print a table of squares Print index, index * index Next Do ‘ Repeat until user enters something Name = InputBox(“Enter your name”) Loop Until Name <> “”

CSM18 Visual Basic Section 2 Department of Computing UniS 18 CSM18 Iteration While Time < “18:00” DoWork ()‘Sub DoWork() 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.

CSM18 Visual Basic Section 2 Department of Computing UniS 19 CSM18 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

CSM18 Visual Basic Section 2 Department of Computing UniS 20 CSM18 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

CSM18 Visual Basic Section 2 Department of Computing UniS 21 CSM18 Arrays Students John Smith Jane Green Mary Brown Mike Stone Ashok Din Profits Quarters 1-Dimensional Array 2-Dimensional Array Element Index Dim Students(1 To 6) As String Dim Profits(1998 To 2000, 1 To 4) As Single

CSM18 Visual Basic Section 2 Department of Computing UniS 22 CSM18 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 TotalProfit = TotalProfit + Profits(Year, Quarter) Next

CSM18 Visual Basic Section 2 Department of Computing UniS 23 CSM18 Program in a Module Use a module to provide code accessible form 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

CSM18 Visual Basic Section 2 Department of Computing UniS 24 CSM18 Program in a Module Option Explicit ‘Forces declaration of variables Private Const Max = 10 Private Names (1 To Max) As String Public Sub AddEntry() ….. Public Sub DisplayList() …..