QBasic Statements
👉It covers
- Different Types of Statements
like:- - Input/ output statement
- Declaration Statements
- Assignment statements
- Control flow statements
The statement is a collection of commands used in the program. CLS, INPUT, PRINT, REM, END etc. are the examples of QBASIC statements. Statements can be categorized into four groups:
- Declaration statement
- Assignment statement
- Input/ Output statement
- Control flow statements1. Declaration statements: The statements which are used to declare variable in the program are called declaration statements. DIM, CONST, REM etc. are the example of declaration statements
DIM statements:- It is specially used to declare array variables. Though it can be also used to declare a simple variable.
Syntax
DIM variable AS data type ----------------> Simple Variable declaration
DIM variable(subscript) ----------------> Array Variable declaration
DIM NAME AS STRING
DIM AGE AS INTEGE ----------> Simple Variable declaration
DIM AGE AS INTEGE ----------> Simple Variable declaration
DIM NAME$(5)
DIM AGE(5) -----------> Array Variable declaration
DIM AGE(5) -----------> Array Variable declaration
CONST Statements:
Here CONST stands for constant. According to its name this statement is used to store constant value in a particular variable throughout the program execution.
Here CONST stands for constant. According to its name this statement is used to store constant value in a particular variable throughout the program execution.
Syntax:
CONST <constname1=expression><,><cinstname2=expression>
Example:
CONST <constname1=expression><,><cinstname2=expression>
Example:
CONST PI = 22/7
REM statement:
Its is used to give remarks to the program. It is non- executable statement which can also be denoted by single quotation(').
Syntax:
REM<remarks>
Example:
Rem program to calculate the area of a rectangle.
' program to find the perimeter of a rectangle.
2. Assignment statements: The statements which are used to assign a numeric or string value to a variable are called assignment statements. LET, SWAP, READ... DATA etc. are the examples of assignment statements.
LET statement:
It is used to assign a numeric value or a string expression to a variable. But it is an optional statement.
Syntax:
LET<variable>=<string expression/value>
Example:
LET I = 25
LET C$="COMPUTER"
SWAP statement
This statements is used to exchange the values of two similar type of variables.
Syntax: SWAP<variable1, variable2>
Example:
CLS
A=5
B=10
C=15
D=20
PRINT"BEFORE SWAPPING"
PRINT A, B, C, D
SWAP A, B
SWAP C, D
PRINT"AFTER SWAPPING
PRINT A, B, C, D
END
OUTPUT: Before Swapping
5 10 15 20
After Swapping
10 5 20 15
READ...DATA statement
It is used as complimentary to each other. READ statement reads multiple values by multiple variables from the data listed in the DATA statement. The number of data listed in DATA statement must be equal or more than the variable in READ statement.
It is used as complimentary to each other. READ statement reads multiple values by multiple variables from the data listed in the DATA statement. The number of data listed in DATA statement must be equal or more than the variable in READ statement.
Syntax:
READ variable1, variable2,..
DATA value1, value2...
DATA value1, value2...
Example:
CLS
READ SNO, NAM$, CL
PRINT SNO, NAM$, CL
DATA 5, Ram, 9
END
Output: 5 Ram 9
Output: 5 Ram 9
a. Input/ Output statements: Input/ Output statements are those statements that allow the user to input data to the computer and print the data. They are used to perform input/output operation for the computer. INPUT, PRINT, LPRINT, etc. are some examples of Input/ Output statements.
CLS statement:
It clears the previous output from the display screen and makes the screen blank.
Syntax:
CLS
Example:
CLS
INPUT statement
Input statement is used to read input from the keyboard during program execution.
Syntax:
INPUT<"prompt"><;/,><var1,var2,var3,...>
Example: Input "Enter your name"; n$
Input "Enter your roll no. and section" ; r, sec$
LINE INPUT
This statement allows to input line of data at a time and assign into single variable. It can accept a complete line of maximum 255 characters.
Syntax:
LINE INPUT "STRING"; string variable
Example:
CLS
LINE INPUT "please enter the line of text"; n$
PRINT n$
End
Output
Please enter the line of text?
Please enter the line of text?
PRINT Statement
Print Statement is used to display the output of the program or displays what ever is written between double quotation marks.
Syntax:
PRINT[list of expression]
Example:-
PRINT "My first program"
PRINT "I am a student of LP DEVKOTA ENGLISH BOARDING SCHOOL"
LPRINT
This statement works just like a print statement, the only difference is that, it sends the output to the printer and no to the screen.
Syntax:
LPRINT<expression list><;/,>
Example:
CLS
LPRINT "LP DEVKOTA ENGLISH BOARDING SCHOOL"
END
PRINT USING
PRINT USING writes formatted output to the screen or to a file.
Syntax:
PRINT USING "FORMATTING STRING"; EXPRESSION
! = Displays only the first character of string.
& = Displays all the string.
# = Digit position is specified.
** = Prints asterisk in the leading spaces. The two asterisk also specifies two digits position.
$$ = Prints a dollar in front of the numeric value.
Example 1:
X$= "###.###"
A= 100: B= 50.50: C= 1
PRINT USING X$; A
PRINT USING X$; B
PRINT USING X$; C
END
Output:
100.000
50.500
150.500
Example 2:
CLS
PRINT USING "!"; "LP DEVKOTA"; "ENGLISH"; "BOARDING" "SCHOOL"
PRINT USING "&"; "LP DEVKOTA PUBLICATION PVT.LTD"
PRINT USING "$$###"; 17
PRINT USING "####."; 17
Output:
LEBS
LP DEVKOTA ENGLISH BOARDING SCHOOL
$17
****17
Control statements
The statements which are used to control the flow of execution of the program statements are called control flow statements. The control flow statement are used to make decision or to control program flow at the time of program execution. This statements are also known as branching and jumping statements. Branching and Jumping refers to departure of program execution from one line to another line of the same program conditionally or unconditionally. GOTO, IF..THEN, SELECT CASE etc. are the example of control flow statements. These statements are also classified into two types:
i. Conditional statement
ii. Unconditional statement
Conditional statement transfers the flow of program execution according to the condition. IF.. THEN, SELECT CASE etc. are used for conditional statement. On the other hand unconditional statement transfers the flow of program execution any condition. GOTO statement is used for this purpose.
GOTO statement
It is the unconditional branching(jumping)statement. It branches the control flow to given line without testing any condition.
Syntax:
GOTO[line number/ label]
Example:
CLS
A=1
PRINT A
GOTO LABEL
B=7
PRINT B
LABEL:
END
The output of the above program is 1 because after displaying the values of a the GOTO statement forces to jump to the line called LABEL and program.
IF statement
We know that QBASIC execute the instructions of program sequentially one after another. But some time problems cannot be solved sequentially. So, we need to branch or jump from one line to another line. IF … THEN statement is the conditional branching (jumping) statement. This statement is used to make decision. It executes block of statement depending upon the given condition. The condition is given between IF and THEN. If the condition is True it performs task and if the condition is false the execution continuous with the next executable statement. It branches the control flow according the given condition.
This statement can be used in different following ways:
a) IF... THEN Statement
Syntax:
IF<condition>THEN<statement>
Example:
CLS
INPUT "Enter first number"; A
INPUT "Enter second number"; B
IF A>B THEN PRINT A
END OUTPUT:
Enter first number? 10
Enter second number? 5
10
b) IF...THEN...ELSE Statement
Syntax:
IF<condition>THEN
<staement1>
ELSE
<staement2>
END IF
Example:
CLS
<staement1>
ELSE
<staement2>
END IF
Example:
CLS
INPUT" Enter any Number"; N
IF N MOD 2 = 0 THEN
PRINT "Even Number"
ELSE
PRINT "Odd Number"
PRINT "Even Number"
ELSE
PRINT "Odd Number"
END IF
END
Output:
Enter any Number? 5
Odd number
Output:
Enter any Number? 5
Odd number
c) IF... THEN... ELSE IF statement
Syntax:
IF<condition 1>THEN
<statement1>
<statement1>
ELSE IF<condition2>THEN
<statements2>
....................
........................
.............................
ELSE
........................
.............................
ELSE
<statement n>
END IF
Example:
END IF
Example:
CLS
INPUT "Enter a number"; N
INPUT "Enter a number"; N
IF N < 100 THEN
PRINT "It is lesser than 100"
ELSE IF N= 100 THEN
PRINT "It is equal to 100"
PRINT "It is lesser than 100"
ELSE IF N= 100 THEN
PRINT "It is equal to 100"
ELSE
PRINT "It is greater than 100"
END IF
END IF
END
Output:
Enter a number? 250
It is greater than 100
It is greater than 100
SELECT CASE SATEMENT
Executes one of several statement blocks depending on the value of an expression.
Syntax:
SELECT CASE test expression
CASE expressionlist1
CASE expressionlist1
[statementblock1]
CASE expressionlist2
[statementblock2]
CASE ELSE
[statementblock-n]
Example:
INPUT "Enter Your percentage"; per
SELECT CASE Per
CASE is >= 90
PRINT "You Got A+"
CASE 80 TO 89
PRINT "You Got A"
CASE 70 TO 79
PRINT "You Got B+"
CASE 60 TO 69
PRINT "You Got B"
CASE 50 TO 59
CASE 50 TO 59
PRINT ''You Got C+"
CASE 40 TO 49
PRINT "You Got C"
CASE ELSE
PRINT "Sorry very less Grade"
END SELECT
Output:
END SELECT
Output:
Enter Your percentage? 95
You Got A+
0 Comments
If you have any doubts, please let me know.