Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP1020 - Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions.

Similar presentations


Presentation on theme: "CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP1020 - Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions."— Presentation transcript:

1 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP1020 - Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions zArranging output on the screen

2 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Arithmetic Operations zFundamental to computing as most computer programmes consist of either mathematics or manipulating text  We shall look at the operations available and the rules that govern their use

3 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Common Mathematical operations Arithmetic operationSymbol Example Exponentiation^ 2^3 = 8 (2*2*2 or 2 3 ) Mult. & division*, / 2/4 = 0.5 Addition & subtraction+, - 2+2=4

4 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Operator precedence What does this mean ? Which mathematical operation will take place first! E.g take the sum: iMyAnswer = 2 + 3 / 4 could give the result iMyAnswer = 1.25 if we add 2 + 3 then divide by 4 or = 2.75 if divide 3 by 4 then add it to 2 This obviously makes a big difference to the result

5 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner The order of calculations QBasic has a fixed set of rules on the order of how these operations are performed. Here is a simplified version Order Operation Symbol 1 BBrackets( ) 2 OExponentiation ^ 3 DMDivision/ Multiplication /,* 4 AS Addition/Subtraction+, - operations on same level performed left to right

6 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Operator precedence in action iMyAnswer = 6 / 2 + 8 * 2 - 12 / 6 Working left to right 6 / 2 evaluated first > 3 8 * 2 evaluated second > 16 12 / 6 evaluated third > 2 then QBasic will work left to right to perform the next order of operations : that is 3 + 16 - 2 so iMyAnswer = 15

7 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Examples iAnswer1 = 4^2 + 6 / 2 - 3 iAnswer1 = 16 + 3 - 3 = 16 iAnswer2 = 35 / 7 - 8 / 4 + 5 * 2 iAnswer2 = 5 - 2 + 10 = 13 iAnswer3 = 5 - 5 * 2 * 2 ^ 3 iAnswer3 = 5 - 5 * 2 * 8 iAnswer3 = 5 - 80 = -75

8 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Use of Brackets Brackets force the machine to perform the calculations in the order we want:- e.g iMyAns = ((1 + 4) / (7 - 2)) + ((16 + 4) * (3 - 2)) calculations in the inner most brackets are performed first - (1 + 4).. (7 - 2)..(16 + 4).. (3 - 2) the results of these are used in the next level of brackets (5 / 5).. (20 * 1) finally the next level is calculated 1 + 20 = 21

9 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Some Concepts and Jargon KEY or RESERVED WORDS - these are words or groups of letters that are used by QBASIC to perform certain mathematical operations, procedures etc. And so can’t be used as variable names e.g. PRINT, CLS STATEMENTS -usually a line of code, for instance PRINT “Principles of Programming”

10 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner QBasic standard functions A function has the following format Answer function name value to be worked on It is easiest to explain what a QBasic standard function is by the use of an example:- iArea= 9 iSide = SQR(iArea) PRINT iSide Here we are working out the square root of 9 Variable = Function(variable)

11 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner QBasic Standard Functions The steps are :- iSide = SQR(iArea) 3) the returning value is assigned to variable iSide 2) the function somehow determines the answer 1) pass the number into the function using variable iArea

12 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner A selection of other functions available QBasic has a range of other available functions that are coded in a similar manner :- Functionreturnsexample INT(X) largest integer <= to x INT(9.13) gives 9 ABS(X) absolute value of X ABS(-11) gives 11 LEN(string)number of characterssMyString = “abc” in a string LEN(sMyString) gives 3

13 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner An example program REM A program to print the length of string REM Ian Coulson REM 24th September 1999 DIM sExample AS STRING DIM iStringLength AS INTEGER LET sExample = “abc” LET iStringLength = LEN(sExample) PRINT “The string abc has “; iStringLength; “ characters” END Statement Reserved word Function

14 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Printing on the screen zQbasic divides the screen into 80 columns and 25 rows. 25 rows 80 columns zQbasic has a number of mechanisms to arrange where on the screen you wish to print information zHave a look at the following examples

15 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Print formatting - using the TAB function PRINT TAB(20); “This is the first thing printed ” PRINT “Next thing printed” This is the first thing printed Next thing printed Start printing at a particular column - in this case the 20th

16 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Print formatting - the SPC function PRINT“First thing printed”; SPC(10);“Next thing printed” First thing printed Next thing printed Start printing 10 spaces after the last thing printed

17 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Print formatting - the locate statement LOCATE 10, 15 PRINT “This is printed here” this moves the cursor to start printing at the specified row and column number rowcolumn This is printed here Row 10 Column 15

18 CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Review questions 1) Using Qbasic rules of operator precedence determine what the following equates to 3 ^ 3 + 23 - 5 * 2 ^ 2 2) Write a statement (a line of code) to obtain the square root of a value stored in fNumber, and store it in another variable called fRoot.


Download ppt "CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP1020 - Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions."

Similar presentations


Ads by Google