Friday, April 10, 2009

Form 5 : Logical Operators, Flow Chart, Control Structure

FUNCTIONS OF LOGICAL OPERATORS

Logical operators are notations that tell the computer to perform logical operations.
Examples of Logical operation are: AND, OR, and NOT.
Logical operator compares 2 conditions and returns a TRUE or FALSE value.

AND operator
Notice that truth value of X AND Y is True ( 1 ) if only both X and Y are True ( 1 ).
Else it is false ( 0 )

OR operator
Notice that truth value of X OR Y is only True ( 1 ) if either X or Y are True ( 1 ) or both X and Y are true ( 1 )
Else it is false ( 0 )

NOT operator

The diagram shows the truth table of NOT operator NOT X is the negation of X, it is essentially the 1's complement operation.
Notice that truth value of NOT X is True
( 1 ) when X is False and vice versa.

FLOW CHART
Let’s identify some of the main elements in the flow chart. We have five main elements in a flow chart.

Flowline and arrowhead use to connect symbols and indicate the sequences of operation.
Input or output shows either an input operation (e.g. an INPUT from the
user) or an output operation (e.g. PRINT some messages).

Process shows a process to be carried out (e.g. calculation).
Decision shows a decision (or choice) to be made. The program should continue along one of two routes (e.g. if...else).

A flow chart is a diagram using symbols to show the step-by-step sequence of procedures in a program. A flow chart describes the logic and program flow of a computer program graphically.

CONTROL STRUCTURES

Control structure is a structure of statements in programming that allows
the programmer to control the flow of a program.
Control structure can be divided into sequence, selection and repetition control structures.

SEQUENCE CONTROL

Sequence control refers to the linear execution of codes within a program. In sequence control, the statements are executed one by one in consecutive order.
In sequence control, the statements are executed one by one in consecutive order.

This program will request the user’s date of birth and then request today’s date, calculate the age and finally will print the user’s age.
It will execute statement 1 followed by statement 2 and any following statements.

SELECTION CONTROL

There are times when you want your program to make a decision based on the situation given.
For example, a program that stores student’s marks may respond differently to different marks.
Or maybe a simple mathematical program will display its result as odd or even, based on the result.
Selection control enables the programmer to assign different events for different situations.

An example of selection control is “If...Then...Else” statement. The basic pseudo code for “If...Then...Else” statement is as follows.
A student requests her service hours in a library from the program, the
program will then check whether her service hours are more than/equal to 30 or not.

If the service hours are over or equal to 30, the program will print a message, “Thank you for your service”.

If the service hours are lower than 30 then the program will print a message, “Please continue to serve in the library”.