Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning a variable
#1
It's a dumb question but how can I assign a variable that isnt a integer, besides using INPUT.

This is all I know about assigning variables:
Code:
main:
        CLS
        f1 = f1 + 1: REM-f1 is a variable that was assigned as 1
        f2 = f2 + 2: REM-f2 is a variable that was assigned as 2
        PRINT "It is"; f1; f2
Reply
#2
I checked in the search option but couldn't find anything, or else I wouldn't have made this topic.
Reply
#3
% INTEGER
# LONG (double-integer)
! SINGLE (floating point)
# DOUBLE (2x floating point precision)
$ STRING (dynamic byte array)
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#4
Um... I think he's gonna need a little more help than that, aga.

You add those symbols to the end of the variable. For example, to create a long variable:

VarName# = 342134

Or a string variable:

VarTwo$ = "String variables hold words."

If you have Qbasic 4.5, go to help>contents>data types to learn more.
Reply
#5
Quote:It's a dumb question but how can I assign a variable that isnt a integer, besides using INPUT.
I'm not sure what you mean by "assign a varable that isn't an integer".

Variables can be declared as having on of the following data types:
integer --- 2 bytes from -32,7688 to +32,767
long ------- 4 bytes from -2,147,483,648 to +2,147,483,647
single ----- 4 bytes, single precision floating point
double ---- 8 bytes, double precision floaing point

Variables are assigned by storing or setting a value into them. You can "assign" variables as follows:

1) If you just say f1=1
f1 will be set to 1, and declared as a single precision floating point variable which is the QB default.

2) However, if before the above statement you wrote DEFINT A-Z
then all subsecuent variables will be declared as integers, unless you explicitly said fx# = 1 in which case fx# is explicitly declared as a double precision floating point variable.
It's a good idea to do DEFINT A-Z at the beginning of your program because most of the time you want to declare variables as intergers, except for those declared explicitly. DEFINT A-Z says declare all variables with letters beginning with A to Z as integers, unless declared explicitly. There's also DEFLNG, DEFSNG, and DEFDBL but these are rarely used.

3) You can use any of the following suffixes after the name of a variable to explicitely declare the data type of the variables:
% for integer
& for long integer
! for single precision floating point
# for double precision floating point

4) I personally don't like using the above suffixes on the varaible names because they make the code more difficult to read. I prefer to declare them explicitly at the top of the program or function as follows:
dim a as integer
dim b as long
dim c as single
dim d as double

5) QB has the peculiarity of allowing you to use (reference) a variable that was never assigned. In you example f1 = f1 +1
you never previously assigned a value to f. QB knows this and gives you a default assigned value of zero. So, the first time you execute this instruction f1 will be 0 + 1 = 1. Don't rely on this QB "feature", if you want a variable to contain zero at the beginning, then initialize it to zero at the top of your program.

6) So, if you want to assign a starting value to a variable, you don't need to input the starting value, you can assign it as follows:
b = 5

7) Sometimes you want to assign a permanent value to a variable, that is, make it a constant that can't be changed. For this, use the CONST statement. For example, set a constant for the maximum number of characters allowed for a name field:
const maxname = 40
If by mistake some instruction in your program attempts to store something on top of maxname, the compiler will signal an error.

Hope this info helps.
*****
Reply
#6
Sorry maybe I worded my first post wrong.

So could I do this then?
const m$ = male. So that when someone types h it says "PRINT "You are a"; m$
Reply
#7
Ok, now we're talking about string variables.
Using your example,
Somehow, the program determines the sex, for example:
Let's say that we have a variable called SEX and that it's equal to 1 if male or equal to 2 if female, then:

IF SEX = 1 THEN M$ = "MALE" ELSE M$ = "FEMALE"
PRINT "YOU ARE A "; M$

Note that when you put string data into a string variable, the string data must be enclosed in quotes.
*****
Reply
#8
So, is a string used to combine variables?
Reply
#9
A string is a set of byte-value (0 to 255) variables that can be changed in size by adding strings together or separating them, using MID$, left$, right$, etc.. and can be converted to an integer
(-32768 to 32767) by doing ASC(bla$) .... or VAL$(bla$)...............................
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#10
Aga, you don't think all the low level stuff about bytes and stuff would help the average person? Na... Smile

A string is a variable that can hold letters as well as numbers. Eg:

Code:
iamastring$ = "Hello, world!"
iamnotastring% = 4

Strings can also represent numbers, eg: lala$ = "1345". However, you can't add/subtract etc when it is a string. That's why there are integers, doubles...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)