300 likes | 471 Views
E0001 Computers in Engineering. Qbasic Commands. Readings & activities. Study Book; Module 8 & 9 note error in summary of readings handout Qbasic with an Intro to Visual Basic by Schneider; p40 to 47; p55 to 57 Practice Problems and Exercises 3.1 & 3.2 as necessary.
E N D
E0001 Computers in Engineering Qbasic Commands
Readings & activities • Study Book; Module 8 & 9 note error in summary of readings handout • Qbasic with an Intro to Visual Basic by Schneider; p40 to 47; p55 to 57 • Practice Problems and Exercises 3.1 & 3.2 as necessary
Key points from last (programming) lecture? • how to access Qbasic • menu system • save and retrieve a program • program development cycle • programming tools • flowchart; pseudocode; top down charts
This lecture • what is a variable • types of variables • arithmetic operations • PRINT statement
Payrate = 6.25 hours = 25 grosspay = payrate * hours PRINT grosspay END A general solution?
Variables • Quantities referred to by symbolic names • make general solutions • Variable name: is the name of a storage location in primary memory where Qbasic stores the value of the variable • value can change during program execution
X = 0.5 y = 10 z = x + y total = z + x y = total x = 10 x = x+y Assignment of variables
Assigning variables [LET] variablename = variable • e.g. LET x = 2 x = 2 LET sum = 24 sum = 24 LET counter = counter +1 total = total + x
Variable names • may only contain letters, digits and full stop • may not contain a blank space • must start with a letter and may be up to 40 characters • may NOT be a reserved word e.g let, print • generally given a value of 0 initially but...
A 4sale Test1 Rumplestiltskin %Interest Gross Pay Grosspay GroSSPay Valid invalid valid valid invalid invalid valid valid Valid names
single-precision default type double-precision scientific notation for printing very large or very small numbers integer long-integer string -3.37*1038to 3.37* 1038 -1.67 *10308to 1.67* 10308 1 * 10-5 = 1E-5 .000013596426 = 1.359643E-5 -32 768 to 32 767 -2 147 483 648 to 2 147 483 648 any alphanumeric Types of Variables
TYPE statement e.g. DIM x AS integer DIM variablename AS type Types single double integer long string Suffix on variable name ! (Single) # (Double) % (Integer ) & (Long) $ (String) Assigning variable types
Output of Program? x = 3.1234 PRINT x% what type of variable is x%? x = 3.123456789 PRINT x
Strings • Strings are groups of characters which represent words, sentences, letters etc • Two types • String Constant • sequence of characters surrounded by quotes • quotes not part of the string constant • String Variables • used to hold a string value • similar to numeric variable • initial value - “” (null or empty string)
Examples hello$ = “welcome to Engineering” PRINT hello$ Let answer$ = “yes” IF answer$=“no” THEN PRINT hello$
Arithmetic Operations • five arithmetic operations • additions (+); subtraction (-); division(/); multiplication (*); exponentiation (^) • executed in a specific order • ( ) inner to outer; left to right • ^ left to right • * and /; \ (integer division); MOD (modulus); left to right • + and - ; left to right
PRINT statement • basic form: PRINT operation (arithmetic) • PRINT 3 * 2 • PRINT variablelist • PRINT a, b, c orPRINT x; y; z • PRINT answer$ • PRINT “prompt”, variablelist • PRINT “the answer is”, a
Screen Placement & Formatting • PRINT zones • line has 80 characters • subdivided into 5 zones • zones 1 - 4 have 14 characters • zone 5 has 24 characters • zones start at columns 1, 15, 29, 43 & 57
Print zones contd • commas as separators in PRINT statement • next item printed in next zone • statement ends in comma • next item printed in next print zone • semicolon as separators in PRINT statement • separated by 2 spaces • statement ends in semicolon • next item separated by 2 spaces
x = 2 y = 3 * x x = y + 5 PRINT x + 4 y = y + 1 END x y 2 6 11 OUTPUT - 15 7 Determine variables and outputs
single precision integer double precision string variable total = 6 total% = 6 total# = 6 total$ = “6” Determine types of variables
DIM x AS LONG DIM y AS STRING DIM z AS DOUBLE DIM zz AS INTEGER x& y$ z# zz% Dim statement
Problem • draw a flowchart which will • define variables for pi, radius and circumference as double, integer and double respectively • set radius = 3.2, pi = 22/7 • calculate the circumference • print all variables
Key points • arithmetic order • types of variables and how to assign specific types • PRINT statement
Your turn • Draw a flowchart that take two times in hours, minutes and seconds and will calculate the total time in hours minutes and seconds e.g 2 hr, 15 min & 12 sec + 1 hr 10 min and 5 sec = 3 hrs 25 min and 17 sec
Variable names - what can you remember? • used for general solutions • value may change during the program • may only contain letters, digits and full stop • may not contain a blank space • must start with a letter and may be up to 40 characters • may NOT be a reserved word e.g let, print • generally given a value of 0 initially but..