Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

Monday, May 23, 2011

Beginning Programming with Just BASIC -- Tutorial 6: File Input and Output



Tutorial 6: File Input and Output

Suppose we want to be able to close a program to go eat supper and come back later. But we want to save all our data. By now you should be familiar with the fact that variables and arrays reset when you close a program.

You can print data to a file. Like this:

input "Give your file a name: "; filename$
open filename$ for output as #save
print #save, "Yotta yotta homina homina jiggabuh."

Some notes about this code: #save is called a handle, and it is used to refer to the output file. Any variable name preceded by a # is a handle, which are used to describe windows in general. So, if you run this program and name your file "output.txt", a new file will be created in the folder that your basic file is saved in named "output.txt," and it will contain a line that says:

Yotta yotta homina homina jiggabuh.

You can also do input files, so you can load a file into your program that you previously saved.

input "What file would you like to open? "; filename$
open filename$ for input as #load
for i = 1 to n
    input data$(i)
next

Here is an example program that allows you to save your work:

[start]
    print "Would you like to (1) write a story, (2) read one you wrote, or (3) exit?"
    input answer
    select case answer
        case 1: goto [write]
        case 2: goto [read]
        case 3: goto [quit]
    end select

[write]
    input "How many lines will your story be? "; n
    dim txt$(n)
    print "Write your story:"
    for i = 1 to n
        input txt$(i)
    next
    input "Enter a name for the file: "; filename$
    open filename$ for output as #save
    for i = 1 to n
        print #save, txt$(i)
    next
    close #save '<- Closes the file afterward
    goto [start]

[read]
    input "What is the file name? "; filename$
    open filename$ for input as #load
    input #load, n
    dim txt$(n)
    for i = 1 to n
        input #load, txt$(i)
        print txt$(i)
    next
    close #load
    goto [start]

[quit]
    end


Some notes about this code: This is a basic example of a program where the user will be producing something (in this case, a story), and then can read it later. Instead of doing it this way, you should look into how to do a file dialog window (it's easier than you might suppose!).

goto [nextTutorial] 

Sunday, May 22, 2011

Beginning Programming with Just BASIC -- Tutorial 5: Arrays



Tutorial 5: Arrays

Arrays are similar to variables, except that they can hold more than one value. For instance, a list of names and grades. This is a good way to organize data, especially when little is known about the data that is going into them, in particular how many data point there are.

To set up an array in your program requires a little more work than it takes to use a variable. First you have to define how big the array will be, that is, how many values it can hold. You do that with the DIM command:

dim myArray(100)

In parentheses you put a number defining the size of the array. In this case I can put 100 values into the array called myArray(). So how do I do that?

With loops! Arrays are often used in conjunction with FOR loops because of how easy it is to systematically define every value.

for i = 1 to 100
    input myArray(i)
next

This is far easier than having to write out

input myArray(1)
input myArray(2)
input myArray(3)...

You get the picture.

You can also have multidimensional arrays. Say you want to associate two parameters to the same value. You can do that easily:

dim twoDimensional(10,10)
for i = 1 to 10
    for j = 1 to 10
        twoDimensional(i,j) = i*j
    next
next

Those are the essentials of making an array. I decided to include them early on so you don't waste time trying to use variables for some complex program.

goto [nextTutorial]

Friday, May 20, 2011

Beginning Programming with Just BASIC -- Tutorial 4: Loops



Tutorial 4: For Loops and While Loops

Maybe you want to run a piece of code through several times. Instead of using GOTO, here's a snazzy way to accomplish this. There are two types of loops, the FOR loop and the WHILE loop. They are similar but are treated differently.

The FOR loop will run through the code a predetermined number of times, keeping track of how many times it has run through the loop. Here is how you would set up a FOR loop:

for i = 1 to n
    some code
next

For example,

for i = 1 to 1000
    print i
next

This code prints out the numbers 1 through 1000.

You can also have the loop count by an interval other than 1 (the default):

for i = 2 to 1000, step 2
    print i
next

This code prints out all the even numbers from 2 to 1000.

The WHILE loop is treated differently, but it can still function the same way. A WHILE loop will continue to run the code until a certain condition is met. For instance:

x = 1
while x < 1000
     x = 2*x
     print x
wend

As long as the value of x is less than 1000 x will continue to be doubled and print to the screen.

And that is all. There is no secret aspect of loops lurking in the shadows waiting to spring out and surprise you with complexity. What you see is all there is.

That said, let's write a program using loops:

Ever wonder how a computer calculates the square root of a number? Well, read further and you shall be enlightened.

You start with a guess, G, and you loop through this overwrite statement

G = (G + x/G)/2

until G is approximately equal to the square root of x.

Take note, however, that if you actually were going to take the square root of something in everyday programming, you'd use a built in function for that. Or just raise the number to the 1/2 power.

i = 0
print "Square root of what?"
input x
print "To + or - what precision? (%)"
input precision
precision = precision/100
lowerx = (1 - precision)*x
upperx = (1 + precision)*x
print "Guess?"
input G
while G^2 < lowerx and G^2 > upperx
    G = (G + x/G)/2
    i = i + 1
wend
print "The square root of x is "; G; " in "; i; " iterations."
end

This type of calculation is called numerical methods, and the subject is so vast that it has its own field of study.

goto [nextTutorial]