Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mr C Johnston ICT Teacher www.computechedu.co.uk BTEC IT Unit 06 - Lesson 05 Learning To Program.

Similar presentations


Presentation on theme: "Mr C Johnston ICT Teacher www.computechedu.co.uk BTEC IT Unit 06 - Lesson 05 Learning To Program."— Presentation transcript:

1 Mr C Johnston ICT Teacher www.computechedu.co.uk BTEC IT Unit 06 - Lesson 05 Learning To Program

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

3 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

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

5 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 TypeData UseRange of Values BooleanValues that can be true or falseTrue or False ByteWhole numbers(8 bit) 0 to 255 IntegerWhole numbers(16 bit) -32,768 to +32,767 LongLarge whole numbers c + / - 2x10 9 SingleDecimal numbers (float)Up to 7 significant figures DoubleLarge decimal numbersUp to 14 significant figures DateDate and TimeVariety of date and time formats StringAny type of characterSometimes char limit VbMsgBoxResultResults of msgbox button pressesvbYes, vbNo, vbOK, vbCancel etc

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

7 Visual Basic for Applications 1 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.

8 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 Applications 2 Modules, Subs and Functions

9 To declare a variable in visual basic we use the DIM statement. Visual Basic for Applications 3 Declaring Variables Dim [variable name] As [variable type] Data Type Boolean Byte Integer Long Single Double Date String VbMsgBoxResult

10 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 Applications 4 Sequence Statements 1

11 Mathematical Operators – As well as concatenating variables we can also manipulate them using mathematical operators. [variableName] = [variableName] [mathsOperator] [variable OR value] Visual Basic for Applications 5 Sequence Statements 2 OperatorData UseExampleOrder of Precedence ^ Raise to power of3 ^ 3 = 271 * Multiply6 * 4 = 242 / Division50 / 5 = 103 Mod Remainder of division10 MOD 3 = 14 + Add123 + 33 = 1565 - Subtract83 – 47 = 366

12

13 IF statements are used to check if a condition has been met If [condition] Then [instructions to be run if true] Else [instructions to be run if false] End If Visual Basic for Applications 6 Selection Statements 1

14 IF statements can be nested to test if different conditions have been met If [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 Applications 7 Selection Statements 2

15 Nested IF statements although useful are quite inefficient, and its better to use a SELECT CASE statement Select Case [variable to be used] Case Is [condition] [instructions if true] Case Is [condition] [instructions if true] Case Is [condition] [instructions if true] etc End Select Visual Basic for Applications 8 Selection Statements 3

16 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 Applications 9 Logical and Relational Operators OperatorMeaning > Greater than < Less than / Equal to >= Greater than or equal to <= Less than or equal to <> Not equal to OperatorMeaningExample 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

17

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 Applications 9 Iteration 1

19 FOR loops repeat a section of code a stated number of times FOR [counter variable] = [start value] to [end value] [instructions to be run inside loop] Next Visual Basic for Applications 9 FOR Loops

20 DO loops keeping repeating the instructions until a criteria has been met. Criteria could be WHILE, or UNTIL DO WHILE [variable] [criteria] [instructions to be run inside loop] Loop DO UNTIL [variable] [criteria] [instructions to be run inside loop] Loop Visual Basic for Applications 9 DO Loops

21


Download ppt "Mr C Johnston ICT Teacher www.computechedu.co.uk BTEC IT Unit 06 - Lesson 05 Learning To Program."

Similar presentations


Ads by Google