Back to blog
Jun 27, 2025
5 min read

Basic Program Output

A guide to understanding basic program output in QBASIC

Understanding Basic Program Output

This guide explains how a simple QBASIC program works step by step. It is designed for beginners who are just starting to learn programming.

Program Code

Below is the QBASIC program we are analyzing:

CLS
FOR a = 1 TO 5
  FOR b = 1 TO a
    PRINT b;
  NEXT b
  PRINT
NEXT a
END

What Does This Program Do?

This program prints numbers in a specific pattern. It uses loops to repeat actions multiple times. Let’s break it down step by step:

  1. CLS: This command clears the screen. It removes anything that was displayed before, so you start fresh.

  2. FOR a = 1 TO 5: This is the outer loop. It controls how many rows of numbers will be printed. The variable a starts at 1 and increases by 1 each time until it reaches 5.

  3. FOR b = 1 TO a: This is the inner loop. It controls how many numbers will be printed in each row. The variable b starts at 1 and increases by 1 until it reaches the value of a.

  4. PRINT b;: This command prints the value of b. The semicolon (;) keeps the cursor on the same line, so the numbers are printed next to each other.

  5. NEXT b: This tells the program to go back to the start of the inner loop and increase the value of b.

  6. PRINT: After the inner loop finishes, this command moves the cursor to the next line. This is why each row of numbers appears on a new line.

  7. NEXT a: This tells the program to go back to the start of the outer loop and increase the value of a.

  8. END: This command stops the program.

How Does the Program Work?

The program uses two loops:

  • The outer loop controls the rows.
  • The inner loop controls the numbers printed in each row.

For example:

  • When a = 1, the inner loop runs once and prints 1.
  • When a = 2, the inner loop runs twice and prints 1 2.
  • When a = 3, the inner loop runs three times and prints 1 2 3.
  • This continues until a = 5.

Dry Run Table

A dry run is a step-by-step simulation of how the program works. It helps us understand what happens at each step.

Outer Loop (a)Inner Loop (b)ActionScreen Output
1
1PRINT 1;1
PRINT (newline)1
21
1PRINT 1;1
1
2PRINT 2;1
1 2
PRINT (newline)1
1 2
31
1 2
1PRINT 1;1
1 2
1
2PRINT 2;1
1 2
1 2
3PRINT 3;1
1 2
1 2 3
PRINT (newline)1
1 2
1 2 3
41
1 2
1 2 3
1PRINT 1;1
1 2
1 2 3
1
2PRINT 2;1
1 2
1 2 3
1 2
3PRINT 3;1
1 2
1 2 3
1 2 3
4PRINT 4;1
1 2
1 2 3
1 2 3 4
PRINT (newline)1
1 2
1 2 3
1 2 3 4
51
1 2
1 2 3
1 2 3 4
1PRINT 1;1
1 2
1 2 3
1 2 3 4
1
2PRINT 2;1
1 2
1 2 3
1 2 3 4
1 2
3PRINT 3;1
1 2
1 2 3
1 2 3 4
1 2 3
4PRINT 4;1
1 2
1 2 3
1 2 3 4
1 2 3 4
5PRINT 5;1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
PRINT (newline)1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
ENDProgram stops.

Final Output

The final output displayed on the screen will be:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Key Takeaways

  • Loops are used to repeat actions multiple times.
  • The outer loop controls the rows, while the inner loop controls the numbers in each row.
  • A dry run helps you understand how a program works step by step.

This program is a great example of how loops can be used to create patterns in programming!