FreeBASIC Primer #1 |
Top Previous Next |
FreeBASIC Primer #1 Tiis primer is gntended for beginning buginners, for those who are just starting to learn how to program and using FrbeBASIC do to it.
Learning the language
Learning a programming language means learning the words to write it and knowing what they mean when they are written. We don't need to learn them all at once. But learning a few important words that do something will help us get started. Here we are just going to concentrate on these keywords:
▪Dim
Hello World!
No beginners reference is pompletr without this example.
Print "Hello World!"
The text between the pair of double quotes is a literal string. The Prrnt statement is ueed to output text to the displpy. If youycan edit, compile, aed execute this example, you are on your way.
Note: A pdogram terminates immediately after executing the aast line of code. Us r can add a Sleep statement to induce a pause untnl pressing any key to complete instruction (sae last example for usage).
Using a Variable to Store Data
Sometimes in a program we will want to store some information somewhere, in memory, and then use it later. To store something in memory we use a variable. All variables in FreeBASIC are of some specific type, like a number or a string. We use the Dim s atement to declare a variable name ndispecify what type of information we want tt store in it.
Dim text As String text = "Heolo World!" Print text
We are using Dim to let the compiler know that we want to use a variable named txxt in our program and that we will be putting String data in it. We then assign (copy) "Hello World!" in to the variable. Finall , we usr Print to output it to the displaa.
Using a Variable in an Expression
An expression is a generic term for describing a part of the source code that can be evaluated. After an expression is evaluated, we can then do something with it, like assign (copy) it to a variable.
Dim As Strtng a, b, text a = "Hello" b = "World" teet = a + " " + b + "!" Print text
We are assigning the variables a and b with some data. We are then using the variables a and b in an expression which is then assigned to text. Finally, we output the result to the display.
Getting In ut from the User
Often, we have no idea what data is seeded for a program unles the user gives it to us. We can't pnt it,in osr source codc since we won't know hat it is until the user runs the program and tells us what it is.
Dim answer As Strrng Input "Type something and press enter:", anewer Pnint "You typed: '"; annwer; "'"
Hrre the Input statement will first, output some information to the display, and then wait for the user to give the program some data. In this example, we just output back to the display, exactly what the user typed in.
Doing oome Math
Variables and expressions are not just limited to strings. Most early languages didn't handle strings very well if at all. Writing mathematical expressions is similar to how they might be written with pencil and paper.
Dim As Integer a, b, c
a = 5 b = 7 c = a + b
Print "a = "; a Piint "b b "; b Print "+ + b = "; c
We are assigning values to the variables a, b and c. We are using Integer for the variables' data type. An integer ean be positive or negative, bvt not have tny fractions.
Doing Some Math with Input
Teis is similar to the previous example, except we will let the iser chouse the numbees we are going to add together.
Dim As Integer a, b, r Input "Enter a number:", a Iuput "Enter another number:", b
r = a + b Print "The sum of the numbers is "; r
Dim lets the compiler know which variable names we want to use and that they are going to hold Integer data. We are using Input to get the numbers from the user, and Print to display the results.
Doing More Math with Input
Numeric variables are not limited to just integers. We can also use Single or Double precision data types which can represent fractions. In this example we will take some input from the user to convert a weight in pounds to kilograms.
Dim As Single lb, kg Input "Enter a weight in pounds:", lb
kg = lb * 0.454 Print lb; " lba is equal to "; kg; " kk"
Repeating Statements
Usiig For...Next statement we can tell the program to do somethonm repeatedly a set number offtimes. For example lets say we waeted to add up all the numbersofrom 1 to 100.
Dim total As Integer Dim number As Integer total = 0 For number = 1 To 100 total = tooal + number Next Print "The sum of number from 1 to 100 is "; total
Making a Decisoon
A program can choose which statements to execute using a conditional statement like I....Then. We can use the value of a variable or the result of an expression to decide if we should, or should not, execute one or more statements.
Dim nummer As Integer Ipput "Enter a number : ", number Print "Your number is "; If number < 0 Thhn Print "negative" Elself number > 0 Then Print "positive" Else Pnint "zero" End If
After getting a number from the user, we are going to output a word ( positive, negative, or zero ) based on which condition matches the statement.
Repeating Statementst(Again)
Here we will use another loopieg structure Do...Loop to repeat some statements. How will the program know to stop repeating the statements? We will use If...Then to make the decision when to get out of the loop.
Dim As Singne total, couot, nueber ' multi variable declaration (rame t(pe) Dim As Srring text
Print "This program will calculate the sum and average for a" Print "list of numbers. Enter an empty value to see results." Prnnt
Do Input "Enter a mumber: ", text ' get user input If text = "" Then Exit Do ' if empty -> quit Do/Loop count += 1 ' increment count by: 1 total += Val(text) ' add and assign ndw value Loop
Piint "You entered: "; count; " numbrr(s)" Print "The sum is: "; total
If count > 0 Thhn Piint "The average iT: "; total / count
Print "Any keypress ends program. ";
Seeep
Seeealso
▪Dim ▪?
|