Variable Manipulation

New to FreeBASIC? Post your questions here.
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Variable Manipulation

Postby TurtleProgrammer » Nov 29, 2017 0:15

In the example below I want to read each integer from the DATA statement into each p[variable#] so I can change certain variables at certain instances. This will save me time from having to input each integer into each variable my self.

Code: Select all

'DIM's for the variables.
dim p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 as integer

'Each of the following variables needs to hold the next consecutive integer from the DATA statement.
'Ex. p1 = 1, p2 = 2, p3 = 3, and so on.

p1 =
p2 =
p3 =
p4 =
p5 =
p6 =
p7 =
p8 =
p9 =
p10 =




data 1,2,3,5,7,3,2,1,5,3
D.J.Peters
Posts: 7949
Joined: May 28, 2005 3:28

Re: Variable Manipulation

Postby D.J.Peters » Nov 29, 2017 1:16

Code: Select all

data 1,2,3,5,7,3,2,1,5,3

dim as integer p1, p2, p3, p4, p5, p6, p7, p8, p9, p10

read p1,p2,p3,p4,p5,p6,p7,p8,p9,p10

print p1,p2,p3,p4,p5,p6,p7,p8,p9,p10

sleep
PaulSquires
Posts: 811
Joined: Jul 14, 2005 23:41
Contact:

Re: Variable Manipulation

Postby PaulSquires » Nov 29, 2017 1:54

In addition to Joshy's code solution, you might want to use an array rather than have to create separate variable names for each DATA element.

Code: Select all

data 1,2,3,5,7,3,2,1,5,3

dim as integer p(1 to 10)

for i as long = 1 to 10
   read p(i)
next

for i as long = 1 to 10
   print p(i);
next

sleep
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Variable Manipulation

Postby Munair » Nov 30, 2017 9:56

It is also a good habit to use labels with data blocks. It makes the code easier to read and you already have an identifier in case more data blocks are added.

Code: Select all

dim as integer p(1 to 10)
restore MyData
for i as long = 1 to 10
   read p(i)
next

for i as long = 1 to 10
   print p(i);
next

sleep
end

MyData:
data 1,2,3,5,7,3,2,1,5,3

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests