Tuesday, September 4, 2018

QBASIC Elements

Every programming language consists of some basic elements which are required to make a program. The element required to construct a QBASIC program consists of a set of characters, keywords, constants, variables, operators and expressions.

A set of characters that are allowed to use in QBASIC is known as the QBASIC Character Set. The QBASIC Character Set consists of alphabets (both small and capital), numbers (0 to 9) and special characters. These special characters have their own meaning and function. The table below shows the special characters used in QBASIC.
QBASIC Elements

QBASIC keywords and variables are formed by using the characters defined in the QBASIC Character Set.

Keywords are those words which have special meanings in QBASIC. Keywords are formed by using characters of QBASIC Characters Set. Keywords are statements, commands, functions (built in functions) and names of operators. The keywords are also called Reserved Words. Some reserved words are CLS, REM, INPUT, LET, PRINT, FOR, DO, SELECT, MID$, ASC, SQR, LEN, LEFT$, TIME$ and INT.

Constants are the data or the values in a program that cannot be changed during the program execution. The data may be a letter, words, numbers, or special characters. A constant can be stored in a variable when it is required to use in more than one statement or expression. In QBASIC, these data/constants are grouped into two main categories. They are:
a. Sting Constant
b. Numeric Constant

a. String Constant:
Sting Constant is a letter, words, numbers, combination of letters with numbers or special characters enclosed in double quotes. Mathematical operations cannot be performed on String Constants.
“B”, “APPLE”, “SYMBOL NO:10205”, “!!! Welcome to QBASIC World !!!”, etc. are some examples of Sting Constants.

b. Numeric Constant:
Numeric Constant refers to a number. A number with or without decimal point is a numeric constant. Thousand separators are not allowed to use in numeric constant. Numeric data should not be enclosed in double quotes. Mathematical operations and logical operations can be performed on the numeric constants. 101, 105.50, 720, 45603, etc. are some examples of numeric constants.

Numeric Constants may be integer, long integer, single precision or double precision.
Integer: Integer is whole number between -32768 to 32767.
Long Integer: Long Integer is a large range of whole number.
Single Precision: Single Precision is seven digit or less than seven digit positive or negative number that contains decimal point. Single Precision can be in the exponential form using E or with a trailing exclamation point. (!). 564, 78.65, 1.2 E-06 and 12345.678! are some examples of Single Precision Constants.
Double Precision: Double Precision is 17 digit or less than 17 digit positive or negative numbers that contains decimal point. Double Precision can be in the exponential form using D or with trailing hash sing (#). 9999.99D-12, 2345.786# and 3456.78 are some examples of Double Precision Constants.

A program is written to perform certain tasks. A program needs data to produce information. A program can use data only when data are stored in the computer memory (RAM). In a computer memory, there may be many data. So, you need to tell the computer to use only those data which you want to use in a program. This is possible if you assign a name to the place where you have stored a data. In QBASIC, you can perform this task by using a variable. A variable is a place in the computer memory which has a name and stores data temporarily. Simply, you can say, a variable is an entity that stores data needed to be used in a program. Each program defines different number of variables. A value of a variable can be change during the execution of the program.

There are mainly two types of variables. They are:
i. String Variable
ii. Numeric Variable

A string variable stores sting data. Its types declaration sign is dollar ($). A numeric variable stores numeric data. A numeric variable can be Integer, Long Integer, Single Precision or Double Precision variables.
An Integer variable can store only an integer number. Its type declaration sign is percentage (%).
A Long integer variable can store a large range of whole number. Its type declaration sign is ampersand (&0).
A Single Precision variable can store a whole number as well as number with decimal. Its type declaration sign is exclamation sign (!). You can also use it without declaration sign. It is the default numeric variable.
A Double Precision variable also stores a whole number as well as number with decimal point. Its type declaration sign is hash (#).

Rules for naming a variable
a. Variable names can have maximum of 40 characters.
b. Variable names can have alphabets, numbers and decimal point.
c. A Variable name must begin with a letter.
d. A Variable name cannot begin with fn or FN alphabets. For example, fnames$, fnumetc.
e. Variable names cannot be reserved words.
f. Variable names may be ended with type declaration characters like $, %, &, !, and #.

Naam$, Address$, Bookname$, GameName$, etc., are examples of Sting Varibales.
Salary!, Age%, Mark, Number1, Number2, FirstNum, RollNumber, etc., are examples of Numeric Variables.

Operators are symbols that indicate the type of operation QBASIC has to perform on the data or on the values of variables.

There are four types of operators in QBASIC. They are Arithmetic Operators, Relational Operators, Logical Operators and Sting Operator.

a. Arithmetic Operators
Arithmetic Operators are used to perform mathematical calculations like addition, subtraction, division, multiplication and exponential. The following table shows arithmetic operators used in QBASIC.

Operation ------------ Operator ---------------- Example ------------------ Result
i. Addition ----------------- + ----------------------- 5+8 -------------------------- 13
ii. Subtraction ----------- - ---------------------- 8-6 --------------------------- 2
iii. Multiplication -------- * ---------------------- 5*4 --------------------------- 20
iv. Division ---------------- / ------------------------ 8/2 -------------------------- 4
v. Integer Division -------- \ ----------------------- 9\2 --------------------------- 4
vi. Exponential ----------- ^ ----------------------- 4^3 ------------------------- 64
vii. Modular Division --- Mod --------------------- 7 mod 3 ------------------------ 1

b. Relational Operators
Relational Operators are use to perform comparisons on two values of same type. A comparison of sting data with numeric data cannot be done. The comparison of sting data is done on the basis of ASCII value. The result of comparison is either true (non zero) or false (zero).
The following table shows the relational operators used in QBASIC.
Operator ------------- Relation ------------------------------- Example
i. = --------------------- Equal to -------------------------------- A = B, A$ = B$
ii. > -------------------- Greater than --------------------------- A > B, “CAT”>”RAT”
iii. < ------------------- Less than ------------------------------- A < B, "cat" < "cat"
iv. > = ---------------- Greater than or equal to ---------------- A > = B, X$ > = Y$
v. < = ----------------- Less than or equal to ------------------- A < = B, X$ < = Y$
vi. < > ---------------- Not equal ------------------------------ A$ < > B$, X <> Y.

c. Logical Operators
Logical Operators combine two or more relational expressions to evaluate a single value as True (Non Zero) or False (Zero). The result of evaluation is used to make decisions about the program flow. The commonly used logical operators in QBASIC are AND, OR and NOT.

i. AND Operator:
AND operator returns ‘True’ when all the results returned from individual relational expressions are ‘True’ otherwise it returns ‘False’.
The AND Truth Table is given shown below.
Condition1 (P) ------------- Condition2 (Q) -------------- Result (P AND Q)
F ------------------------------- T ----------------------------------- F
T ------------------------------- F ----------------------------------- F
F ------------------------------- F ----------------------------------- F
T ------------------------------- T ----------------------------------- T

Note: A ‘T’ indicates a true value and a ‘F’ indicates a false value.

ii. OR Operator:
OR Operator return ‘True’ if any one of the relational expressions returns ‘True’. If all the relational expressions returns ‘False’ then only the combined result returned by OR operator will be ‘False’.

The OR Truth table is as given below.
Condition 1 (A) ------------------ Condition2 (Q) --------------- Result (A or B)
F ------------------------------------------- T ---------------------------------- T
T ------------------------------------------- F ---------------------------------- T
T ------------------------------------------- T ---------------------------------- T
F -------------------------------------------- F ---------------------------------- F

iii. NOT Operator:
NOT Operator operates on one operand and returns ‘True’ if the logical operation returns ‘False’. The NOT truth table is as given below.
Condition1 (A) --------------------------- Result (NOT A)
F ----------------------------------------------- T
T ----------------------------------------------- F

d. String Operator
String Operator joins two or more than two sting data. The plus sign (+) is used as the String operator. The act of combining two stings is called concatenation. The following table shows the use of Sting Operator.
String Data (A$) -------------------- Sting data (B$) ----------------- A$ + B$
“Ram” ---------------------------------- “Thapa” ------------------------ Ram Thapa
“50” ------------------------------------- “45” ----------------------------- 5045

An expression is the combination of operators, constants and variables that is evaluated to get a result. The result of the expression is either string data, numeric data or logical value (true or false) and can be stored in a variable. For example, the following are expressions in QBASIC.
(A + B) > C
A > = B + C
u* t + ½*a*t^2

An arithmetic expression may contain more than one operator. While evaluating such expressions, a hierarchy is followed. The hierarchy in arithmetic operations is listed as given below:
a. Exponentiation (^)
b. Negation (-)
c. Multiplication and division
d. Integer division
e. Modular division
f. Addition and Subtraction

The hierarchy in relational operations are =, >, <, <>, < =, and > = respectively. The hierarchy in logical operations are NOT, AND and OR.

When parenthesis is used, it changes the order of hierarchy. The operators inside the parenthesis are evaluated first. So, you can say QBASIC expression follows rule of PEDMAS where P, E, D, M, A and S stand for parenthesis, Exponentiation, Division, Multiplication, Addition, and Subtraction respectively.
Algebraic expression cannot be used directly in programming. It must be converted into QBASIC expression.

Algebraic Expression --------------------------------- BASIC Expression
A = L × B ------------------------------------------------- A = L * B
P = 2(L + B) ---------------------------------------------- P = 2*(L + B)
I = (P × T × R)/100 --------------------------------------- I = (P * T * R)/100
V = 4/3 pi R^3 ------------------------------------------- V = 4/3 * PI * R^3

Friday, June 22, 2018

Dotnet Core console app with NLOG in Visual Studio Code

In this post I am going to create simple minimalistic dotnet core console application and integrate it with nlog.

Let's create a folder for our project. I am going to name it nlog_console.

$mkdir nlog_console
$cd nlog_console

Now we create dotnet core console app project and if you have Visual Studio Code installed run the second command below.

$dotnet new console
$code . &

Now lets add nlog package to a project.

$dotnet add package nlog

This will add the following code to our project file.

<PackageReference Include="nlog" Version="4.5.6" />

Nlog framework requires configuration file 'nlog.config' to specify logger tagerts and rules. We can copy paste the sample nlog.config file from this github page or use one below.

xml version="1.0" encoding="utf-8" ?>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
internalLogLevel="Info" >


<target xsi:type="File" name="file"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
<target xsi:type="Console" name="console"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />

<logger name="*" minlevel="Trace" writeTo="file,console" />


'nlog.config' file need to be copied to destination directory during project build. To achieve it add the code below to a project file.

<None Include="nlog.config" CopyToOutputDirectory="Always" />

Now we need to add actual logging code to a program. Copy paste the Program class code below.

using System;

namespace dotnetcore_nlog
class Program
private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
Console.WriteLine("Hello World!");
log.Trace("Loggin trace.");

To build and run our console app run code below or add launch.json config to Visual Studio Code.

$dotnet build
$dotnet run

You should see the following output and log file created in the same directory as our console app.

2018/06/19 18:48:13.130|TRACE|Loggin trace. |nlog_console.Program|



Thursday, September 21, 2017

Java Program to Print Table Using Loop

Java program to print a table

Below is a simple Java program to print a table using loops and Math.pow() function. Alternative solution to Introduction to Java Programming Chapter 2 Exercise 2.14. I decided to write this because a user made a comment about using for loops to solve the problem but his code didn't produce the required results.

public class PrintTable {
    public static void main(String [] args) {
        System.out.println("a"+ "\t" + "a^2" + "\t" + "a^3");
        for (int j = 0; j < 4; j++) {
            System.out.println((j+1) + "\t" + ((j+1)*(j+1)) + "\t" + ((j+1)*(j+1)*(j+1)));

Or you can use the Math.pow() function to achieve the same result.

import java.lang.Math;

public class PrintTable {
    public static void main(String [] args) {
        System.out.println("a"+ "\t" + "a^2" + "\t" + "a^3");
            for (int j = 0; j < 4; j++) {
                System.out.println((j+1) + "\t" + (int)Math.pow((j+1),2) + "\t" + (int)Math.pow((j+1),3));

Click here to see other solutions to Introduction to Java Programming.

Saturday, September 16, 2017

Don’t Hammer Away at That Keyboard

How to care for computer keyboard

Many people frequently say that they spend a great deal of time pounding on their computer keyboards. They indicate that they hammer away when working at their computers. In point of fact, pounding on a computer keyboard -- striking the keys too hard -- can have negative consequences not only for the computer itself but such action can result in serious health consequences for the computer user.

Pounding away at the computer keyboard can have long term health consequences for a regular computer user. Perhaps the most common disability or condition that can arise from striking the keys on a computer too stridently is carpal tunnel syndrome. This debilitating condition is painful and more often than not requires a person to have invasive surgery on the hands, wrists and lower part of the arms.

Another health condition that can arise when a computer user is too hard on the keys is a deadening of the nerves at a person’s fingertips. While this may not seem like that significant of a problem on the surface, in point of fact deadening the nerves at the ends of a person’s fingers can be very serious. On a day to day basis, a typical person avoids a good deal of harm to his person because he can feel what is in front, near or around him. For example, if you are working in the kitchen, you can avoid serious damage to yourself by instantly detecting the heat from a stove by lightly passing your fingers over and above a particular burner.

In addition to the other ailments and conditions set forth in this article, using too much pressure when striking a keyboard, hitting the keys too hard, can result in a deterioration of joints in a person’s fingers. In time a person may even end up afflicted with arthritis as a result of the heavy pounding on a computer keyboard.

When all is said and done, it simply is not necessary for a person using a computer to use a great deal of force or pressure on the keys. More often than not, a keyboard will function properly with only a light touch on the keys themselves. In other words, you simply do not need to bang away on the keyboard to be effective in your typing and utilization of a computer unit. By lightly touching your keyboard you will avoid a host of health problems both now and into the future. 

Monday, August 14, 2017

Displaying assemblies used by the application process

Today I am going to show how to print out assemblies used by the running application. This information may be useful for logs and can be collected on application/service start.

Below is a snippet of the console application that prints out information to console.

using System;
using System.Diagnostics;

namespace ConsoleApplication
public class Program
public static void Main(string[] args)
Process oProcess = Process.GetCurrentProcess( );

Console.WriteLine( string.Format( "Process: {0} ({1})",
oProcess.ProcessName, oProcess.Id ) );
Console.WriteLine( );
Console.WriteLine( "MODULES:" );
Console.WriteLine( "________" );

foreach ( ProcessModule oModule in oProcess.Modules )
Console.WriteLine( string.Format( "{0} ({1})",
oModule.ModuleName, oModule.FileName ) );