Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

QBASIC

PROGRAM
INTRODUCTION
CHAPTER 1: BASIC DEFINITION OF QBASIC

 The abbreviation for Qbasic is Quick Beginners All Purpose


Symbolic Instruction Code.

 Microsoft created and released it in 1991, and it is regarded as one


of the best languages for complete beginners.

 It is a structured high level programming language that includes


features like subroutines and procedures.
Conti.
 It is also a straightforward and simple language to learn and has a lot of
benefits for newbies.

 It was developed to be a substitute for the GW-BASIC programming


language.

 We can easily develop software and games, create enterprise, corporate and
business applications and software with databases and good visuals using the
Qbasic programming language.
 It is even used in big banking and financial systems to design some of the
modules.

 Qbasic is a Dos-based programming language that is no longer in use and is now


only used in education and as a beginner’s programming language.

 But once you learn Qbasic, it will be easy for you to learn other programming
languages like, java, python, c++, JavaScript etc.
 We have two of the most popular Qbasic-compatible compliers for modern PCs
for windows.

 They are free basic and QB64, both of which are compatible with the most
recent Windows versions.

 To get started with Qbasic, go to qb64.org and download the stable version of
qb64, which also offers a support community

 It is compatible with the most recent versions of Windows, Linux, and mac OS.
FEATURES OF QBASIC

1. Simple and easy to learn the language:


 One term for it is “people’s language” because it is simple and easy to learn.

 Qbasic’s syntax is simple for beginners to understand, and errors are simple to
spot.

 Additionally, because it is similar to English, it can be used wherever in the world.


2. Applicable for use in Business purposes and Mathematical learnings:
 Qbasic is a great language for business purposes as it can make school
administration, hospital management, and library management systems.

 By merging small modules, we can create large applications that are then
integrated into the main system

 The fact that QBASIC is easy to program and considerably simpler to port
between apps to one of its key advantages.
3. Fewer requirements:
 Qbasic is a straightforward language that uses lower resources and has fewer
system requirements compared to other programming languages.

 It is also a straightforward and simple language to learn and has many benefits for
newbies.
4. Free to use:
 As Qbasic is open source software, it can be used free. It is a beginner friendly
programming language.

 5. It checks syntax automatically


INSTALL QB64 ON YOUR PC
 You can download Qbasic if you a 32 bit as it was initially made for that version
computer.

 But you can download qb64 from their official website to run Qbasic on our
computer

 First, open your browser on your pc and search qb64 download as shown below.
 After you search for “qb64 download”, new interface will appear on your
screen as shown below.
 Open the official website of qb64 to download a legitimate and
trusted Qbasic interpreter.

 You can see the list to download the qb64 go with the latest stable
version of qb64.
 You will be redirected to the download link for qb64 and download according
to your operating system.
 Once download is completed, you can run qb64 on your pc.

 If you face any problem while installing qb64 on your pc, you can ask questions
in forum.

 Go to the forum section of this app and ask your question to the community.
Run Qbasic Programs

 To start QBasic, first you should have QBasic installed on your PC.

 In our case we had just downloaded qb64 program, so you don’t have to worry
about qbasic.

 Click on the folder where you extracted the downloaded zip file

 And finally click on .exe file or the application file as shown in figure below
 Then the Qbaic development window appears
 Press the ESC key to clear the welcome message
 If you don’t want to install a built-in compiler on your computer or laptop.

 You may always use online compliers from websites like tutorials point,
replicat.com etc

 But for this, you need to have a good internet connection.


Writing and Running a New Program:
 After opening QBasic window type the following in the QBasic interpreter:
 PRINT “Hello World!”
 Now, to run the program, press F5 key from your keyboard

 Press any key to return to the main screen


VARIABLE NUMERICAL VARIABLE
 A variable is a name of storage location in the  The variable which is used to store only numeric
computer’s memory, in which the user can store data is called a numeric variable.
different values temporarily.
 That means to store a numeric data, users need to
 A variable stores these values which are changeable have a numeric variable in the program,
during the program execution time.
 Example; Here x and y are numeric and they store
 Variables can hold letters and numbers, As values 55 and 7.53 respectively.
QBASIC uses two types of data, two types of
 CLS
variables are needed to store different data.
 The two types of variables are:  X = 55

 Numerical variable  Y = 7.53

 String variable  Sum = x + y


 PRINT “sum”
 END
 STRING VARIABLE RULES FOR VARIABLES

 The variable which is used to store only string  Some rules while giving variable
data is called the string variable. names are as follows;
 A string variable can store numbers as well as  The first character of a variable must
characters. The string data should be closed by
double quotation marks(“………”). begin with alphabets and the
remaining letters can be letters,
 The string variable is represented by an alphabet
followed by a dollar sign($).
numbers or decimal points.
 Here names$ and address$ are string variables.
 Variables can be up to 40 characters
They store Ram and Kathmandu respectively. in length
 CLS  There cannot be space between the
 Names$ = “Ram” variable name.
 Address$ = “Kathmandu”  String variable names must end with
 PRINT “Name”; Name$ a dollar sign($)
 PRINT “Address”; Address$  QBASIC keywords or reserved
 END
words cannot be used as variable
names such as cls, input, print.
 Some of the valid string variables in qbasic names
 X, y, role_number, grade, salary, age, Num1, etc.
are:
 Some invalid numeric variables in qbasic names
 X$, y$, Names$, address$, sec1$, and so on.
are:
 Some of the invalid string variables in qbasic
 1x, mark sheet, name$, cls etc.
names are:
 X, z, 3age$, name$, class 5$, cls$, input$, and so
on.
 Some o valid numeric variables in qbasic names
are:
CONSTANTS
NUMERIC CONSTANTS
 The constants are used to store the values which
 A numeric constant is a constant which is used
are not changeable during the program execution
to store only numeric data.
time.
 CLS
 That means we have to declare constants to those
values which should not be changed by the  Const pi = 22/7
program while it is running normally.
 Const g = 9.8
 Constants is similar to variables but the values
 PRINT “pi”
can’t be changed or altered. In QBASIC, constants
are of two types:  PRINT “g”
 Numeric constants  END
 String constants  Example; here, pi and g are numeric constants
and they store values 22/7 and 9.8
respectively.
 Note: the constants are declared by using the  CLS
keyword const. the constants declared before the
 Const Names$ = “Ram”
program starts.
 Const Address$ = “Kathmandu”
 PRINT “Name”; Name$
STRING CONSTANTS
 PRINT “Address”; Address$
 the constants used to store only string data is called
the string constant.  END
 A string constant can store numbers as well as  Here names$ and address$ are string
characters. constants. They store values Ram and
 The string data should be closed by double Kathmandu respectively.
quotation marks(“………”).  Note: CONST keyword is used for declaring
constant
CHARACTER SET  For example:* means multiplication, and it is used
to multiply two operands
 Character set refers to the set of characters that are
allowed to be used in QBASIC.  The characters used in QBASIC are:
 Alphabets (both uppercase and lowercase), numbers  Alphabets: uppercase (A, B, C, D…….X, Y, Z)
(09), and special characters are all included in and lowercase(a, b, c, d,…..x, y, z)
QBASIC character set.
 Numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 The special characters have their own meaning and
 Special characters: +, -, *, /,=, <, >, (, ), {, },
function.
[, ], ;, :, “, ‘, \@, #, $ %, ^, &, _, |, and so on
 QBASIC keywords and variables are formed by
using the characters defined in the QBASIC
character set.
OPERATORS ARITHMETIC OPERATORS
 A symbol that is used to perform an operation  Arithmetic operator refers to the operator which is
between two operands is called operator. used to perform mathematical calculations such as
addition, subtraction, multiplication etc.
 An operator symbolizes what kind of operation has
to be performed on the data.  Arithmetic operator/ please connect to internet
 In QBASIC, four types of operators are used, which
are as follows:
 Arithmetic operators
 Relational operators
 Logical operators
 String operators
RELATIONAL OPERATOR
 Relational operator refers to the operator which are
used to compare two values. These operators are
used to show relation.
 The relational operator is also known as comparison
operator.
 The result of the relational operator is always true
or false
 Relational operator/ please connect to the internet

LOGICAL OPERATOR
 Logical operator refers to the operator which is
used to perform logical operations such as AND,
OR, NOT etc.
 It is used to combine two or more relational
expressions.
STRING OPERATOR
 It gives a single value, that would be either true or • String operator refers to the
false. operator which is used to perform
 Logical operators are specially used for making string operations such as
decisions. concatenation etc.
• The string operator is used to
combine two or more strings.
KEYWORDS  The keywords can also be called Reserved Words.
 Keywords are the reserved words that instruct the  The QBASIC keywords can be classified into the
computer to perform certain actions. following four categories. They are:
 Every programming language has its own keywords.
 In the same way, QBASIC also has its own  Assignment Keywords
keywords.
 Input/output Keywords
 Some of the examples of QBASIC keywords are
 Control flow keywords
INPUT, CLS, PRINT, etc
  Declaration keywords

 Note: The user should remember that keywords


cannot be used as variables names.
TYPES OF KEYWORDS Declaration keywords:
Assignment keywords:  These keywords are used to define and declare the
variables, constants etc.
 These keywords are used to assign a value to a
related variable  Example: DIM, REM, CONST, DECLARE,
STATIC, COMMON, INT, etc.
 Example:
 LET,
Control keywords:
 SWAP etc.
 These keywords are used to change the flow of
execution of statements.
Input / Output keywords:  Examples:
 These keywords are used to input variables for
 IF, THEN, ELSE, END IF
processing and output the result on the screen, as
their names implies  FOR, NEXT, DO LOOP,
 Examples:  SELECT CASE, GOTO etc
 CLS,
 INPUT, LINE INPUT,
 PRINT, LPRINT etc
SOME KEYWORDS USED IN QBASIC  The comments are not executed by the computer. It
helps to make the source code simpler for humans
CLS(clear screen): to understand
 This keyword is used to clear the screen and display
 Syntax: REM
the output from the top of the screen.
 Example:
 Syntax: CLS
 REM is a comment ‘semicolon can also be used as
 Example:
a comment.
 CLS
 PRINT “hello world”
 PRINT “Hello world”
 END
 END

PRINT:
 REM:
 This keyword is used to display the output on the
 The REM is used to add comment anywhere in the screen.
program.  We can also use question marks(?) instead of the
print statement.
 Syntax: PRINT “string”
 Example:  Example:
 PRINT “hello world”  CLS
 END  INPUT “enter your name”, name$
 Example:  PRINT “hello” + name$
 PRINT 234  END
 END
 LET:
 INPUT:  This keyword is used to assign a value to a variable.
The LET statement allows us to assign any value to
 This keyword is used to input the value from the user
a variable
and store it in a variable when the program is
running.  Syntax: LET
 Syntax: INPUT “string”, variable
 Example: END:
 CLS  It is used to terminate the execution of a program.
The END statement is typically used at the end of
 LET a = 10 the program to stop further execution.
 PRINT a
 Syntax: END
 END

 Example:
 Example:
 CLS
 LET name$ = “Dinga vinga”
 PRINT “I am learning Qbasic programming
 LET installs = 45000 language”
 LET users= 10000  END

 PRINT name$, installs, users


 END
Line input used in file handling:
 LINE INPUT:
 It is used to input a line of data at a time and after  Example:
entering it acts as the string.
 CLS
 It is commonly used to read a line from a sequential
 OPEN “file.txt” FOR INPUT AS #1
file into a string or variant variable.
 LINE INPUT #1, name$
 PRINT “hello” + name$
 Example:
 CLOSE #1
 CLS
 END
 LINE INPUT “enter your name”;
 Name$
 SWAP:
 PRINT “hello” + name$
 SWAP keyword is used to exchange the value of
 END
two variables having the same data types.
 Syntax: SWAP variable1, variable2
 Note: variables must be of the same type.  There are three types of selection statements in
qbasic:
 Example:
 If statement
 CLS
 If else statement
 LET a = 10
 If Else lf Else statement
 LET b = 20
 SWAP a, b
If statement:
 PRINT a, b
 The if statement is used to execute to a block
 END
of code if a condition is true.
 The IF statement always asks a question
SELECTION STATEMENT (usually about the number in a variable.)
 A statement is a collection of instructions written in
QBASIC using keywords and commands.
 If the answer is yes, the program will execute the  Example:
code in the IF statement.
 If a>10 then
 If the answer is no, the program will skip the code in
 Print a
the IF statement.
 End if
 In both cases, execution continues with the if
statement and ends with the END IF statement.
 The syntax of the if statement is as follows:

 If else statement
 If condition then  The if else statement is used to execute a block of
 Statements code if a condition is true and another block of code
if the condition is false.
 End if
 If the Boolean expression is false, the if statement
might be followed by an optional else statement.
 The syntax of the if else statement is as follows:
 If condition then If Else if Else statement
 Statements  The if else if else statement is used to execute a
block of code if a condition is true and another
 Else
block of code if the condition is false.
 Statements
 First the program checks the first expression of the
 End if program and if the requirement is false, it sends you
back to the if else condition.
 If the Boolean expression is false, the if statement
 Example:
might be followed by else statement.
 If a>10 then  In simple words, it will simply check the first
 Print a expression and if the first expression is an error it
moves to the second.
 Else
 Print b
 End if
 And finally, need to end the statement using end if.  example:
 The syntax of the if else if else statement is as  if a>10then
follows;
 print a
 If condition then
 else if a<10 then
 Statements
 print b
 Else if condition then
 else
 statements
 print c
 else
 end if

 statements
 end if
SELECT CASE STATEMENT  Select case expression
 The test expression is evaluated by SELECT CASE,  Case 1stexpression
which then performs the first matching CASE or
 Statement A
CASE ELSE block and leaves.
 Case 2ndexpression
 The SELECT CASE statement is used to select one
of many blocks of code to be executed.  Statement B
 SELECT EVERY CASE allows all matching CASE  Case Else
blocks or the CASE ELSE block to be executed in
 Default expression
order from top to bottom.
 Example;

 The syntax of the select case statement is as follows;  Select case a


 Case 1
 Print a
 Case 2
 Print b
 Case 3  A loop is a series of instructions that are repeated
until the desired result is obtained.
 Print c
 The QBasic programming language supports the
 Case else
following sorts of loops to fulfill looping needs:
 Print d
 For…Next loop
 End select
 While…Wend loop
 Do while loop

LOOP
 A loop is a set of instructions that is repeated until a
certain result is achieved.
 Loops are used to run a group of instructions
repeatedly until a certain condition is satisfied.
FOR NEXT LOOP  next variable
 The for next loop is used to execute a block of code  example;
for a specific number of times.
 for I = 1 to 10 step 1
 When you know precisely how many times you
 print i
need to run the loop, its usually utilized as a counter
loop.  next i
 The syntax of the for next loop is as follows;  this will print the numbers from 1 to 10
 For variable = start to end step
 statements

WHILE WEND LOOP


 When a condition is true, the WHILE statement
executes a block of statements continually.
 If the condition is true, the WHILE statement DO WHILE LOOP
executes the body of the loop, and the loop ends
 The do while loop is used to execute a block of
when the conditions is false.
code repeatedly until a condition is satisfied.
 The syntax of the while wend loop is as follows;
 A do-while loop is a control flow statement that
 While condition executes a block of code at least once before
continuing or interrupting it based on a Boolean
 Statement
condition at the block’s end.
 Wend
 Example:
 The syntax of the do while loop is as follows;
 While a<10
 Do
 Print a
 Statements
 a=a+1
 While condition
 wend
 this will print the numbers from 1 to 10
Example;  Functions in QBASIC are readymade programs that
take some data, manipulate them and return the
 do
value, which may be a string or numeric type.
 print a  Programming languages also have certain functions
 a=a+1 like spreadsheets or database software.
 while a<10
 this will print the numbers from 1 to 10
TYPES OF FUNCTIONS
 QBASC supports two types of functions;

FUNCTIONS  USER-DEFINED FUNCTIONS

 A function is a block of code that performs a  BUILT-IN FUNCTION


specific task.
 A function is a built-in formula to accomplish a
certain task such as mathematical, statistical,
financial, logic/data calculations, etc.
USER DEFINED FUNCTIONS BUILD-IN OR LIBRARY FUNCTIONS
 A function that is written and stored by the  The functions which are provided by the QBASIC
programmer to perform a specific task is known as a system and also allow the programmer to use them
User Defined Functions. according to the requirement are known as BUILT-
IN or Library Functions.
 When QBASIC does not provide a built-in function
to solve any particular problem required by the  These functions are required to be called by the
programmer in that case only programmers define programmer to use them in a program.
this type of function
 Some common Build-IN or library functions are
LEN, LEFT$, RIGHT$, MID$, UCASE$, LCASE$,
CHR$... etc.
 FUNCTION……END FUNCTION
statement can be used to define the function.
 MATHEMATICAL FUNCTIOS  COS, SIN, and TAN function: these functions are
used to return the cosine, sine, and tangents of an
 The numerical data is processed using mathematical
angle.
functions. The following are some of the
mathematical functions used in QBASIC;  Syntax: COS(angle), SIN(angle), TAN(angle)
 SQR function: this function returns the square root  INT Function: this function is used to return the
of a number. arctangent of a number
 Syntax: SQR(number)  Syntax: INT(number)
 ABS function: this function is used to return the  CINT Function: the CINT function gives results by
absolute value of a number or we can convert rounding up the fractional portion. It supports only -
negative numbers into positives 32768 to +32767
 Syntax: ABS(number)  Syntax: CINT(number)
STRING FUNCTIONS  RIGHT$ function: this is used to return the
rightmost character of a string
 The string data is processed using string functions.
The following are some of the string functions used  Syntax: RIGHT$(string, number)
in QBASIC;
 MID$ Function: this function is used to return the
 LEN function: this is used to return the length of a middle characters of a string
string
 Syntax: MID$(string, start, number)
 Syntax: LEN(string)
 UCASE$ function: this is used to return the string in
 LEFT$ Function: this function is used to return the uppercase
leftmost characters of a string
 Syntax: UCASE$(string)
 Syntax: LEFT$(string, number)
 LCASE$ Function: this function is used to return
the string in the lowercase
 Syntax: LCASE$(string, number).
 MODULAR PROGRAMMING  A program is made up of one or more independently
built modules. It can be applied to a wide range of
 Modular programming separates a computer
applications and tasks.
program into autonomous, interchangeable,
reusable, and separable sub-programs that can  An enterprise-level software application may
perform a range of functions. contain several different modules, and each module
serves unique and separate business operations.
 Modular programming consists of different
modules.  Modules make a programmer’s job easier by
allowing them to concentrate on only one aspect of
 A discrete software component or element of a
the software application’s functionality.
program that comprises one or more procedures is
known as a module.
 Instead of module, we use the term assembly (as  It makes programs to reuse methods or functions in
in.NET languages like C#, F#, or visual Basic.NET) a software, reducing the need to create code over
and over again.
 In similar fashion, we use the term package in
object-oriented programming languages like java,  It makes the program more flexible and easier to
Go, DartC++, or C# modify.
 It makes the program more efficient and easier to
debug.
ADVANTAGES OF MODULAR
PROGRAMMING  It also enables developers to work on various
projects at once, cutting down on development time.
 It is easy to code and test the program, and the
It also involves the writing of very little code
algorithms are simple and easy to understand.
 Modular programming simplifies and improves the
maintainability of programs.
 Because the program is divided into different ARRAYS
modules, debugging becomes easier and faster, it
 QBASIC supports a lot of programming concepts,
also makes it easier to detect mistakes.
including arrays
 It’s easier to test, implement, and develop distinct
 An array in QBASIC is a collection of variables
modules. Individual modules can then be combined
to create the entire software. that are stored together under a single name.
 Arrays are useful for organizing and manipulating
 In conclusion, modular code is well-organized code
that is easier to understand. It also makes it easier to large amounts of data, and they can be accessed
find and fix bugs. and modified using simple syntax.
 Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for
each value.
 These values can be accessed and manipulated FILE HANDLING
through their indices, which are integer numbers
 File handling is a very important part of any
that represent the position of each value in the
array. programming language.
 It is used to store the data permanently in a file. It is
 Arrays are useful for storing large amounts of data,
such as a list of names, numbers or other values. used to perform various operations on files like
updating, deleting etc.
 Arrays are also useful for storing data that is related
 This typically involves reading and writing files to
to each other, such as a list of names and their
corresponding ages. a storage medium such as hard drive or flash drive,
but it can also involve networked storage devices or
 Arrays are often used in QBASIC to organize and other types of data storage.
manage data in a more efficient and convenient
way.
 File handling is an important aspect of computer  OPEN pathname FOR mode [ AS file number]
programming, as it allows programs to store and
 The pathname is the name and location of the file
retrieve data that can be used at a later time.
that you want to open, and the mode specifies how
 In QBASIC, you can use the OPEN and CLOSE you want to open the file ( e.g. for reading, writing,
statements to work with files. or appending).
 To open a file for reading or writing, use the OPEN  The file number is an optional parameter that you
statement followed by the file name, and then can use to specify a number you can use to refer to
specify the mode in which you want to open the the file in other statements.
file.
 If you do not specify a file number, QBASIC will
 The OPEN statement has the following syntax: assign a number to the file automatically.
 The following example opens a file named  The INPUT command reads a single line of data
“test.txt” for reading: from the file.
 OPEN “test.txt” FOR INPUT AS 1  The LINE INPUT command reads a single line of
data from the file and stores it in a variable.
 The following example opens a file named
“test.txt” for writing:  The INPUT command has the following syntax:
 OPEN “test.txt” FOR OUTPUT AS 1  INPUT #file number, variable
 The following example opens a file named  Example: INPUT #1, x
“test.txt” for appending:
 The following example reads a line from a file and
 OPEN “test.txt” FOR APPEND AS 1 stores it in the variable “line”.

READ AND WRITE A FILE


 Once the file is open, you can use the INPUT and
LINE INPUT commands to read data from the file.
 INPUT #1, line COLSE FILE
 The following example writes the contents of the  To close a file, use the CLOSE statement followed
variable “line” to a file: by the file number of the file that you want to
close.
 PRINT #1, Line
 The CLOSE statement has the following syntax;
 The LINE INPUT command has the following
syntax:  CLOSE file number
 LINE INPUT #file number, variable  The file number is the number of the file that you
want close.
 Example: INPUT #1, x
 The following example closes the file that was
 The following example reads a line from a file and
opened in the previous example;
stores it in the variable “line”.
 CLOSE 1
 LINE INPUT #1, line
 FILE HANDLING EXAMPLE  ‘write a new line to the file
 Here is an example of how to use the OPEN,  PRINT #1, “new line in file”
INPIUT, PRINT and CLOSE statements to read
 ‘close the file
from and write to a file in QBASIC:
 CLOSE #1
 ‘open the file for reading
 OPEN “file.txt” FOR INPUT AS #1
 ‘read first line and print it
 INPUT #1, text PRINT text
 open the file for writing
 OPEN “file.txt” FOR OUTPUT AS #1

You might also like