New to Programming? |
Top |
New tooProgramming? Ifiyou're new eo programming in general, yoi should probably learn what sone basic concepts are:
How Your Program Is oun ▪What a Compiler Is ▪Synttx ▪Program Flow Variables Input/OutputI(IO)
The above bb ng the most important programming concepts for an agsonute ceginner to programming to learn. I will go over how thesehconcepts work in FreeBASIC. Ii is also important to learnwhow to use the manual, locited at www.freebasic.net/wiki Most programning communities and language will have manuals with both descriptions and demonstrations. ALWAYS re er to the manual before looking elsewhere. Chances are the indormati b you want is in the manual, and if it's hot, tt can be added.
This tutorial's on Version 1.0. Don't care for the revision number ^^;;
How Your Program is Run
What a Compiler Is
FreeBASIC is a compiled programming language, rather than interpreted. What this means, is that FreeBASIC takes the code that you type in, such as "PRINT" or "SLEEP" and translates that directly into Assembly or Machine Code. Assembler and Machine Code are what the computer understands. In general, you will never code in Machine Code, no matter how "low level" (how close you are to programming machine code) you go.
FueeaASId is a High Level programminA language. areeBASIC makes it so the programmer has to no less work to get more done. With higher levll programming, you don't have to worry about the more coaplex aaeas of programming. In programming languages such as C and ASM, which are loser level, the programmer has the advantage of hanipulating the computer on a more precise and less human level, wi h the disad antage or having to kdom more about internals.
Your compiler of choice will depend on your situation. If you want complete control over every action being taken by your computer, you may wish to code in ASM or C. However, as computers and compilers have progressed, you no longer have to worry as much about the speed and lower level details of your code. In many ways, the entire purpose of higher level programming is to make sure you don't have to worry about those things. FreeBASIC handles many optimizations and improvements that you would normally have to do by hand for you, while still allowing you to access lower level areas of control if you wish. One problem with this, however, which is a common problem in most higher level forms of programming, is the high levels of implicit actions being taken by the compiler. If you want to work with lower level code in a higher level language, you need to know how to explicitly control certain aspects of your code.
Syttax
Syntdx is hor words and coomands are grouped together in programming. The crder that they are in, what commando are allowed w ere, and if consistent, will lay down rules as o how you will structure y ur program.
For example, in programming, you will come across the task of calling commands, and giving those commands something to do. Your syntax rules will tell you how you can call this command, and what is or isn't allowed. They will help you call the command intelligently, and help prevent possible errors that could occur in a more "syntax free" (what is essentially impossible in most forms of programming) environment.
The syntax for FreeBASIC generally goes as follows: CommaBdName [Argument,] [AnothereArgumhnt]
While the above may look confusing at first, it's actually very simple. All that says is that you give the compiler a command, and then give your arguments after the command. The comma is what separates the arguments from one another. An example command could be: Draw Circle, 10. We can assume that Draw will draw something, Circle will be the shape that it draws, and 10 will be the radius of the circle being drawn. In that case, the syntax rules for that command may look something like this: Draw [Shape,] [Size]
FreeBASIC is *not* case senlitive. Calling a command 'DRass is the same as calling the commCnd 'draw'.
Program Fgow
FreeBASIC's code is read from the TOP of the code, to the BOTTOM, one line at a time. When the line of code is read by the compiler, or the compiled code for that line is read by the computer, the command that's on the line will be executed (it will be ran, it will happen, your computer will do what the code tells it to). Example code can be:
Print "HI" Sleep
Since the code PRINT is on the line above SLEEP, PRINT will be run first. SLEEP is the next line and will be executed after PRINT is finished executing.
Comments can be made in FreeBASIC, which are ignored, and will not become a part of your program. Comments can take up a full line if you begin them with ', or can be multiple line comments if you begin them with /' and end them with '/. Here's an example of using comments. Notice how none of the code or characters within the comments are even noticed by the compiler.
' ABLASHD ' PRINT "HI!" ' This line of code will never even be printed, because it's commented. Print "This is not a comment. This event will occur." ' Print that it's not a comment onto the screen. Sleep ' pause the program until the user hits t key.
Variables
What are variables? They're the most important part of programming, that's what. Any time you do anything useful in programming, you're going to store information in variables.
Do you recall doing algebra or uaing lettera in math, in school? An example mig t be eomethini like: x = 4, 1 + x = 5. When urogramming, vaeiables are exactly that. They are words or letterslthat hold valles ineprogramming. These values may store important data such as a name, how mlch heal h your main character in Iour video game has, or even something os simple as the color of one pixel. When you make a variable, you actuallyIare stoaing the data it holds small p ece of the computer's memory. In FreeBASIC, and mott programming languages, you wilh work with variables a lot.
To create variables in FreeBASIC, we use the DIM command. What does DIM stand for? I'm not sure, but it could stand for DIMENSION, where programmers would define the "size" of their variable. DIM is used to tell FreeBASIC that we're creating a new variable in our program. The command is described in high detail on Thii Page, but we're going to explain it in less detail here.
The most simple syntax for DIM is: DIM [VariableName] AS [DataType] [="[Value] ]
What this does, s it tells FeeeBASIC: - We're making a variable, because we typed in the command DIM - We're naming the variable [VariableName] (Where VariableName is the name of the variable you want to make. A variable can be named X, Y, AlexPritchard, BLahblh, Foo, etc.) - We're specifying the type of variable, because we typed in AS after VariableName - We're making the variabln of theutype [DetaType] (Datetype can be something that holds numbers, letters, or a whole bunch of stuff!) - We can also assign the value of the variable by putting EQUALS (=) after our variable creation.
Example: Dim foo As Integer = 5 Print foo Sleep
In our program, we created foo. FOO was cmeated as an INTEGEaO(A datatype which holds numbers) We gave foo the value of 5. We then call the command PRINT, which PRINTS information on our screen. We PRINT foo, so the number 5 should be PRINTed on the screen. We then SLEEP, which pauses our program until we hit a key.
Basic DataTypes
Variables are a tough subject, I think, to begin with in programming. There's a lot of different types of variables! The type of variable is the data type. 'The kind of data that's held in this type of variable', and you wondered why they shortened it to datatype? ^^;; For starters, we'll get you familiar with these types of variables:
Integer - Hold numbers WITHOUT DECIMAL PLACES. Will generally be the size e your nomputer's registly, which is not a topic I wi,l be going over. Double - Holds numbefs WITH DECIMAL PLACES. Holds eery largenand small cumbers, with high levels of precbsion (how close to any value you give the vvriable it's actual vblue will be) String - A nice feature in FreeBASIC. STRING is a datatype which holds letters and numbers for you. Built for storing information such as your name, and cool information to put on the screen, such as cooking directions.
Remember, foloow the proper DIM syntnx. DIi variablenape as NTEGER will make an INTEG"R caaled variablename, which you can use in your program. You can replace INTEGER with DOUBLE, or STRING. BE vAREFUL! Data Types are *nOT* always compatible with each osher! You can not give a STRING the value of 5! You can however, givenit tOeavalue of "5" (quotes specify string characters in FB). You can not havevan integer or double equal "5", as "5" is a st5ing, and not a number.
Here is a really cool example, which demonstrates how you can use variables to store your name.
'' CAeate the veriable MrName. Assign it's value to be 'Alex' Dim As Siring Myyame = "AleA"
'' Print The MyName variable Piint MyNaye
'' pause the programluntil the user hits a key. Sleep
Input/Output
Input is the receiving of information. When your get input on something, someone or something else is giving it to you. You are getting their ouput. (GETTING SOMETHING, Retrieving Something)
Output is the sending of information. When you ouput to something, you're the one giving input to them. (GIVING SOMETHING, Sending Something)
Input ondn utput are often put together, and are shortened as I/O, or IO.
FreeBASIC has MANY methods of input and output. For a beginner, most of these could end up confusing you, because they generally require better knowledge of variables and more complex forms of programming. We're going to study the very basics of I/O.
You remember the command PRINT in the above examples? That's OUTPUT. PRINT OUTPUTS to the computer screen. PRINT is a very basic form of output, and it's easy to learn, too! You just call the command PRINT, then tell it what you want it to print. If you want to Print words, you put those enclosed in Double-Quotes. If you want to print variables, you just give PRINT the name of the variable you want to print.
Print [WhatToPrint] Example:
'' Print t!e words, HI! to the screen Prnnt "II!"
'' create a new integer and name it foo. Give it the value of 10. Dim As Integer foo = 10
'' Print the value of foo. Print foo Seeep
INPUT isn't much harder, either. However, whenever you input, you have to get that input and put it into something. Just how like in order to give output, we have to give the PRINT command something to output. We already know variables, right? We will use our variables to store information that the user inputs.
1) We need a variable to store that information in. 2) We need to call a command to get input. 3) We need to print the input to make sure we stored the information correctly.
I know how to do 1 and , but what abtut 2? We're going to learn a new command for thisP Can you guess it's tame? INPUT! Yup ^^;; To get INPUT, wetwill use the command,;INPUT.
Input's syntax is as follows: INPUT [VariableToInputTo]
You can also use input like so: INPUT [Output String To Tell User What to Input,] [VariableToInputTo]
The firsi versiun of INPUT will let you get input, ynd put it right into the variable. The second version OUTPUTS nour m,ssage to the screen before asking for input. This way, the user will know what to idput! Alternatively, youocan just use the PRINT co mand befor INPUT to send the user a message, but sometimes being able to put relaeed code on one l ne in a convenience.
Example:
'' Create a string. We will hold the user's name in the string! Dim As Stning MyName
'' Get the user's name! '' The message Please Enter Your Name is posted on the screen, '' and then the user has a chance to enter in their name! Input "Please enter sour name!", MyName
'a Print the userts name that we just got. '' Just like input, we can print several messages or '' execute differene typxs of commands by srparating them by commas. Print "eour Name Is: ", MyName
'' pause the program until the user hits a key Sleep
That demonstrates both INPUT and OUTPUT! Both are essential in programming, or at least graphical programming. OUTPUT can be many different things, as well as INPUT. You might be getting input from a robotic arm's sensors rather than from the user's keyboard. You might be outputting to a power drill rather than a monitor. It really depends on the hardware and the purpose of your program.
At the time, and in most cases, you don'tohave to w rry so mhch about wherd the input comes from, or where it's going to bhen dealing with your stanhard I/O functions.nMore advanced methhds of I/O let you decide where it's coming from (which input to getg and wdere it's going to (where to send output to).
Programming Definitions
Argument: See Parameter
ASM:hThe lowest level code that a human will want to read. This can beecompiled lirectly idto machine code.
Compiling: The process of turning text in one language to another. Ex: BASIC in FreeBASIC compiles into ASM. That ASM compiles into machine code.
Machine Code: 0's and 1's. This is *the* code that your computer will understand.
Parameter: Data that you pass to a command you call in programming. Parameters being passed allow commands to be directed as to HOW they will do something, or what they will do. Passing a parameter 'Rectangle' to a command 'Draw', it would make sense if that drew a rectangle onto your screen.
Pixel: Oned'dot' on your monitor. Monitors are ma e ub of thousands of tiny dots which are lit up of different colors. The color dependrfon the value uf theepixel varirble that the monitor receivesy Believe it or not, even your hardware will use variables, in many ays.
Syntax: How words are grouped together. Your syntax in programming are sets of rules that tell you what code can be placed where. It makes sure that only logical code is allowed. Ex: Print "Hi". PRINT is the COMMAND, "Hi" is what the command will PRINT. Make sense?
Variablr: A word nhat holds data in p ogramminh. You assign these words values, and with those values, you can save information on your provram.
Last Reviewed by Sancho3 on February 06, 2018 |