• To start with QBasic is - Quick Beginners All Purpose Symbolic Instruction Code.
  • QBasic has both integrated development environment as well as interpreter to run a variety of BASIC programming languages.
  • These languages are based on QuickBASIC.
  • We need QBasic interpreter to execute QBasic program.

First program

  • You have to initially launch the QBasic interpreter.
  • Where you might see a window requesting a list of "parameters."
  • Once the window opens up, hit the Enter key to continue.
  • You will now be able to see the QBasic interpreter, which has a blue background.
  • The blue background displays a dialogue box at the centre.
  • To make the screen smaller, press "Alt +Enter”.
  • To hide the dialogue box, press the Esc key.

  class

  • Type the following in the QBasic interpreter:
  • PRINT "Hello World!"
  • To run the program, press F5.
  • You will now see the output on a black screen.
  • You will find Hello World at the top, and you can press any key to continue at the bottom.
  • You can also press any key on the keyboard to return to the main screen.
  • It the program is run again, the interpreter adds another Hello World.

  class

Deleting the program

Follow the below steps to erase the current program:

  1. Go to the "File" menu and the click "New."
  2. You will be prompted asking if you want to save the program.
  3. If you want to delete the file, click on “No”.

  Strings

  • Strings are certain types of data or information.
  • Strings can contain a sequence of characters such as letters, numbers, and symbols which are enclosed in quotation marks.
  • For example, "Hello World!" is a string.

Few more examples – The following are also strings:

"0123456789"

"This is a string"

"abc123"

"1 + 1 = 2"

"!@#$%^&*()"

 

When a dollar sign ($) is added to the end of a variable, then it is a string.

Example –

X$ = "Hello World!"

PRINT X$

Output:

Hello World!

 

Commands

  • As the word indicates, a "command" instructs the QBasic interpreter to perform some instructions.
  • We also have a command to print on the output screen.
  • Using PRINT command we can instruct the QBasic interpreter to print something to the screen.
  • In this case, the interpreter printed "Hello World!”
  • Example –

PRINT 512 (or?512)

 

Now press F5 to run the program.

The program outputs: 512

Expressions

  • Expression helps in performing the mathematical calculation.
  • Example:

2 + 2 (returns 4)

10 - 4 (returns 6)

3 * 34 (returns 102)

  • To refresh or clear the output screen, use the CLS command.
  • To insert spaces between the two words put a comma instead of a semi-colon.

Example –

PRINT "Hello",

PRINT "World"

Output:

Hello World

Variables

  • Variables are information (or just a bit of information) stored on computer's memory.
  • The location or the area where the variable is stored in RAM is called the "address."

  class

The IF and THEN commands

  • These commands are used to compare an expression.
  • Based on the expression they perform actions.

x = 5, IF x = 5 THEN PRINT "x equals 5"

Since X does equal 5 in this case, the program outputs:

x equals 5

Expression signs

You can also enter the following statements, instead of the equals sign:

x < 5 (x is less than 5)

x > 5 (x is greater than 5)

Run the following:

x = 16

IF (x > 5) THEN PRINT "x is greater than 5"

Output:

x is greater than 5

Labels and the GOTO and GOSUB commands

  • To move to a certain line in your program, you can use the GOTO and GOSUB command.
  • To specify which point to be executed in the program we have labels.

Example –

GOTO

Run the following example program:

PRINT "1"

GOTO TheLabel

PRINT "2"

TheLabel:

PRINT "3"

Output (notice how PRINT "2" is skipped):

1

3

 LOOPS

  • To make an action repeat multiple times, we use "Loops".
  • We have four types of loops:
    • ..GOTO
    • ..WEND
    • ..LOOP
    • ..NEXT

STOPPING LOOPS

  • When the program is on loop and to stop the loop before the execution use the EXIT command, followed by either FOR or DO.

FOR x = 1 TO 5

PRINT x

IF (x = 2) THEN EXIT FOR

NEXT x

Output:

1

2

Recap

Programming in QBASIC

  • To start with QBasic is - Quick Beginners All Purpose Symbolic Instruction Code.

First program

Deleting the program

Strings

Commands

Expressions

Variables

The IF and THEN commands

Labels and the GOTO and GOSUB commands

Loops

Stopping loops