Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Wednesday, February 16, 2022

Formatted Output in QBASIC

 

Formatted Output in QBASIC



Formatted Output in QBASIC



QBasic is a simple and easy-to-learn programming language that was popular in the 1990s, and is still used by some hobbyists and educators today. One of the features of QBasic is its ability to formatted output text to the screen, using various formatting options to control the appearance of the output. In this article, we'll explore some of the ways that you can use formatted output in QBasic.


Text Alignment

QBasic also provides ways to align text to specific columns on the screen, using the TAB function and the SPC function. Here's an example program that demonstrates these features:


TAB

This function moves the cursor to the print position when used in the PRINT and LPRINT statements.

Syntax: TAB (column)

Where,

Column, we have to mention the number of column where we want to display the text.

Example:

REM Example of TAB function

CLS

a$ = "QBASIC"

b$ = "Programming"

PRINT a$; TAB(10); b$

END

Output



 
Note:  There is 10 spaces gap between a$ and b$

 

SPC

This function skips the specified number of spaces in a PRINT and LPRINT statement.

Syntax: SPC(n)

Where,

n, number of spaces


Example:

REM Example of SPC function

CLS

PRINT "School"; SPC(8); "Level"; SPC(5); "Programming"

END

Output


 Note: In above example there is 8 spaces gap between “School” and “Level” and 5 spaces gap between “Level” and “Programming”

 

LOCATE

This function moves the cursor to the specified position. monitor.  It accepts the row and column position while moving the cursor.

Syntax:  LOCATE [row, column]

Where,

row, column are the number of row and column which the cursor moves.


Example:


REM Example of LOCATE function

CLS

LOCATE 3, 10

PRINT "School Level Programming"

LOCATE 5, 17

PRINT "QBASIC"

END

Output



 

Example:
CLS
PRINT "Text Alignment Example"
PRINT "---------------------"
PRINT
PRINT "Using TAB Function:"
PRINT "Column 1"; TAB(10); "Column 2"; TAB(20); "Column 3"
PRINT "Value 1"; TAB(10); "Value 2"; TAB(20); "Value 3"
PRINT
PRINT "Using SPC Function:"
PRINT "Item"; SPC(10); "Quantity"; SPC(10); "Price"
PRINT "-----"; SPC(10); "--------"; SPC(10); "-----"
PRINT "Apples"; SPC(10); 10; SPC(10); "$1.50"
PRINT "Bananas"; SPC(10); 5; SPC(10); "$0.75"
PRINT "Oranges"; SPC(10); 3; SPC(10); "$2.00"

In the above program, we use the TAB function to align text to specific columns on the screen, and the SPC function to insert a specified number of spaces between output values. The TAB function takes a numeric argument that specifies the column number to align to, and the SPC function takes a numeric argument that specifies the number of spaces to insert.


Numeric Formatting

QBasic provides a way to format numeric values with specific precision and alignment, using the PRINT USING statement. Here's an example program that demonstrates this feature:

PRINT USING Statement

This statement is used to print strings or numbers using a specified format on the screen.

Syntax::           PRINT USING “format string” ; expression


Example:  

      

CLS

PRINT USING “#####.##” ; 1234

PRINT USING”###.#” ; 123.275

END

Output:

1234.00

123.2

 

 

Formatting character for numeric data.

Character

Result

#

Digit position

+

Position of number’s sign

.

Decimal point position

-

Prints trailing minus sign for negative numbers only

**

Fills leading spaces with *

$$

Prints leading $

**$

Combines ** and $

^^^^

Prints number in exponential format

 

 Formatting character for string data

Character

Result

&

Prints entire string

!

Prints only the first character

\  \

Prints first ‘n’ characters, where n is the number of blanks between slashes + 2

 

LPRINT and LPRINT USING Statements

These statements function in the same way as the PRINT and PRINT USING statements except that output goes to the line printer and file number option is not permitted. . LPRINT will accept the same parameters that PRINT will, such as variables (numbers and string expressions), static text, and multiple combinations of each.

Syntax:

LPRINT [Expression list] [, or ;]

Example: LPRINT “This line directly goes to printer”

Note:  The LPRINT statement causes the PRINT statements to be directed to the printer instead of directing to the screen


Conclusion

Formatted output is a useful feature of QBasic that can help make your programs more readable and presentable. By using the PRINT USING, TAB, and SPC functions, you can control the appearance of your output and make it easier for users to understand. With a little experimentation, you can create a wide variety of output formats in QBasic.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages