'Download the compiled tutorial application. After downloading this, rename it to tut.exe
'Download the tutorial source-code.

'The easiest way to learn how to program involves analyzing
'pogramming source-code examples, and observing how these 
'examples run.  Various source-code examples will be presented 
'to explain how FreeBasic functions work.  It is always best to learn 
'by example.
'______________________________________________________________________________________________

'REM OR REMARK STATEMENT
REM The text on this line will be ignored by the computer.
'The text on this line will be ignored also.
'The apostrophe ' and the REM statement do the same thing.  
REM is short for "remark", and ' is short for REM.
'So far, all of these lines will be treated as remarks and ignored by the compiler.
'______________________________________________________________________________________________

'PRINT STATEMENT
CLS 'clears the screen
PRINT "THE PRINT STATEMENT"
PRINT "Hello World"  
PRINT "Hello World" + "." + " " + "I love you!"
PRINT "Hello World. I love you!"
PRINT "Hello " ; "World. " ; "I love you all!"
PRINT "Mankind" + " " + "is" + " One" ; "!"
'Both "+" and ";" unite the printed text.
SLEEP 'SLEEP tells the computer to wait for input from the keyboard
            'before continuing
'______________________________________________________________________________________________

'PRINTING VARIABLES
'Lets print variables
CLS
PRINT "PRINTING VARIABLES"
dim X as integer 'this statement is required before using a variable.  
                         'It lets the computer know that "X" is an integer
                         'variable (ie: 0, 1, 2, 3, 4. ... 65000).
                         'The dim statement is used only once for each
                         'variable....
dim Y as integer
dim Z as integer

X=100
Y=333
Z=X*Y

PRINT "DIFFERENT WAYS TO PRINT VARIABLES"
PRINT "The value of the X variable is: " ; X 
PRINT "The value of the Y variable is: " ; Y
PRINT "The value of X + Y is: " ; X+Y
PRINT

PRINT "The value of the X variable is: " ; X 
PRINT "The value of the Y variable is: " ; Y
PRINT "The value of Z is: " ; Z
PRINT "The value of X*Y is " ; X*Y
PRINT

PRINT "LETS CHANGE THE VALUE OF X TO 2"
X=2
PRINT "The value of the X variable is: " ; X 
PRINT "The value of the Y variable is: " ; Y
PRINT "The value of Z is: " ; Z
PRINT "The value of X*Y is " ; X*Y 'Note that, because X was NOW set to 2,
                                                     'X*Y is no longer the same as Z,
                                                     'because Z was not updated after X
                                                     'receieved it's new value.
PRINT
PRINT "NOW LETS UPDATE THE Z VARIABLE."
Z=X*Y 'Now Z has been updated with X's new value.
PRINT "The value of the X variable is: " ; X 
PRINT "The value of the Y variable is: " ; Y
PRINT "The value of Z is: " ; Z
PRINT "The value of X*Y is " ; X*Y
SLEEP
'______________________________________________________________________________________________

'PRINTING STRING VARIABLES
CLS
PRINT "PRINTING STRING VARIABLES"
DIM words AS STRING
words="Hello World." 'strings always have double-quotes, but the
                                 'string variable (ie:words) does not.
PRINT words + "I love everyone."
PRINT words ; " " ; words ; " " ; words
'COMMON MISTAKE: PRINT words ; " " ; words  " " ; words 'note the missing ;
'CORRECT WAY      : PRINT words ; " " ; words ; " " ; words

DIM moreWords AS STRING
moreWords="Let's be friends."
PRINT words + moreWords + words
SLEEP
'______________________________________________________________________________________________

'PRINTING STRINGS TOGETHER
CLS
PRINT "PRINTING STRINGS TOGETHER USING SEPERATE PRINT STATEMENTS"
PRINT "HELLO"
PRINT "HOW ARE"; 'note the semicolon. 
PRINT " YOU?"
PRINT "I";
PRINT " ";
PRINT "am";
PRINT " fine."
PRINT "Thank you"
SLEEP
'______________________________________________________________________________________________

'IF THEN conditional statements
CLS
PRINT "IF THEN CONDITIONAL STATEMENTS"
DIM test AS INTEGER
test=1
PRINT "the test variable is " ; test
IF test=1 THEN PRINT "The variable 'test' is 1, so this is printed."
IF test=2 THEN PRINT "'test' is still 1, but this will only print if it is 2."

test=2
PRINT "the test variable is " ; test
IF test=1 THEN PRINT "test is only one."
IF test=2 THEN PRINT "'test is a pair."

test=999
PRINT "the test variable is " ; test
IF test=0 THEN PRINT "test is nothing."
IF test<0 THEN PRINT "test is negative."
IF test>0 THEN PRINT "test is positive."
IF test>100 THEN PRINT "test is very large."

test=40
PRINT "the test variable is " ; test
IF test=0 THEN PRINT "test is nothing."
IF test<0 THEN PRINT "test is negative."
IF test>0 THEN PRINT "test is positive."
IF test>100 THEN PRINT "test is very large."

test=1
PRINT "the test variable is " ; test
IF test<50 then test=100
PRINT "the test variable is now " ; test
if test<50 then test=0
if test>=50 then PRINT "test is greater than or equal to 50"
PRINT "the test variable is now " ; test
IF test<>9 THEN PRINT "the test variable is not 9."
if test<=9 THEN PRINT "the test variable is less than or equal to 9."

DIM message AS STRING
test=10
PRINT "the test variable is " ; test
IF test<10 THEN message="You can count it with 2 hands."
IF test>10 THEN message="It's bigger then ten."
IF test=10 THEN message="It's exactly 10!"
PRINT message
SLEEP
'______________________________________________________________________________________________

'MULTIPLE COMMANDS ON 1 LINE
CLS
PRINT "MULTIPLE COMMANDS ON 1 LINE"
PRINT "test=";test
PRINT "This is one seperate command" : PRINT "This is another "; : test=5 : PRINT test
PRINT "test=";test
if test=5 then test=1: PRINT "test was 5.  Now it's";test : test=2
PRINT "test=";test
SLEEP
'______________________________________________________________________________________________

'MULTIPLE COMMANDS IN AN IF/THEN STATEMENT


if test=5 then test=1: PRINT "test was 5.  Now it's";test : test=2

'The above line is the same as the commands below

if test=5 then 
  test=1
  PRINT "test was 5.  Now it's";test
  test=2
end if

'______________________________________________________________________________________________
'IF THEN ELSE
CLS
PRINT "IF THEN ELSE"
test=1
if test=1 then PRINT "test=one": PRINT "Hello" ELSE PRINT "test is not one": PRINT "Oh well."
test=0

if test=1 then PRINT "test=one": PRINT "Hello" ELSE PRINT "Now test is not one": PRINT "Oh well."

'The above line is the same as the commands below

IF test=1 THEN
 PRINT "test=one" 
 PRINT "Hello" 
ELSE 
 PRINT "Now test is not one": 
 PRINT "Oh well."
END IF


SLEEP

'______________________________________________________________________________________________

'COUNTING
CLS
PRINT "COUNTING"
DIM C AS INTEGER
C=0 'note: Always set your counting variables to 0 before counting.
PRINT C
C=C+1
PRINT C
C=C+1
PRINT C
C=C+2
PRINT C
C=C+10
PRINT C
C=C-1000
PRINT C
SLEEP
'______________________________________________________________________________________________

'LOOPING
CLS
PRINT "TYPICAL COUNTING DO WHILE LOOP"
DIM rep AS INTEGER 'the variable name "rep" is short for repitition
rep=0

DO
 rep=rep+1
 PRINT rep; 'Note that FreeBasic automatically insertes a space after printing
                  'an integer variable.
LOOP WHILE rep<200

PRINT
PRINT "The Loop is exited, and the rep variable =";rep
SLEEP
'______________________________________________________________________________________________

CLS
PRINT "DO UNTIL LOOP"
C=0

DO
C=C+1
? "C =";C
Loop Until C=5

PRINT "The Loop is exited, and C =";C
SLEEP
'______________________________________________________________________________________________

DIM HELLO as INTEGER
HELLO=0
CLS
PRINT "DO LOOP, with EXIT CONDITION before PRINT 'Hello'" 'This prints "Hello" 4 times.
C=0

DO

C=C+1
? "C =";C
if C=5 THEN EXIT DO
?"Hello"
HELLO=HELLO+1

Loop

PRINT "The Loop is exited.  C =";C;" and there were";HELLO;" Hellos."
SLEEP
'______________________________________________________________________________________________

HELLO=0
CLS
PRINT "DO LOOP, with EXIT CONDITION after PRINT 'Hello'" 'This prints "Hello" 5 times.
C=0


DO

C=C+1
? "C =";C
?"Hello"
HELLO=HELLO+1
if C=5 THEN EXIT DO
Loop


PRINT "The Loop is exited.  C =";C;" and there were";HELLO;" Hellos."
SLEEP

'The above 2 examples demonstrate an important lesson to be aware of with loops.  Make sure that
'the loop ends at an appropriate place, and that all the necissary commands have been run before
'the loop is broken.  This type of error will not be pointed out to you, like a syntax error will,
'This type of error could be the cause of many lost hours.
'______________________________________________________________________________________________

CLS
PRINT "THE FOR NEXT LOOP, 1 to 100"
FOR X = 1 TO 100
 PRINT "Looping, x=";X;". ";
NEXT X
PRINT
PRINT "The loop has ended.  The final value of X is";X
SLEEP
'______________________________________________________________________________________________

CLS
PRINT "THE FOR NEXT LOOP, 1 to 100, with STEP 5"
FOR X = 1 TO 100 STEP 5
 PRINT "Looping, x=";X;". ";
NEXT X
PRINT
PRINT "The loop has ended.  The final value of X is";X
SLEEP
'______________________________________________________________________________________________

CLS
PRINT "THE FOR NEXT LOOP, 100 to 1, with STEP -2"
FOR X = 100 TO 1 step -2
 PRINT "Looping, x=";X;". ";
NEXT X
PRINT
PRINT "The loop has ended.  The final value of X is";X
SLEEP

'For the above program:
'One cannot assume that the value of X is equal to 1 after the loop has ended.
'However, one can assume that the value of X will be somewhere from 100 to 1 while the loop
'is running...  While the loop is running, the commands between the FOR and NEXT statement will
'be executed.  When the loop is finished running (ie: X is not between 100 to 1 {inclusively}), 
',the commands between FOR and NEXT are no longer run.  At that point, execution resumes with 
'the command that is after the NEXT statement.
'______________________________________________________________________________________________

CLS
PRINT "STRING ARRAYS"
DIM fullName(100) as string
fullName(1)="Bob Johnson"
fullName(2)="Billy Joel"
fullName(3)="Mac Stone"
fullName(4)="Jerry Jamison"
fullName(5)="Amanda Jones"
'......
FOR C = 1 to 5
 PRINT "Person #";C;"'s fullName is ";fullName(C);"."
next C
SLEEP
'______________________________________________________________________________________________

PRINT
PRINT "INTEGER ARRAY"
DIM age(100) as integer
age(1)=20
age(2)=32
age(3)=14
age(4)=100
age(5)=16
'......
FOR C = 1 to 5
 PRINT fullName(C);"'s age is";age(C)
next C
SLEEP
'______________________________________________________________________________________________

PRINT
PRINT "SORT BY AGE"
dim anAgeWasUpdatedWhileTesting as string
dim tempInt as integer
dim tempString as string
'do the sorting
do
anAgeWasUpdatedWhileTesting="false"
FOR C = 1 to 4
 if age(c)>age(c+1) then 

  'Switch ages for array locations C and C+1
  tempint=age(c)     'note: This technique of using a temporary variable (ie: tempint) to switch
  age(c)=age(c+1)    '      2 other variables [ie: age(c), and age(c+1)] around., is very  
  age(c+1)=tempint   '      important.

  'Now switch the fullName array's values at the same array position as were switched just before. 

  tempstring=fullName(c)      'A. note: tempString is simply a temporary place-holder.
  fullName(c)=fullName(c+1)   'B.
  fullName(c+1)=tempstring    'C. note: A, B,& C are all used in switching 2 variables around.

  anAgeWasUpdatedWhileTesting="true" 
 end if
next C
if anAgeWasUpdatedWhileTesting="false" then exit do
loop

'show the results
FOR C = 1 to 5
 PRINT fullName(C);"'s age is";age(C)
next C
SLEEP
'______________________________________________________________________________________________

CLS
PRINT
PRINT "SORT BY AGE, shows step by step changes"
dim showResultsC as integer 'show results counter

'reset back to original values
fullName(1)="Bob Johnson"
fullName(2)="Billy Joel"
fullName(3)="Mac Stone"
fullName(4)="Jerry Jamison"
fullName(5)="Amanda Jones"
age(1)=20
age(2)=32
age(3)=14
age(4)=100
age(5)=16

'show the results
PRINT "Original values:"
FOR C = 1 to 5
 PRINT "age() and fullName() array location:";C;" ";fullName(C);"'s age is";age(C)
next C

'do the sorting
PRINT
do
PRINT "New Loop starts:"
anAgeWasUpdatedWhileTesting="false"


FOR C = 1 to 4

 if age(c)>age(c+1) then 
  'Switch ages for array locations C and C+1
  tempint=age(c)
  age(c)=age(c+1)
  age(c+1)=tempint
  'Switch full names for the exact same array locations (ie: c)
  tempstring=fullName(c)
  fullName(c)=fullName(c+1)
  fullName(c+1)=tempstring
  anAgeWasUpdatedWhileTesting="true"
 end if

'show the results
FOR showResultsC = 1 to 5
 if showResultsC=C or showResultsC=C+1 then color 15,0 'These are the array variables being compared.
 PRINT "age() and fullName() array location:";showResultsC;" ";fullName(showResultsC);"'s age is";age(showResultsC)
 if showResultsC=C or showResultsC=C+1 then color 7,0 'These are the array variables being compared.
next showResultsC
PRINT
sleep

next C


if anAgeWasUpdatedWhileTesting="false" then PRINT "No changes were made, so exit the loop.  We are done." 
if anAgeWasUpdatedWhileTesting="false" then exit do 
PRINT "Loop ended.  "
sleep
loop

SLEEP
'______________________________________________________________________________________________

CLS
PRINT "SIMPLE TEXT ANIMATION"
dim direction as string
dim xold as integer
dim yold as integer
direction="upright"
x=7:y=3
FOR C=1 to 80

'set old coordinates before changing the new ones
xold=x
yold=y

'movement
if direction="downright" then x=x+1:y=y+1
if direction="downleft" then x=x-1:y=y+1
if direction="upleft" then x=x-1:y=y-1
if direction="upright" then x=x+1:y=y-1

'enforce boundaries
if y<=2 then direction="downright"':y=2
if x>=24 then direction="downleft"':x=24    'teach  ::: ifs
if y>=24 then direction="upleft"':y=24
if x<=2 then direction="upright"':x=2

sleep 50

locate y,x
print "O";
locate yold,xold
print " ";

NEXT C
'______________________________________________________________________________________________

CLS
PRINT "MORE COMPLEX TEXT ANIMATION"
'xArray(40) and yArray(40) is the leader.
'For all array psoitions, array(tempPos)=array(tempPos+1) starting from the 1st position to the last.
'Array(40) is not printed out, but it's the invisible leader that makes the first move.
'All boundry checks & movement manipulations happen to Array(40)
'Array(1) to array(39) is printed out, but not array(40) because array(39) will equal array(40).

SLEEP
CLS
dim xArray(40) as integer
dim yArray(40) as integer
dim temp as integer
direction="upright"

'initialize the arrays
for temp=1 to 40
 xArray(temp)=7
 yArray(temp)=2
next temp


FOR C=1 to 300 'The rainbow snake  will make 300 seperate movements.

'Update previous coordinates before updating the later ones
for temp=1 to 39
 yArray(temp)=yArray(temp+1)
 xArray(temp)=xArray(temp+1)
next temp

'movement
if direction="downright" then xArray(40)=xArray(40)+1:yArray(40)=yArray(40)+1
if direction="downleft" then xArray(40)=xArray(40)-1:yArray(40)=yArray(40)+1
if direction="upleft" then xArray(40)=xArray(40)-1:yArray(40)=yArray(40)-1
if direction="upright" then xArray(40)=xArray(40)+1:yArray(40)=yArray(40)-1

'enforce boundaries
if yArray(40)<=2 then direction="downright"':yArray(40)=2
if xArray(40)>=24 then direction="downleft"':xArray(40)=24 
if yArray(40)>=24 then direction="upleft"':yArray(40)=24
if xArray(40)<=2 then direction="upright"':xArray(40)=2

sleep 51-(C/300)*50.9 'note: When the SLEEP command has a number after it, it tells the program to
                      '        pause for that many miliseconds.  At first, it pauses for 50ms, but
for temp=1 to 39      '        it gradually decreases to a .1ms pause, which is very fast.
 locate yArray(temp),xArray(temp)
 color temp,0
 print "O";
next temp

locate yArray(1),xArray(1)
print " ";

NEXT C

'______________________________________________________________________________________________