# QBasic

Implementation of programming language BasicQBasic is an IDE created by Microsoft Corporation, based on QuickBASIC.

QBasic is a DOS-style IDE, and was shipped with MS-DOS (starting with version 5.0) and Windows 95, 98, NT and ME. It belongs to second generation of Basic languages, featuring tools for structured programming, subroutines, while loops, line labels and user-defined structures.

QBasic contained a smaller subset of commands available from QuickBASIC, and did not support creating executable programs.

## Examples:

### Factorial:

Example for versions QBasic 1.1, QuickBASIC 4.5This example uses recursive factorial definition.

The default data type for calculations is floating point, so program output looks like this:

0! = 1

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

7! = 5040

8! = 40320

9! = 362880

10! = 3628800

11! = 3.99168Е+07

12! = 4.790016Е+08

13! = 6.227021Е+09

14! = 8.717829Е+10

15! = 1.307674Е+12

16! = 2.092279Е+13

```
DECLARE FUNCTION factorial (n)
FOR i = 0 TO 16:
PRINT STR$(i) + "! =" + STR$(factorial(i))
NEXT i
END
FUNCTION factorial (n)
IF n = 0 THEN
factorial = 1
ELSE
factorial = n * factorial(n - 1)
END IF
END FUNCTION
```

### Fibonacci numbers:

Example for versions QBasic 1.1, QuickBASIC 4.5Numbers which have already been calculated are stored in array F and are retrieved from it to calculate the next ones. To get program output in required format, the numbers in the array are concatenated to form one string with required delimiters. `STR$`

function converts a number to a string.

```
DIM F(16)
F(1) = 1
F(2) = 1
FOR i = 3 TO 16:
F(i) = F(i - 1) + F(i - 2)
NEXT i
DIM S AS STRING
S = ""
FOR i = 1 TO 16:
S = S + STR$(F(i)) + ", "
NEXT i
S = S + "..."
PRINT S
```

### Fibonacci numbers:

Example for versions QBasic 1.1, QuickBASIC 4.5This example uses recursive definition of Fibonacci numbers. Each call of `PRINT`

function prints the arguments to a separate line and adds a space before and after printed number, so program output looks like this:

1 ,

1 ,

2 ,

3 ,

5 ,

8 ,

13 ,

21 ,

34 ,

55 ,

89 ,

144 ,

233 ,

377 ,

610 ,

987 ,

…

```
DECLARE FUNCTION fibonacci (n)
FOR i = 1 TO 16:
PRINT fibonacci(i); ", "
NEXT i
PRINT "..."
FUNCTION fibonacci (n)
IF (n <= 2) THEN
fibonacci = 1
ELSE
fibonacci = fibonacci(n - 1) + fibonacci(n - 2)
END IF
END FUNCTION
```

### Fibonacci numbers:

Example for versions QBasic 1.1, QuickBASIC 4.5Fibonacci numbers are calculated using Binet’s formula. The resulting numbers can differ from actual ones slightly due to calculation imprecision; to remove this effect, we use function `INT`

which truncates fractional part of the number.

```
DECLARE FUNCTION FIBONACCI (n)
DIM S AS STRING
S = ""
FOR i = 1 TO 16:
S = S + STR$(INT(FIBONACCI(i) + .1)) + ","
NEXT i
S = S + "..."
PRINT S
FUNCTION FIBONACCI (n)
p1 = ((1 + SQR(5)) * .5) ^ n
p2 = ((1 - SQR(5)) * .5) ^ n
FIBONACCI = (p1 - p2) / SQR(5)
END FUNCTION
```

### Factorial:

Example for versions QBasic 1.1, QuickBASIC 4.5This example uses iterative factorial definition. Arithmetic overflow happens when trying to calculate 13!, but different implementations handle it in different ways: QBasic reports an exception, while QuickBasic just proceeds printing negative values.

```
DIM f AS LONG
f = 1
PRINT " 0 ! ="; f
FOR i = 1 TO 16:
f = f * i:
PRINT i; "! ="; f
NEXT i
END
```

### Hello, World!:

Example for versions QBasic 1.1, QuickBASIC 4.5, bwBASIC 2.50```
PRINT "Hello, World!"
```

## Comments

]]>blog comments powered by Disqus

]]>