Qbasic Programming
Qbasic Programming
Lecture [6]
Current
line
Current
column
The Constants:
1.
2.
Numeric constants: include all numbers (real, not real, integer, ..).
Example: 25, -230, 0, 16.44, 0.88
Character constants: include all characters sets (letters, digits, symbols) between two . Example: BASIC
, The width is = 83 , Telephone Number 07901 .
The Variables:
A variable is a name which can contain a value. The variables must include the conditions below:
1. From A to Z (include all letters).
2. Not contained symbols except dot (.).
3. Maximum length of variable is 40.
4. Must not represent any word which is defined as a special word in QBASIC.
5. Must be start by letters.
A. Numeric Variables:
Like: M, A2, WE,.etc
In Numeric variables the symbol (%) (Integer) mean make the numeric variable as real number.
Like: A%=6.2
its mean A%=6
A%=6.6
its mean A%=7
A%=6.5
its mean A%=7
A%=-6.5
its mean A%=-6
Using symbol (&) (Long) with numeric variable make it long variable.
Using symbol (!) (Single) with numeric variable mean the length of variable equal to 7 digits or less.
Using symbol (#) (Double) with numeric variable mean the length of variable more than 7 digits.
B. Character (String) Variables: If the variable holds symbols or text, it may be a character variable or a string
variable. like: M$, A2$, WE$,.etc
Numeric variables:
Single-precision variables (floating-point variables): these types of variables are used
to store numbers that contain a decimal value such as 1.89 or 3.141593.
INTEGER: A non-floating point variable (no decimal value) that can store integers
between -32,768 and 32,767
LONG: same as INTEGER, but can contain numbers between -2,147,483,648 and
2,147,483,647
DOUBLE: same as SINGLE, but can have twice as many digits. (like: 983288.18)
SINGLE: single precision variables. (like: 39.2932)
Example:
B=26
A$= I Like to Learn QBASIC
PRINT B
PRINT A$
Output:
26
I Like to Learn QBASIC
Strings variables:
String variables are ones that can hold all ASCII characters (Letters, Numbers, Symbols). They
can not be used in math problems. When asking questions about
them, or changing their content, the expressions must be in quotes.
Example:
String variables are letters and numbers followed by a dollar sign ($).
X$= "Hello World ! "
String names must have a letter as the first character,
D$= "Hi +X$
but everything else is up to you.
PRINT X$
Examples:
PRINT D$
" 0123456789 "
" abc123 "
" 1+1=2 "
Output:
"!@&%$?><^ "
Hello World !
" Hi "
Hi Hello World !
Expressions:
Operator
Function
Example
Result
Add
8+2
10
Multiply
8*2
16
Divide
8/2
Subtract
8-2
Exponentiation
8^2
64
Priority of operations:
1. The parenthesis (from left to right & from inside to outside).
2. The exponentiation (from left to right).
3. Division & Multiply (from left to right).
4. Add & Subtract (from left to right).
Example:
Z= 500+(10*7)
rate=50
time=2
distance=rate*time
PRINT Z
PRINT distance
Output:
570
100
Example: Write the following expression using BASIC format. And define the priority of the
calculation for the expression
Solution:
X = (A^3
7*
3
6
7
2+3/4*5
2+3^2
3*4/5
3+4-5
= 5.75
2+9 = 11
12/5 = 2.4
7-5 = 2
Comparison Operators:
Operator
Meaning
Example
Equal to
IF a=15 THEN...
<>
Not equal to
IF a<>15 THEN...
<
Less than
IF a<15 THEN...
<=
Less or equal to
IF a<=15 THEN...
>
More than
IF a>15 THEN...
>=
More or equal to
IF a>=15 THEN...
Logical Operators:
1. OR operator: the result of this operator is true if one of the statements is true.
A
OR
Example
True
True
True
True
False
True
False
True
True
False
False
False
2. AND operator: the result of this operator is true when both statements
are true.
A
Example
AND B
True
True
True
True
False
False
False
True
False
IF 10<2
False
False
False
IF 10<2
3. NOT operator:
Example:
NOT 15 > 10
NOT 15 = 8
NOT (5 >= 5 AND 6/2= 4)
False
True
True
True
C<100
False
2
True
False
3
True
False
CLS
This command clears the screen
REM Program to compute area
LET command:
General Form:
LET X= A+B*C
LET J=J+1
LET C=SQRT (A*A+B^2)