Back to: Jss2 Computer Science – ICT (BST)
Topic: Basic Statements
WEEK: 9 & 10
A program written in BASIC comprises a comprehensive collection of statements, which are used to direct the computer to perform specific tasks. These statements are composed of various elements such as keywords, expressions, variables, constants, operators and special symbols. Each statement in a BASIC program is identified by a line number, which is a positive whole number and is followed by a keyword.
In BASIC programming, reserved words are known as keywords. These are words that have special meanings and uses in the language. BASIC keywords can be categorized into four groups, namely program flow control, input and output, data manipulation, and so on. These keywords are essential in the language, and using them correctly is necessary for writing efficient and effective BASIC programs. Understanding the different categories of BASIC keywords can help programmers write clear and concise code that is easy to read and maintain. Program flow control keywords are used to determine the sequence in which statements are executed, input and output keywords are used to handle input and output operations, data manipulation keywords are used to perform mathematical and logical operations on data, and so on.
Typical BASIC keywords are shown below:
A. Data manipulation
The table below shows Data manipulation statements and their uses
|LET||Evaluates expression and assigns value to variable|
|DATA||Holds a list of values assigned sequentially with READ command|
|CLS||Clears information displayed on computer monitor|
|READ||Reads data items from the DATA statement|
B. Program flow control
The table below shows program flow control statements and their uses
|RUN||Executes program statements.|
|END||Indicates the end of program execution.|
|STOP||Temporarily pauses program execution.|
|FOR … TO … (STEP) …||Repeats a section of code for a given number of times, with a counter variable available within the loop.|
|IF … THEN … ELSE||Performs comparisons or makes decisions based on specified conditions.|
|WHILE … END||Repeats a section of code while the specified condition is true.|
|REPEAT … UNTIL||Repeats a section of code until the specified condition is true.|
|GOTO||Jumps to a numbered or labelled line in the program.|
|ON … GOTO/GOSUB||Chooses where to jump based on specified conditions.|
|GOSUB||Temporarily jumps to a numbered or labelled line, returning to the following line after encountering RETURN.|
C. Input and output
Input and Output Statements and Their Uses
|Used to print or display information on the computer monitor|
|INPUT||Prompts the user to enter the value of a variable through the keyboard|
|TAB or AT||Sets the position where the next character will be shown on the screen or printed on paper|
|LPRINT||Used to print information from a computer to a connected printer or other output devices|
This is used to add comments and annotations to a program. It is also commonly used to give a title to a program and to indicate the purpose of a specific section of code.
Constants in BASIC refer to fixed values that do not change during the program’s execution. There are two types of constants: numeric and string. Numeric constants can represent integers, decimal fractions or numbers in scientific notation, while string constants consist of a sequence of characters enclosed in double quotation marks.
Variables are values that can change during a program’s execution. They are used to give identity to a computer memory location whose contents can be altered. Variables can be either numeric or string and are used to join values of both types. String variables are indicated by adding a dollar sign ($) at the end of their name, whereas numeric variables do not have this sign. Examples of string variable names include B12$, CLASSS, YEAR$, and MONTHS, while numeric variable names can be X, NUM1, SCORE, AMOUNT, and so on. BASIC has specific rules regarding variable naming, which are listed below.
it could be noted that variable names in BASIC must start with a letter and can contain letters, digits and underscores. They cannot contain spaces, punctuation marks or other special characters. Furthermore, BASIC is a programming language that was developed in the United States in the 1960s and is still used today in some applications.
Rules for Naming Variables in Basic
- A variable name in BASIC must begin with a letter between A to Z and cannot start with a number or a special character.
- A variable name cannot exceed 40 characters in length.
- Variable names may be followed by digits.
- There should not be any blank spaces between variable names.
- If a variable is a string, its name must end with a dollar sign ($).
- It is not possible to use BASIC keywords as variable names.
- Variable names are not case-sensitive. This means that the uppercase and lowercase letters in a variable name are treated the same. For example, “Score”, “score” and “SCORE” are all considered the same variable name in BASIC.
- Variable names should be meaningful and descriptive. It is important to use names that accurately reflect the purpose and content of the variable. This makes the code more readable and easier to understand.
- A variable name should not be a reserved word in another programming language. This is because the same variable name may have a different meaning in a different language, leading to errors in the code.
- It is good practice to follow a consistent naming convention for variables. This helps to ensure that variable names are easy to read and understand, and makes it easier to maintain and modify the code over time. Examples of common naming conventions include camel case (e.g. firstName, lastName) and snake case (e.g. first_name, last_name).
Simple Basic Statements
Simple Basic Statements are commands in the Basic programming language that perform a specific action. These statements are used to create and manipulate variables, control program flow, and perform calculations. Examples of Simple Basic Statements include PRINT, INPUT, IF…THEN, FOR…NEXT, and GOTO. These statements are used to create basic programs that perform simple tasks, such as printing text or performing simple calculations. Simple Basic Statements are often the building blocks of more complex programs and are a fundamental aspect of learning programming.
Examples of BASIC statements
- Assignment statements: These statements assign values or data items to variables. The LET, INPUT, DATA and READ statements are commonly used for this purpose in BASIC programming.
- Output statements: These are important BASIC statements that instruct the computer to display or print out information on an output device, such as a printer or screen. The PRINT and LPRINT statements are commonly used for this purpose.
- Program terminators: These statements indicate the end of a BASIC program or stop its execution. The END and STOP statements are commonly used for this purpose. The END statement must always be the last statement in a BASIC program, while the STOP statement can be used to halt the program’s execution at any point.
- Line numbers: Every BASIC statement must start with a line number. Line numbers are integer values with a maximum of five digits, and they must be unique within the program. BASIC statements are executed in numerical order, from the lowest line number to the highest.
- REM statement: The REM statement is used to add comments or documentation to a BASIC program. It allows the programmer to describe what the program or a segment of the program is about, and can be used anywhere in the program.
- RUN statement: This BASIC statement is used to execute a BASIC program.
- LIST statement: The LIST statement is used to display a range of BASIC program lines. For example, the command “LIST 45-95” would display lines 45 through 95 of the program.
- IF statement: The IF statement is used in BASIC programming to test whether a condition is true or false. If the condition is true, the statement following the IF statement is executed; if the condition is false, the statement is skipped. The IF statement is often used in conjunction with other statements to control the flow of a BASIC program.
- FOR-NEXT loop: This is a control structure used in BASIC to repeat a set of statements a fixed number of times. The loop is defined using the FOR statement, which sets the loop counter variable to an initial value and specifies the number of times to repeat the loop. The statements to be repeated are enclosed in the loop, and the loop is terminated with the NEXT statement.
- GOTO statement: This statement is used in BASIC to transfer control to a specified line number in the program. It can be used to create loops or to jump to a specific section of the program.
- GOSUB-RETURN: This is a control structure used in BASIC to transfer control to a subroutine within a program. The GOSUB statement is used to call the subroutine, which is a set of statements that perform a specific task. The subroutine is terminated with the RETURN statement, which returns control to the line following the GOSUB statement.
- DIM statement: This statement is used in BASIC to declare the dimensions of an array. Arrays are used to store multiple values under a single variable name. The DIM statement specifies the size and structure of the array, allowing the program to allocate the necessary memory.
- DATA statement: This is used to define a set of data values within a BASIC program. The values can be of any data type, including strings, integers, and floating-point numbers. The values are separated by commas and can be accessed by READ statements in the program.
- ON-GOTO statement: This statement is used in BASIC to transfer control to a specified line number based on the value of an expression. The expression is evaluated, and the corresponding line number is used as the target for the GOTO statement.
- RANDOMIZE statement: This is used in BASIC to initialize the random number generator. The statement sets the seed value for the random number generator, which determines the sequence of random numbers generated by the program.
- WHILE-WEND loop: This is a control structure used in BASIC to repeat a set of statements while a certain condition is true. The statements to be repeated are enclosed in the loop, and the loop is terminated with the WEND statement.
- INPUT statement: This is used in BASIC to read input from the user. The statement prompts the user for input and stores the input in a variable. The input can be of any data type, including strings, integers, and floating-point numbers.
- OPEN statement: This is used in BASIC to open a file for input or output. The statement specifies the file name, file access mode, and file type. Once the file is open, it can be read from or written to using other statements in the program.