Qbasic Control Statements with Examples

📋 Table Of Content
If you are new to programming or just a beginner you might find it hard to understand the concept of control statements, especially while working with the QBASIC programming language.
QBASIC is a beginner-friendly easy-to-learn programming language that allows you to write programs that can be executed on a computer. And one of the fundamental concepts you will learn about programming is about the control statement. So, in this article, we will understand QBASIC control statements and their syntax with the help of examples.
What are Control Statements in QBASIC?Types of Control Statements1. Conditional Statements2. Loop Statements3. Unconditional StatementControl Statements In QBASIC (QB64)Conditional Statement with exampleIF THEN statementIF THEN ELSE StatementIF THEN ELSEIF statementSELECT CASE StatementLooping Statement with exampleDO LOOP statementFOR NEXT StatementUnconditional Statement with ExampleGOTO StatementConclusion:
What are Control Statements in QBASIC?
The control statements are those statements that control the flow of execution of commands in a program. It helps us to execute certain statements based on specific conditions till the specific condition is met.
In QBASIC sometimes the user might want to change the order of execution of code, either by repeating or by skipping the executions of a few statements based on a given condition. In such cases, you have to use the control statements as they have the capability to make certain decisions in the run time of the code. Examples of control statements in QBASIC (QB64) are GOTO, IF THEN, IF THEN ELSE, END IF, etc.
Types of Control Statements
The control statement can be classified into three main types:
1. Conditional Statements
It enables you to execute a set of statements based on a particular condition. Examples of conditional statements are: IF-THEN, IF-THEN_ELSE, and SELECT CASE
2. Loop Statements
These statements enable you to repeat a set of statements until a specific condition is met. Examples of loop statements are: DO LOOP and FOR NEXT.
Learn more about Looping Statements in QBasic
3. Unconditional Statement
This statement executes regardless of whether a condition is true or false. It transfers the flow of the execution from one statement line to another statement. The GOTO statement is the only unconditional statement in QBASIC programming.
Here's a table repesentation of the control statements in QBasic.
Conditional StatementsLooping StatementsUnconditional StatementIF THENDO LOOPGOTOIF THEN ELSEFOR NEXTIF THEN ELSEIFSELECT CASE
Control Statements In QBASIC (QB64)
Here are some of the examples of control statements in QBASIC with syntax and examples.
Conditional Statement with example
IF THEN statement
It is the most basic control statement in Qbasic. It is used to make decisions based on comparisons. Here it checks if the condition is True, if yes, then it executes the THEN statement.
Syntax:
IF [conditional expression] THEN
[statement]
END IF
Example:
CLS INPUT "Type YES to print 1 to 4"; Y$ IF Y$ = "YES" THEN PRINT "1" PRINT "2" PRINT "3" PRINT "4" ENDIF END
ENDIF statement is used when we have multiple IF statement in the program. We have to use the ENDIF before the END statement.
IF THEN ELSE Statement
The IF THEN ELSE statement is a decision-making control statement. If the given IF condition is True then the THEN statement will execute or else the ELSE statement is executed.
Syntax:
IF [conditional expression] THEN
[statement 1]
ELSE
[statement 2]
END IF
Example:
CLS INPUT "Enter a number"; N IF N > 10 THEN PRINT "The entered number is greater then 10" ELSE PRINT "The number is smaller then 10" END IF END
IF THEN ELSEIF statement
It is similar to the IF THEN ELSE statement, but the difference is that in IF THEN statement we could only test one IF condition, and with the help of the IF THEN ELSEIF statement we can use multiple IF conditions in the same program.
Syntax:
IF [condition] THEN
[statement 1]
ELSEIF
[statement 2]
ELSE
[statement n]
END IF END
Example:
CLS N = 10 IF (N = 5) THEN PRINT "The first statement is true" ELSE IF (N = 10) THEN PRINT "The second statement is true" ELSE PRINT "Both statements false" END IF END
SELECT CASE Statement
The SELECT CASE statement enables you to execute a set of statements based on the value of the expression. It's similar to IF THEN statement but much easy to understand and concise.
Syntax:
SELECT CASE expression CASE value1 'Statements to execute if expression = value1 CASE value2 ' Statements to execute if expression = value2 CASE ELSE ' Statements to execute if expression does not match any of the above cases END SELECT
Example:
CLS INPUT "ENTER NUMBER BETWEEN 1 TO 4"; N SELECT CASE N CASE 1 PRINT "You are level 1" CASE 2 PRINT "You are level 2" CASE 3 PRINT "You are level 3" CASE 4 PRINT "You are level 4" END SELECT
Looping Statement with example
Looping statements are those control statement that enables you to repeat a set of statements until the condition is met.
DO LOOP statement
The DO LOOP is also known as the DO WHILE LOOP and it is used to execute a set of instructions within a loop if the comparison is True.
Syntax:
DO
[statement]
LOOP WHILE END
Example:
CLS N = 1 DO PRINT N N = N+1 LOOP WHILE N <=10 END
This program will print out natural numbers from 1 to 10.
FOR NEXT Statement
The FOR NEXT statement is used to execute a set of instructions for a certain amount of times.
Syntax:
FOR counter = [initial value] TO [final value]
[statement]
NEXT counter
Example:
CLS FOR n = 1 TO 5 PRINT n NEXT n END
This will print numbers from 1 to 5.
Unconditional Statement with Example
GOTO Statement
The GOTO statement is the only unconditional statement in QBASIC programming language. Itis used to transfer the execution flow of a program statement from one line to another.
Syntax:
GOTO [label/ line]
Example:
CLS
INPUT "Enter your mark:"; N
IF N >= 30 THEN GOTO P
IF N < 30 THEN GOTO F
P:
PRINT "You passed your exam"
END
F:
PRINT "You failed your exam"
END
Here, the GOTO statement along with the IF THEN statement execute a specific line depending on the condition of the IF condition.
Conclusion:
In this article, we have discussed the control statements in QBASIC and how we can use them to control the flow of execution in a program.
We have learned about conditional statements (IF...THEN...ELSE, SELECT CASE), looping statements (DO...LOOP WHILE, FOR...NEXT, WHILE...WEND), and unconditional statements (GOTO).
You can easily understand the use of all three main control statements by looking at their syntax and examples.
Related Articles:
Free QBasic Online Compilers – Online Editors
QBASIC Programming – Beginner’s Friendly
Download Qbasic (QB64) Free for Windows 10 and 11
QBasic Commands and Statements-2023
Qbasic Programming Examples and Exercises
Qbasic Color statement
Qbasic Looping statements with Examples