Download presentation
Presentation is loading. Please wait.
1
CP1020 - Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation
2
An Example Program REM program : To calculate the area of a square REM written by : I Coulson REM date written: 9/9/98 DIM iSide AS INTEGER DIM iArea AS INTEGER CLS ‘ clear the screen iSide = 2 ‘ set variable iSide to contain value 2 iArea = iSide * iSide ‘ calculate the area PRINT " The area of a square of length "; iSide; " is "; iArea
3
Reserved Words REMthis is a remark or comment CLSclears the screen PRINTprint something on the screen ‘is an extra comment We will come across others later Clearly there are words that Qbasic recognises as instructions - hence we cannot use them for other purposes such as variable names
4
Variables We need to store data in our programs, to do this we use VARIABLES Imagine a variable is a box with a value in it The variable zmust have a NAME zcan be assigned a value zthe value may be altered by the program as it runs
5
A variable 2 Area in computer memory iSide Variable name Value stored in area of memory
6
Variable Names A Variable Name must: zStart with a letter zOnly contain letters and numbers Eg iSide fWeight iMarks iVal2 A Variable Name must NOT zContain spaces zBe a reserved word iFred Bloggs
7
Data Types Qbasic like many other programming languages insists that the variables be dedicated to store a particular type of data We can store a variety of data types: Integer numbers (6, 891, 0, -42) Floating Point numbers (6.4, 3.192, 0.0, -5.001) Single Characters (‘A’, ‘z’, ‘£’) Character strings (“Hello”)
8
Declaring variables zIt is good practice to declare each variable and its TYPE zWe need to decide what type of data we need to store i.e. DIM iSide AS INTEGER this declares (creates for use) the variable iSide to be an integer (whole number)
9
declaration examples DIM sLetter AS STRING DIM fWeight AS SINGLE DIM iCount AS INTEGER
10
Hungarian variable notation Convention: the first letter indicates the Type s = string i = integer (whole numbers) f = floating point (single,double) This is just a human convention so a reader can follow the program - Qbasic reads this as just another character
11
Eg.iSide = 2 this assigns (puts) the value 2 in the variable called iSide next Assigning a value Multiply what is stored in iSide by what is also stored in iSide puts the answer in the variable called iArea iArea = iSide * iSide
12
Output zA program must be able to give back results! zWe can put results onto the screen by using PRINT PRINT "The area of a square of side "; iSide; " is "; iArea gives on screen: The area of a square of side 2 is 4
13
Input zA program is more useful if we can give it some values while it is running zWe can do that using an INPUT statement
14
Another example program REM program to calculate area of a square REM written by: I Coulson REM date : 9/9/98 DIM iSide AS INTEGER DIM iArea AS INTEGER CLS 'clear the screen INPUT "Enter side of square "; iSide 'get a value for side iArea = iSide * iSide 'calculate area PRINT "The area of a square of length "; iSide; " is "; iArea END
15
Input statements INPUT "Enter side of square "; iSide 'get a value for side is a reserved word is what we want printed on the screen as a prompt is the variable we want to put the value into
16
The Input statement zWe can display a ? as part of the prompt message by using a ; character to separate the prompt message from the variable name E.g Input “Enter your name ” ; sName will give Enter your name ? The use of a, character will just display the prompt message e.g. Enter your name
17
Testing zWe must not assume that a program works correctly when it runs! zWe need to TEST it, by doing the calculations by hand, and then checking that the program gives the same answers
18
Documentation If anyone else needs to look at or run your program it will need DOCUMENTING zWhat the problem is zHow did you design it? zHow did you test it? zWhat sort of data does it use? zWhat does each line of code do? zHow do you run it?
19
Questions 1 What is a variable and why do we use them? 2 What is meant by assignment? 3 Why do we need to test and document a program? Return to view another lecture
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.