Note on Arithmetic, Relational or Logical Expression

  • Note
  • Things to remember
  • Videos
  • Exercise
  • Quiz

Arithmetic Expressions

QBASIC can handle arithmetic expression involving the five arithmetic operators + (addition), - (subtraction), * (multiplication, / (division) and ^ (exponentiation).

The hierarchy of operations is as follows:

  • Exponential
  • Multiplication and division
  • Addition and subtraction

Thus, in a particular arithmetic expression, the order of execution is as per this hierarchy, i.e. all exponential operations are performed first, and then multiplication/division and the addition/subtraction operations are the last to be carried out. Note that within a particular hierarchical group. Within a particular hierarchical group, the operations are executed from left to right and by use of parentheses normal hierarchy of operations can be altered.

In addition to this hierarchy of operations, the following rules must be kept in mind in arithmetic expression:

  • Two operations must not appear together. For example, C+-D, A/-C, etc are not permitted.
  • String constants and string variables should not be used in arithmetic expressions. For example P+P$ is wrong.
  • When brackets are used, they must be used in pairs, i.e. every left bracket must be matched with a right bracket.
  • Denominator of an expression should not be zero.
  • Within a given pair of parentheses, the natural hierarchy of operations will apply.

Let us take an example where we give QBASIC equivalents of a few algebraic expressions.

Algebraic Expression QBASIC Equivalent
-2A+B -2*A+B
-A+B/C+D -(A+B)/(C+D)
-A(B+C) -A*(B+C)
-B2-4AC -B^2-4*A*C

Relational or Logical Expressions

A relational expression is formed by using any of the following relational operators:

Relational Operator Meaning
= Equal to
> Greater than
< Less than
<= Less than or equal to
>= Greater than or equal to
<> Not equal to

In the execution of programs, it is sometimes desired to compare two numerical quantities (or sometimes string quantities) and take decisions on achieving certain conditions. For example, we may be interested to check the number of repetitive calculations performed or to find out whether the denominator of an arithmetic expression has become zero or if a particular quantity is negative, and so on. Expressions written to compare two quantities using certain rational operators are known as relational expressions.

When expressions are used on either side of relational operators, the expressions will be evaluated first and then the results of expressions compared. This means that relational operators come last in hierarchy of operators.

Logical operators

QBASIC, also supports logical operators to perform logical operation on numerical values. Logical operators are used to connect two or more relations and return a TRUE or FALSE value to be used in a decision.

The common logical operators are:

  • AND Conjunction
  • OR Disjunction
  • NOT Logical Negation

For example, the expression A>50 AND B>150 is TRUE when A is more than 50 and at the same time B is more than 150.

Logical operators return results as indicated in the following tables. T indicates a TRUE and F indicates FALSE. X and Y are relational expressions.

AND Operator

X Y X AND Y
T T T
T F F
F T F
F F T

OR Operator

X Y X OR Y
T T T
T F T
F T T
F F F

NOT Operator

X NOT X
T F
F T

Keywords

Specific words which are not applicable to use as a variable on computer program are Keywords. These are also called reserved words. For example INPUT, GOTO, PRINT etc.

Syntax

A set of rules and regulation that must be followed to construct the program structure is called Syntax. It is one type of grammar to use the command and statement of computer.

  • Logical operators are used to connect two or more relations and return a TRUE or FALSE value to be used in a decision.
  • Specific words which are not applicable to use as a variable on computer program are Keywords.
  • A set of rules and regulation that must be followed to construct the program structure is called Syntax.
.

Very Short Questions

QBASIC can handle arithmetic expression involving the five arithmetic operators + (addition), - (subtraction), * (multiplication, / (division) and ^ (exponentiation). The hierarchy of operations is as follows:

  • Exponential
  • Multiplication and division
  • Addition and subtraction

Logical Operator, Meaning
=, Equal to
> , Greater than
< , Less than
<=, Less than or equal to
>=, Greater than or equal to
<> , Not equal to

Logical operators are used to connect two or more relations and return a TRUE or FALSE value to be used in a decision.

The common logical operators are:

  • AND Conjunction
  • OR Disjunction
  • NOT Logical Negation

0%
  • The meaning of < = is ______ .

    greater than or equal to
    not equal to
    less than or equal to
    greater than
  • The symbolic representation of greater than  equal to is ______.

    < =
    > =
    =
    >
  • The Qbasic equivalent of -A+B/C+D is ______.

    -B^2-4*A
    -(A+B)/(C+D)
    none of answers are correct
    -B^2-4*A*C
  • While using AND operator, if the x is true and y is false then output will be ?

    Can't say
    Maybe False
    False
    True
  • While using OR operator, if the x is true and y is false then output will be ?

    False
    True
    Neither true nor false
    Cannot be true
  • Specific words which are not applicable to use as a variable on computer program are called _______.

    keywords
    relational or logical expressions
    syntax
    logical negation
  • You scored /6


    Take test again

DISCUSSIONS ABOUT THIS NOTE

You must login to reply

Forum Time Replies Report
sabin parajuli

enter your best friend's first name and display in reverse form and also show palindrome or not.


You must login to reply

In which statement logical operators are given


You must login to reply

Prabin Subedi

WAP to display following patterns:Programme, rogramm, ogram, gra, r


You must login to reply

Prabin Subedi

WAP to display the following series:Computer, Compute, Comput, Compu, Comp, Com, Co, C


You must login to reply