Upload
adelia-palmer
View
219
Download
0
Tags:
Embed Size (px)
Citation preview
Mr C JohnstonICT Teacher
www.computechedu.co.uk
BTEC IT Unit 06 - Lesson 05
Learning To Program
Session Objectives
Understand the concept of variables in programs and appreciate their use and properties,
Understand different programming constructs which can be used to structure programs,
Use different program constructs to create small programs to illustrate their use.
Variables All programs at some point will need to store data which is either input or
from processing, Although programs often output data to a more permanent form the data
still needs to be stored somewhere within the program prior to this, Data is temporarily stored within in a program in a VARIABLE, Variables all have a name and a data type, and need to be defined within
the program before they can be used.
Dim studentName As String
Declares the variable Names the variable Set the data type of the variable
Variable Names When I first started to program we called variables X and Y as name
length was limited – hard to tell what they are, Good programming technique now is to use sensible names for our
variables to make the code easy to read and maintain, Most language don’t all spaces in variable name so often we use
capital letters (studentName) or underscores (student_Name) instead.
Variable Data Types As well as being declared and having a name a
variable needs a data type, The data type refers to the type of data which will be
stored within the variable. Typical data types used include:Data Type Data Use Range of Values
Boolean Values that can be true or false
True or False
Byte Whole numbers (8 bit) 0 to 255
Integer Whole numbers (16 bit) -32,768 to +32,767
Long Large whole numbers c +/- 2x109
Single Decimal numbers (float) Up to 7 significant figures
Double Large decimal numbers Up to 14 significant figures
Date Date and Time Variety of date and time formats
String Any type of character Sometimes char limit
VbMsgBoxResult
Results of msgbox button presses
vbYes, vbNo, vbOK, vbCancel etc
Programming Constructs
There a several different programming constructs which make up the structure of a program:
Sequence Statements: When the program statements are followed one after another
Selection (or decision): When a choice is made based on criteria – usually IF
statements Iteration (or repetition):
When a section of code is repeated using a loop – there are two main types of loop fixed (repeat until) and conditional (while do)
Visual Basic for Applications1
Overview To demonstrate some of the programming constructs
we are going to use a programming language built into PowerPoint called Visual Basic for Applications (VBA),
To access VBA you can either press Alt+F11 or click on the Visual Basic Icon from the developer ribbon.
To start programming within visual basic you first need to create a new module and then define a subroutine or a function to house the code,
Subroutine and function names can be anything sensible as long as there are no spaces and a reserved word is not used,
Code is then added between the Sub and the End Sub to make the program.
Code added here
Visual Basic for Applications2
Modules, Subs and Functions
To declare a variable in visual basic we use the DIM statement.
Visual Basic for Applications3
Declaring Variables
Dim [variable name] As [variable type]Data Type
Boolean
Byte
Integer
Long
Single
Double
Date
String
VbMsgBoxResult
Assign data to a variable:[variableName] = [value]
Output to screen:MsgBox(“[message]”) OR MsgBox([variableName])
Input value into program:[variableName] = InputBox(“[instruction]”)
Concatenate variables and data:[variableName] = “[message] ” & [variableName] & “ [message]”
Visual Basic for Applications4
Sequence Statements1
Mathematical Operators – As well as concatenating variables we can also manipulate them using mathematical operators.
[variableName] = [variableName] [mathsOperator] [variable OR value]
Visual Basic for Applications5
Sequence Statements2
Operator
Data Use Example Order of Precedence
^ Raise to power of 3 ^ 3 = 27 1
* Multiply 6 * 4 = 24 2
/ Division 50 / 5 = 10 3
Mod Remainder of division
10 MOD 3 = 1 4
+ Add 123 + 33 = 156 5
- Subtract 83 – 47 = 36 6
IF statements are used to check if a condition has been metIf [condition] Then
[instructions to be run if true]Else
[instructions to be run if false]End If
Visual Basic for Applications6
Selection Statements1
IF statements can be nested to test if different conditions have been metIf [condition] Then
[instructions to be run if true]Else IF
[instructions to be run if true]Else IF
[instructions to be run if both false]End If
Visual Basic for Applications7
Selection Statements2
Nested IF statements although useful are quite inefficient, and its better to use a SELECT CASE statementSelect Case [variable to be used]
Case Is [condition][instructions if true]
Case Is [condition][instructions if true]
Case Is [condition][instructions if true]
etcEnd Select
Visual Basic for Applications8
Selection Statements3
When creating IF and CASE statements we use operators to construct conditions,
Relational operators allow use to construct conditions which compare variables with other values,
Logical operators can be used to combine different conditions.
Visual Basic for Applications9
Logical and Relational Operators
Operator Meaning
> Greater than
< Less than
/ Equal to
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
Operator
Meaning Example
AND Returns true if all conditions are true
Age > 18 AND Gender=‘F’
OR Returns true if one of the conditions is true
Age > 18 OR Gender=‘F’
NOT Returns the opposite of the condition
NOT Age > 18
Iteration means repeat or loop, There are two different types of loop we use within
programming: Fixed loops – repeat for a fixed number of times Variable loops – repeat until criteria are met
Visual Basic for Applications9
Iteration1
FOR loops repeat a section of code a stated number of timesFOR [counter variable] = [start value] to [end value]
[instructions to be run inside loop]Next
Visual Basic for Applications9
FOR Loops
DO loops keeping repeating the instructions until a criteria has been met.
Criteria could be WHILE, or UNTILDO WHILE [variable] [criteria]
[instructions to be run inside loop]Loop
DO UNTIL [variable] [criteria][instructions to be run inside loop]
Loop
Visual Basic for Applications9
DO Loops