Jump to content

BASIC/Introduction

From Wikiversity
Completion status: this resource has reached a high level of completion.

BASIC, which is an acronym for Beginner's All-purpose Symbolic Instruction Code, high level language that stresses ease of use. It was developed in 1964 by John George Kemeny and Thomas Eugene Kurtz with a goal to create a programming language that anyone could use. Although BASIC is no longer widely used as it once was, it is still a popular programming language used on personal computers and microcontrollers.

FreeBASIC is an open-source, free BASIC programming language compiler that is compatible with Microsoft QuickBASIC. For FreeBASIC IDE, student can download FBIDE.

BASIC source code has .bas extension, e.g main.bas

Hello, World !

[edit | edit source]

Let's start with "Hello, World !", the very first BASIC program.

hello.bas

' This program displays "Hello world!"
'
' References:
'     http://www6.uniovi.es/qbasic/qtutor1.html

PRINT "Hello world!"

Variables

[edit | edit source]

Example 1: Convert a Fahrenheit temperature to Celsius

DIM Fahrenheit
DIM Celsius

PRINT "Enter Fahrenheit temperature:"
INPUT Fahrenheit

Celsius = (Fahrenheit - 32) * 5 / 9

PRINT STR$(Fahrenheit) + "° Fahrenheit is " + STR$(Celsius) + "° Celsius"

Example 2: Addition and subtraction of (random) integral numbers

' Program to create examples for addition and subtraction of integral numbers for output on printers
' source code for FreeBASIC; tested with PDFCreator

#lang "fblite"
dim as integer max,a,b,n,i

print
input "Maximum absolute value of the numbers [default: 100]: ";max
if max<2 then              
    max=100      
end if
print
input "How many examples [default: 25] ";n
if n<=0 then 
  n=25
end if
Lprint         ' output on the printer                
Lprint           
Lprint
Lprint "        ";n;" arithmetical exercises with integral numbers from ";-max;" to";max  
Lprint
Lprint

randomize      'initialize the rnd random-number generator
for i=1 to n 
    a=int(rnd*(2*max+1))-max
    b=int(rnd*(2*max+1))-max 
    Lprint using "       ###)   ";i;
    Lprint a;
    if b<=0 then                
       Lprint " -";abs(b)           
    else
       Lprint " +";b
    end if
    Lprint
    Lprint
next i  
Lprint Chr(12)   ' for output on computer screen use the command "sleep"