- Basic Programming (one-dimensional array): Basic programming refers to the fundamentals of computer programming. A one-dimensional array is a data structure that holds a collection of values of the same type, each identified by an index or a key.
- DIM Statement: In BASIC, the `DIM` statement is used to declare the dimensions and size of arrays. It allocates memory space for the array elements. For example: `DIM myArray(10)` declares an array named `myArray` with 11 elements (indexed from 0 to 10).
For… Next Statement: The `For… Next loop is used for executing a block of code repeatedly for a specific number of times. It uses a loop variable that is incremented or decremented with each iteration. The loop continues until the loop variable reaches a specified value.
Example:
“`
FOR i = 1 TO 10
PRINT i
NEXT i
“`
- While… End Statement: The `While… End` loop is used for executing a block of code repeatedly as long as a certain condition remains true. The loop checks the condition before each iteration. Example:
“`
WHILE condition
‘ Code to be executed
END WHILE
“`
Now, let’s write a BASIC program to calculate the area of 10 different rectangles using both the `While… End` loop and the `For… Next` loop.
Using For… Next Loop:
“`basic
DIM length(10)
DIM width(10)
DIM area(10)
FOR i = 1 TO 10
INPUT “Enter length of rectangle ” + STR$(i) + “: “, length(i)
INPUT “Enter width of rectangle ” + STR$(i) + “: “, width(i)
area(i) = length(i) * width(i)
NEXT i
FOR i = 1 TO 10
PRINT “Area of rectangle ” + STR$(i) + “: “; area(i)
NEXT i
“`
Using While… End Loop
“`basic
DIM length(10)
DIM width(10)
DIM area(10)
i = 1
WHILE i <= 10
INPUT “Enter length of rectangle ” + STR$(i) + “: “, length(i)
INPUT “Enter width of rectangle ” + STR$(i) + “: “, width(i)
area(i) = length(i) * width(i)
i = i + 1
END WHILE
i = 1
WHILE i <= 10
PRINT “Area of rectangle ” + STR$(i) + “: “; area(i)
i = i + 1
END WHILE
“`
Both versions of the program will calculate and display the areas of 10 rectangles using different types of loops. The choice between `For… Next` and `While… End` depends on the specific needs of the program and the desired looping behaviour.
Read also:
Graphics: CorelDraw, Examples & Features
Database
Computer Network: LAN, MAN, WAN, PAN, HAN, CAN, VPN, WLAN
Career opportunity in data processing
Software Maintenance