Thursday, April 19, 2012

LESSON 8 : DATA TYPES

DATA EXAMPLES FOR DIFFERENT DATA TYPES
Data type determines the type of data a variable can store, for example a number or a character. Examples of data types are integer, double, string and boolean.



DATA EXAMPLES FOR DIFFERENT DATA TYPES

Integer
Integer data type contains any whole number value that does not have any fractional part.


This is how we declare an integer type constant in Visual Basic statement.


This is how we declare an integer type variable in Visual Basic statement.

Double
Any number value that may and could contain a fractional part.


This is how we declare a double type constant in Visual Basic statement.


This is how we declare a double type variable in Visual Basic statement.

String
Any value that contains a sequence of characters.


This is how we declare string type constant in Visual Basic statement.


This is how we declare string type variable in Visual Basic statement.


Boolean
Boolean type consists either a True or False value. Programmers usually use it to store status.


This is how we declare a boolean type constant in Visual Basic statement.


This is how we declare a boolean type variable in Visual Basic statement.

OTHER DATA TYPES


*Ihsan Panitia ICT Kebangsaan

LESSON 7 : CONSTANTS AND VARIABLES


CONSTANTS AND VARIABLES
Constant is a virtual data container that stores information. The value will
never change (remains constant) at any time during the course of a
program. 

Variables is a virtual data container that stores information. The value
inside may change at any time during the course of a program.


DIFFERENCES BETWEEN CONSTANTS AND VARIABLES


LESSON 6 : BASIC ELEMENT IN PROGRAMMING

5 Basic elements in programming.

1.   Constant.
2.   Variable.
3.   Data Type.
4.   Operators.
5.   Control Structures.



CONSTANTS AND VARIABLES

Constants
Constant is a data container that stores information. The value will never change (remains constant) at any time during the course of a program.

Declare is the official term used in programming to announce to the program
the condition of statement in programming.

Variables
Variable is a data container that stores information. The value inside may change at any time during the course of a program.
DATA TYPES, OPERATOR AND CONTROL STRUCTURES




Lesson 12 : 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.



Lets see an example of pseudo code that has sequence control structure.

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.



For example, today’s date is 1-JAN-2006, then the results will  be shown as follows:


Let’s see another example of pseudo code that has sequence control structure.


The following is the result of the example.

Lets see the flow chart for a general sequence control structure. 


As we can see, the flow chart for sequence control is very simple.

It will execute statement 1 followed by statement 2 and any following statements.


EXAMPLE

The flow chart represents a program that will request the user’s date of birth and then request today’s date, calculate the age and finally will display the user’s age.



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.
 
Let's see a pseudo code example for the “If...Then...Else" statement for a program that will print “You are too heavy to ride the toy car” if the student’s weight is more than or equal to 50.



Otherwise it will print “You can ride the toy car”

Let's see the flow chart for a similar selection control example.

Let's see a program that will respond differently for different service hours in a school library.



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”.

Let's write an example of a program that implements sequence control structure.


This program will declare a constant pi equal to 3.142. Then the program declares two variables to be used in the program (r and volume).

The program will then assign some value to the r variable.

The program will then calculate the volume using the formula volume = (4/3)Ï€r

The program will then display the volume calculated with a message box.


Now write an example program that implements selection control structure.


Write a program that will retrieve the current date from the system.

If today’s date is more than 15 then the program will display a message box with the message “We are towards the end of the month”.

Else the program will print “We are at the beginning of the month”.


DIFFERENTIATE BETWEEN SELECTION CONTROL AND SEQUENCE CONTROL







EXT : REPETITION CONTROL STRUCTURES

Let’s learn about the last control structure which is repetition control structure. A repetition control structure allows the programmer to specify an
action to be repeated while some condition remains true.


This is a pseudo code of  "While" structure.


This program segment will continue its looping until the condition of variable
named product is greater than 3000.


This is a flow chart of a repetition control structure.

*Ihsan Panitia ICT Kebangsaan

LESSON 4 : TRANSLATOR


Sometimes two people cannot understand each other because they don’t speak the same language. So they need the help of a third person who understands both languages. This third person is known as a translator.

All software packages or programs are written in high-level languages, for example, C++, Visual Basic and Java.

However, in order for the computer to be able to carry out the instructions, the high-level languages must be translated into machine language before the computer can understand and execute the instructions in the program.

The translation of high level languages to machine language is performed by a translator.

PROGRAM

Have you ever wondered how your computer runs your favourite software?
Your favourite software is a program that consists of several instructions that perform its operation.


A programmer will write a source code which consists of the instructions
needed to run a program. Then the compiler or interpreter with assembler will translates the source code into machine language which is made of a sequence of bits (eg. 01100011).

The computer will load the machine code and run the program.

ASSEMBLER



An assembler is a computer program for translating assembly language — essentially, a mnemonic representation of machine language — into machine language.

For example in intel 80836, the assembly language for the ’no operation’ command is NOP and its machine code representation is 10010000.

Example of assemblers are MACRO-80 Assembler and Microsoft MASM.


INTERPRETER
Interpreter is used to interpret and execute program directly from its source without compiling it first. The source code of an interpreted language is interpreted and executed in real time when the user execute it.

The interpreter will read each codes converts it to machine code and executes it line by line until the end of the program.



Examples of interpreter-based language are BASIC, Logo and Smalltalk.

COMPILER

The source code (in text format) will be converted into machine code which is a file consisting of binary machine code that can be executed on a computer. If the compiler encounters any errors, it records them in the program-listing file.

When a user wants to run the program, the object program is loaded into the memory of the computer and the program instructions begin executing.

A compiled code generally runs faster than programs based on interpreted language. Several programming languages like C++, Pascal and COBOL used compilers as their translators.


LESSON 3 : PROGRAMING APPROACHES

STRUCTURED PROGRAMMING EDUCATION

Structured programming often uses a top-down design model where
developers map out the overall program structure into separate subsections
from top to bottom.


In the top-down design model, programs are drawn as rectangles. A top-down design means that the whole program is broken down into smaller sections that are known as modules.A program may have a module or several modules.


Structured programming is beneficial for organising and coding computer
programs which employ a hierarchy of modules. This means that control is passed downwards only through the hierarchy.

Examples of structured programming languages include Ada, Pascal and
Fortran.


OBJECT-ORIENTED PROGRAMMING

The object-oriented approach refers to a special type of programming approach that combines data with functions to create objects.


In an object-oriented program, the object have relationships with one another.



One of the earliest OOP languages is Smalltalk. Java, Visual Basic and C++ are examples of popular OOP languages.  

DIFFERENCE BETWEEN STRUCTURED AND OBJECT ORIENTED PROGRAMMING

  •       Structured programming often uses a top-down design model.
  •  The object-oriented programming approach uses objects.