1 / 18

CP1020 - Week 3

CP1020 - Week 3. Arithmetic operations Operator precedence Simple QBasic functions Arranging output on the screen. Arithmetic Operations. Fundamental to computing as most computer programmes consist of either mathematics or manipulating text

Télécharger la présentation

CP1020 - Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CP1020 - Week 3 • Arithmetic operations • Operator precedence • Simple QBasic functions • Arranging output on the screen

  2. Arithmetic Operations • Fundamental 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. Common Mathematical operations Arithmetic operation Symbol Example Exponentiation ^ 2^3 = 8 (2*2*2 or 23) Mult. & division*, / 2/4 = 0.5 Addition & subtraction+,- 2+2=4

  4. 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. 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 B Brackets ( ) 2 O Exponentiation ^ 3 DM Division/ Multiplication /,* 4 AS Addition/Subtraction + , - operations on same level performed left to right

  6. Operator precedencein 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. 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. 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 inthe 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 levelis calculated1 + 20= 21

  9. 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 socan’t be used as variable names e.g. PRINT, CLS STATEMENTS -usually a line of code, for instance PRINT “Principles of Programming”

  10. Answer function namevalue to be worked on QBasic standard functions A function has the following format Variable = Function(variable) 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

  11. 1) pass the number into the function using variable iArea 2) the function somehow determines the answer 3) the returning value is assigned to variable iSide QBasic Standard Functions The steps are :- iSide = SQR(iArea)

  12. A selection of other functions available QBasic has a range of other available functions that are coded in a similar manner :- Function returns example 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 characters sMyString = “abc” in a string LEN(sMyString) gives 3

  13. Reserved word Function Statement 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

  14. 25 rows 80 columns Printing on the screen • Qbasic divides the screen into 80 columns and 25 rows. • Qbasic has a number of mechanisms to arrange where on the screen you wish to print information • Have a look at the following examples

  15. This is the first thing printed Next thing printed Start printing at a particular column - in this case the 20th Print formatting - using the TAB function PRINTTAB(20); “This is the first thing printed ” PRINT “Next thing printed”

  16. First thing printed Next thing printed Start printing 10 spaces after the last thing printed Print formatting - the SPC function PRINT“First thing printed”; SPC(10);“Next thing printed”

  17. row column this moves the cursor to start printing at the specified row and column number Column 15 Row 10 This is printed here Print formatting - the locate statement LOCATE 10, 15 PRINT “This is printed here”

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

More Related