{{fbdoc item="title" value="FreeBASIC Primer #1"}}----
This primer is intended for beginning beginners, for those who are just starting to learn how to program and using [[FreeBASIC]] do to it.

{{fbdoc item="section" value="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:

	- ##[[KeyPgDim Dim]]##
	- ##[[KeyPgPrint Print]]##
	- ##[[KeyPgInput Input]]##
	- ##[[KeyPgFornext For...Next]]##
	- ##[[KeyPgIfthen If...Then]]##
	- ##[[KeyPgDoloop Do...Loop]]##


{{fbdoc item="section" value="Hello World!"}}
	No beginners reference is complete without this example.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer1.bas"}}%%(freebasic)
Print "Hello World!"
%%
	The text between the pair of double quotes is a literal string.  The ##[[KeyPgPrint Print]]## statement is used to output text to the display.  If you can edit, compile, and execute this example, you are on your way.

{{fbdoc item="section" value="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 ##[[KeyPgDim Dim]]## statement to declare a variable name and specify what type of information we want to store in it.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer2.bas"}}%%(freebasic)
Dim text As String
text = "Hello World!"
Print text
%%
	We are using ##[[KeyPgDim Dim]]## to let the compiler know that we want to use a variable named ##//text//## in our program and that we will be putting ##[[KeyPgString String]]## data in it.  We then assign (copy) ##"Hello World!"## in to the variable.  Finally, we use ##[[KeyPgPrint Print]]## to output it to the display.

{{fbdoc item="section" value="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.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer3.bas"}}%%(freebasic)
Dim a As String, b As String, text As String
a = "Hello"
b = "World"
text = 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.

{{fbdoc item="section" value="Getting Input from the User"}}
	Often, we have no idea what data is needed for a program unless the user gives it to us.  We can't put it in our source code since we won't know what it is until the user runs the program and tells us what it is.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer4.bas"}}%%(freebasic)
Dim answer As String
Input "Type something and press enter:", answer
Print "You typed: '"; answer; "'"
%%
	Here the ##[[KeyPgInput 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.


{{fbdoc item="section" value="Doing Some 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.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer5.bas"}}%%(freebasic)
Dim a As Integer, b As Integer, c As Integer

a = 5
b = 7
c = a + b

Print "a = "; a
Print "a = "; b
Print "a + b = "; c
%%
	We are assigning values to the variables ##//a//##, ##//b//## and ##//c//##.  We are using ##[[KeyPgInteger Integer]]## for the variables' data type.  An integer can be positive or negative, but not have any fractions.

{{fbdoc item="section" value="Doing Some Math with Input"}}
	This is similar to the previous example, except we will let the user choose the numbers we are going to add together.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer6.bas"}}%%(freebasic)
Dim a As Integer, b As Integer, r As Integer
Input "Enter a number:", a
Input "Enter another number:", b

r = a + b
Print "The sum of the numbers is "; r
%%
	##[[KeyPgDim Dim]]## lets the compiler know which variable names we want to use and that they are going to hold ##[[KeyPgInteger Integer]]## data.  We are using ##[[KeyPgInput Input]]## to get the numbers from the user, and ##[[KeyPgPrint Print]]## to display the results.

{{fbdoc item="section" value="Doing More Math with Input"}}
	Numeric variables are not limited to just integers.  We can also use  ##[[KeyPgSingle Single]]## or ##[[KeyPgDouble 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.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer7.bas"}}%%(freebasic)
Dim lb As Single, kg As Single
Input "Enter a weight in pounds:", lb

kg = lb * 0.454
Print lb; " lb. is equal to "; kg; " kg"
%%

{{fbdoc item="section" value="Repeating Statements"}}
	Using ##[[KeyPgFornext For...Next]]## statement we can tell the program to do something repeatedly a set number of times.  For example lets say we wanted to add up all the numbers from 1 to 100.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer8.bas"}}%%(freebasic)
Dim total As Integer
Dim number As Integer
total = 0
For number = 1 To 100
  total = total + number
Next
Print "The sum of number from 1 to 100 is "; total
%%


{{fbdoc item="section" value="Making a Decision"}}
	A program can choose which statements to execute using a conditional statement like ##[[KeyPgIfthen If...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.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer9.bas"}}%%(freebasic)
Dim number As Integer
Input "Enter a number : ", number
Print "Your number is ";
If number < 0 Then
  Print "negative"
ElseIf number > 0 Then
  Print "positive"
Else
  Print "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.

{{fbdoc item="section" value="Repeating Statements (Again)"}}
	Here we will use another looping structure ##[[KeyPgDoloop Do...Loop]]## to repeat some statements.  How will the program know to stop repeating the statements?  We will use ##[[KeyPgIfthen If...Then]]## to make the decision when to get out of the loop.

	{{fbdoc item="filename" value="examples/manual/proguide/primer/primer10.bas"}}%%(freebasic)
Dim total As Single, count As Single, number As Single
Dim text As String

Print "This program will calculate the sum and average for a"
Print "list of numbers.  Enter an empty value to end."
Print

Do
  Input "Enter a number : ", text
  If text = "" Then
	Exit Do
  End If

  count = count + 1
  total = total + Val(text)

Loop

Print
Print "You entered "; count; " numbers"
Print "The sum is "; total
If count <> 0 Then
  Print "The average is "; total / count
End If
%%

{{fbdoc item="see"}}
	- ##[[KeyPgDim Dim]]##
	- ##[[KeyPgPrint (Print | ?)]]##
	- ##[[KeyPgInput Input]]##
	- ##[[KeyPgFornext For...Next]]##
	- ##[[KeyPgIfthen If...Then]]##
	- ##[[KeyPgDoloop Do...Loop]]##

{{fbdoc item="back" value="CatPgProgrammer|Programmer's Guide"}}