Introduction to QBasic
Quick Beginners, All-Purpose Symbolic Instruction Code is a high-level programming language developed by Microsoft Corporation, the USA in 1985. It is modular programming, where programming is divided into different modules or procedures.
Q BASIC uses English like words and Mathematical symbols to write programs. Once you learn QBASIC programming it will be helpful to learn another high -level programming languages such as C, C++, Java, PHP, Dot-Net, etc.
QBasic, a short form of Quick Beginners All purpose Symbolic Instruction Code, is an integrated development environment and interpreter for a variety of BASIC programming languages which are based on QuickBASIC
Modes of Operation
The various ways in Q BASIC can be operated for obtaining the result:
- View Window Mode: – The (upper) layer part of the full screen.
- Immediate Window Mode: – The small window of the bottom of the editing window.
Q BASIC Statements and Its Purpose
CLS is used to Clear the display screen. Rem is used to writing remarks or explanation of the program.
Syntax: Rem Remark
Input is used to supply or input data from the keyboard.
Syntax: Input"prompt message";variable list
where prompt message tells the user what type of data is to be entered. Let is used to assign the value of an expression to a variable. It is an optional statement. Syntax: Let Variable=Expression
eg. Let A = S Let Name$ = 'Binod'
Print is used to display output on the screen in the desired alignment with some spacing in limited ways. It displays data, the value of variable or expression on the screen.
Syntax: Print (Expression) Separator (Expression)
eg. Print"My Name"; ABC Print"My Phone No"; XXXXXXXX Print (3^2+5)/2
Const is a non-executable statement that declares one or more symbolic constants.
Syntax: Const Symbolic Constant = Expression
CLS INPUT"ENTER THE RADIUS";R
CONST PIE = 22/7 A= PIE* R^2 END PRINT"AREA OF CIRCLE";A
The END is used to terminate the Q Basic Program. A statement is an instruction within a program. The four types of statements are;
- Declaration Statement
- Assignment Statement
- Control Statement
- Input/Output Statement
The command is the instructions to the computer to do something to a program as printing, saving etc.
Character Set is a set of characters which are valid in Q Basic. It consists of alphabet (A-Z, a-z) number ( 0-9) and special characters like: !,@,#,$,%,%,^,&,*,(,),?,>,<,+,=,',",.
Keywords are special words that have a special purpose and meaning in Q Basic. Some of the keywords are CLS, REM, PRINT. AND, OR, LEN, WHILE, etc.
Variables and Its Types
A variable is a symbolic unique name that occupies space in the computer memory for storing data temporarily. It can simply be said as a thing whose values keep on changing during the execution of a program. A value of a variable can be changed due to the execution of the programs.
1. String Variable:
It is a name or reference that stores alphanumeric characters. It contains dollar sign $, for example, R$, NI$, etc. Any types of declaration characters are not allowed in the variable name.
2. Numeric Variable
It is the name or reference which stores a positive or negative number. There is four types of Numeric Variable.
- Integer- percentage sign %
- long Integer- ampersand sign &
- Single precision- exclamation !
- Double precision- has tag #
Constants and Its Types:
Constants are the data of values in a program, that may be a letter, number or special characters. Its value doesn’t change during the execution of the program.
1. String Constant
An alphanumeric data enclosed in double quotes is called string constant. It may be a word, number, blank space, special character, etc. for example: “Dhading”.”Mahendranagar”.
2. Numeric Constant
A number with or without a decimal point that is not enclosed inside double quotes. All positive and negative and decimal numbers are numeric constants. for example: 5,-7,7426.42etc
Rules For Naming The Variable
- a. A variable must be up to 40 characters long.
- b. It must begin with a letter.
- Variable word cant is reserved word.
It is a variable in which the intermediate value of another variable is stored. It helps to find the sum of all values assigned to the particular variable. Example
CLS S = 0 FOR P = 1 TO 5S = S + PNEXT PPRINT " THE SUM IS " ; SEND
The process of a departure of the control of the program Conditionally or Unconditionally from its sequential flow depending upon the result of the condition is known as Branching or Jumping. Control Structure is the statement of the structure that is used to handle different conditions and iterations. It is used to control the flow of a program. They are Sequential Structure, Selection Structure, and Loop Structure.
A structure in which the statements are executed sequentially one after another without changing the flow of the program. Example,
CLS INPUT A INPUT B LET S = A + B PRINT " SUM OF NUMBER"; S END
It is also known as a branching structure that allows you to transfer the program control from one part to another on the basis of a specified condition or without condition.
Types of Selection Structure:
- If Statement
- Select Case Statement
This is a decision-making statement that decides which statement has to be executed on the basis of the specified condition.
CLS INPUT A INPUT B LET S = A + B PRINT " SUM OF NUMBER"; S END
1. If…..Then Statement
It is a one-way decision-making statement that evaluates a condition and executes the statement if the result of the condition is true. Syntax,
If < Condition > Then Statement 1 End If
It is the two-way decision-making statement that can decide which part of the computer it has executed when the condition is true or false. It executes one part of a program if the condition is true and another part if the condition is not true or false. Syntax:
If < Condition > Then Statement 1 Else Statement 2 End if
Write a program to check whether the number is divisible by 4 and 5
CLS INPUT" ENTER THE NUMBER" ; N IF N MOD 4 = 0 AND N MOD 6 = 0 THEN PRINT" NUMBER IS DIVISIBLE IS DIVISIBLE BY 4 AND 6" ELSE PRINT" NUMBER IS NOT DIVISIBLE BY 4 AND 6" END IF END Mod=(divisible sign)
It is a multi-way decision-making statement that is used when there are two or more conditions to be evaluated. Syntax
If < Condition 1 > Then Statement 1 Else If < Condition 2 > Then Statement 2 ----------------------- -------------------- Else Statement- N End If ( N= Any Number)
It repeats a block of statements a specified number of times. 1,2,3,4…..10
CLS FOR K = 1 TO 10 PRINT K ; NEXT K END
It executes a block of statements repeatedly until the specified condition is true. 1,4,7,10…….25
CLS A = 1 WHILE A < = 25 PRINT A A = A +3 WEND END
It repeats a part of the program while a condition is true or until the condition becomes true. 100,90,80……up to 10
CLS S = 100 DO PRINT A; A = A -10 LOOP WHILE A > = 10 END
The Loop inside the loop structure is called Nested loop. It contains an outer loop and inner loop. 7 7 7 7 7 7 7 7 6 6 6 6 6 6 6 5 5 5 5 5 5 4 4 4 4 4 3 3 3 2 2 1
CLS FOR A = 7 TO 1 STEP -1 FOR B = 1 TO A PRINT A; NEXT B PRINT NEXT A END