Control Structures in QBasic

When programming in QBasic, one of the foundational concepts you'll encounter is the use of control structures. These structures form the backbone of how your program makes decisions, executes tasks, and controls the flow of execution. Understanding and mastering these control structures is vital for writing effective and efficient QBasic programs. In this article, we will delve into the various types of control structures available in QBasic, including IF statements, loops, and SELECT CASE statements.

IF Statements

The IF statement is perhaps the most basic control structure in any programming language. It allows your program to make decisions based on certain conditions. Here's how you can use IF statements in QBasic:

Basic Syntax

IF condition THEN
    ' code to execute if condition is true
END IF

Example

Here's a simple example that checks if a number is positive:

DIM number AS INTEGER
PRINT "Enter a number: "
INPUT number

IF number > 0 THEN
    PRINT "The number is positive."
END IF

In this example, if the user enters a positive number, the program will output that the number is indeed positive. If they enter zero or a negative number, nothing will happen.

IF-ELSE Statements

Sometimes you'll want to execute one branch of code if a condition is true and another branch if it's false. This is where ELSE comes into play:

IF condition THEN
    ' code if true
ELSE
    ' code if false
END IF

Example

DIM number AS INTEGER
PRINT "Enter a number: "
INPUT number

IF number > 0 THEN
    PRINT "The number is positive."
ELSE
    PRINT "The number is not positive."
END IF

Nested IF Statements

QBasic also allows you to nest IF statements. This is useful if you have multiple conditions to check:

IF condition1 THEN
    ' code for condition1
ELSEIF condition2 THEN
    ' code for condition2
ELSE
    ' code if none of the conditions are true
END IF

Example

DIM number AS INTEGER
PRINT "Enter a number: "
INPUT number

IF number > 0 THEN
    PRINT "The number is positive."
ELSEIF number = 0 THEN
    PRINT "The number is zero."
ELSE
    PRINT "The number is negative."
END IF

Loops

Loops are control structures that allow you to repeat a block of code multiple times, which is extremely useful for iterating through arrays, generating repetitive outputs, or prompting user input.

FOR...NEXT Loops

The FOR...NEXT loop is one of the most common types of loops in QBasic. It allows you to execute a block of code a specific number of times:

FOR counter = start TO end
    ' code to execute
NEXT counter

Example

Here’s how you can use a FOR...NEXT loop to print the numbers 1 to 5:

DIM i AS INTEGER
FOR i = 1 TO 5
    PRINT i
NEXT i

WHILE...WEND Loops

The WHILE...WEND loop is another popular loop structure which continues executing the block of code as long as the specified condition is true.

WHILE condition
    ' code to execute
WEND

Example

This example will keep asking the user for a positive number until they finally enter one:

DIM number AS INTEGER
PRINT "Enter a positive number: "
INPUT number

WHILE number <= 0
    PRINT "That is not a positive number. Please try again."
    INPUT number
WEND

PRINT "Thank you! You entered a positive number."

DO...LOOP

The DO...LOOP structure is versatile and allows for both pre-testing and post-testing of conditions. Here’s the basic structure:

DO
    ' code to execute
LOOP WHILE condition

Example

Below is an example where the loop continues until the user opts to stop:

DIM answer AS STRING
DO
    PRINT "Would you like to continue? (yes/no)"
    INPUT answer
LOOP WHILE answer = "yes"
PRINT "Thank you! Goodbye!"

SELECT CASE Statements

A SELECT CASE statement is a more elegant way to handle multiple conditions based on the value of a variable. It is often cleaner and easier to read than a series of IF...ELSEIF statements.

Basic Syntax

SELECT CASE variable
    CASE value1
        ' code for value1
    CASE value2
        ' code for value2
    CASE ELSE
        ' code if none match
END SELECT

Example

Here’s how you can use a SELECT CASE statement to handle different cases based on the day of the week:

DIM day AS INTEGER
PRINT "Enter a number from 1 to 7 for the days of the week:"
INPUT day

SELECT CASE day
    CASE 1
        PRINT "Monday"
    CASE 2
        PRINT "Tuesday"
    CASE 3
        PRINT "Wednesday"
    CASE 4
        PRINT "Thursday"
    CASE 5
        PRINT "Friday"
    CASE 6
        PRINT "Saturday"
    CASE 7
        PRINT "Sunday"
    CASE ELSE
        PRINT "Invalid entry! Please enter a number between 1 and 7."
END SELECT

Conclusion

Control structures are essential for managing the flow of execution in QBasic programs. The IF statement allows for decision-making, while loops let you perform repetitive tasks effortlessly. SELECT CASE statements provide a cleaner alternative when dealing with multiple conditions. By becoming proficient with these control structures, you're well on your way to writing powerful and efficient QBasic programs. As you continue your programming journey, don’t forget to practice and experiment with these constructs to solidify your understanding!