Computer Programming 1Part 1
Computer Programming 1Part 1
TIMBAL
Teacher
Notice that after the number 7, we put 10. This is not the same as ten, but is read as “
one and zero” (1, 0) to represent the next value. This is the same for 20 which is not twenty but “two
and zero” (2, 0), and so on.
To illustrate the octal number system, observe the following table:
Decimal Octal
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 10 (1, 0)
9 11 (1, 1)
10 12 (1, 2)
Columns are used in the same way as the decimal system, as such the leftmost column
is used to represent the greatest value.
Hexadecimal System
The hexadecimal number system is a number system that uses sixteen digits as its base,
instead of 10 digits as in the decimal system. These sixteen digits are represented by 1, 2, 3, 4, 5, 6,
7, 8, 9, A, B, C, D, E, and F.
In the hexadecimal number system, when we reach 9, instead of adding another column
of the digit that starts with zero, we simply use a different digit represented by letter A.
Consider the following table as guide:
Notice that the hexadecimal adds another column starting with zero only after exhausting
its sixteen digits. This repeats every time we count beyond its base of 16, just like how the decimal
system repeats and adds another column starting with zero after we exhaust its ten digits.
Binary System
The binary number system or the base-2 number system uses 0 and 1 to represent a
range of values. The binary number system follows the same principle as in the decimal, octal, and
hexadecimal number systems.
For example, with the decimal system, when we exhaust all the available digits (0 t0 9),
we simply add another column of 0 and write 1 on the left so we have 10, which represents ten.
The same principle works for the binary number system. When we exhaust all the
available digits (0 and 1), we simply add another column of 0 and write 1 on the left, so we have 10
(1, 0), which represents two. To illustrate this, study the following table on the next page.
The Binary Number System
You have probably wondered why, of all number systems, it is the binary system that is
used for computers. In the first days of computer technology, the decimal system was used for its
operation. However, a mathematician named John von Neumann knew that a numbering scheme for
computers should be aligned with the physical characteristics of electronic circuitry, which only has
two states: on and off. By using the binary system, Neumann's design was able to simplify the ways
computers handle data.
In this lesson, we will learn how to perform number system conversion. This is important
for us to understand how computers process data and translate this data into different formats or
number base.
Since we are working with four numbers systems that use only one numeral system (the
symbols 0 to 9) the represent their values, this can easily confuse readers when looking at numbers
and without knowing what kind of number system they represent.
In writing we distinguish different number systems from each other by indicating its base
using a subscript. Consider the following examples.
Example:
10002
In this example, we recognize that the set of digits are binary and not decimal because of
a subscript that indicates its base. Therefore, we know that the digits 1000 in the example do not
represent “one thousand” in decimal, but actually represent a value of 8 in binary, read as “one, zero,
zero, zero”. The same guide applies for octal, decimal, and hexadecimal numbers. Remember that
the subscript denotes the base of the number or its number system.
Step 2: Get the remainder from step one as the rightmost digit (least significant digit) of a new base
number.
Step 4: Record the remainder from step three as the next digit (to the left) of the new base number.
Repeat steps three and four, getting reminders from right to left until the quotient
becomes zero in Step 3.
Let us take a look at how these steps are applied in conversions from decimal to binary,
decimal to octal, and decimal to hexadecimal.
Step 2: Multiply the base digits by their base value, raised to the power of their positional value that
we get from step one.
Step 3: Get the some of the products from Step 2. The total is the equivalent value in decimal.
Today we hear a lot of buzz around the word “digital”. It has a lot to do with modern
computer technology. Surprisingly, only a few people know what it means when you ask them what
digital is, or what makes a computer digital.
A modern computer is an electronic device that uses digital signals. Digital signals
represent information with a sequence of 0s and 1s. Since digital signals are processed inside a
computer, what would they be the language of a computer is called machine language. For us to
understand how digital signals are processed, we have to learn binary number systems and the
mathematics behind it.
Binary Addition
Binary addition is done the same way as decimal addition, except that only 0s and 1s Are
used instead of the zero to 9. This makes a binary addition much simpler than decimal addition, as
we only need to remember the following rules:
Rule #1: 0 + 0 = 0
Rule #2: 0 + 1 = 1
Rule #3: 1 + 0 = 1
Rule #4: 1 + 1 = 10
Rule #5: 1 + 1 + 1 = 11
Binary Subtraction
Just like binary addition, binary subtraction has several rules when it comes to subtracting
its digits. Take note of the following basic rules for subtracting binary:
Rule #1: 0 - 0 = 0
Rule #2: 1 - 1 = 0
Rule #3: 1 - 0 = 1
Rule #4: 0 - 1 = 1
Binary Multiplication
Binary multiplication uses the same technique as decimal multiplication. In fact, binary
multiplication is much easier because each digit we multiply is either zero or one. All you have to
remember is that only 1 * 1 produces a product of 1, all the rest produces 0. Take a look at the basic
binary multiplication rules.
Rule #1: 0 * 0 = 0
Rule #2: 0 * 1 = 0
Rule #3: 1 * 0 = 0
Rule #4: 1 * 1 = 1
Binary Division
Let’s Expand!
Program design, sometimes called software design, is the process of developing how a
computer program should work or simply, designing how a program should respond to a set of
interrelated inputs. Program design is about writing a flow work that illustrates a sequence of options,
actions, and outputs. It is usually made in the form of a flow chart.
Algorithm
When designing a program, you must first outline the solution, and then develop that
solution into an algorithm. As a guide to outlining a solution, we usually answer the following
questions:
• What are the major steps involved?
• What are the major sub tasks involved?
• What are the major variables that affect decisions in the solution?
Once we have developed an initial outline, we can proceed to writing an algorithm for this
outline. An algorithm is a set of specific steps that describe the order and manner in which tasks shall
be performed to obtain a result or solve a problem.
Algorithms have five defining characteristics:
1. Algorithms are organized and follow a logical order. This means that we must write an
algorithm that has correct order or logic. A wrong order of operations may not work or may not
produce the desired result.
2. Algorithms have clear instructions. We must be very clear and simple on how we write our
algorithms. Our instructions must be very basic so that they no longer need to be simplified.
For example, if you have a list of letters that you want to arrange alphabetically, you
might write an instruction that says “sort these letters”. However, this is not clear to a computer
because it has no basic operation for sorting. When writing algorithms, we must stick to basic
operations, also known as primitive operations or simply primitives, which are the simplest form of
instructions that a computer can understand.
3. Algorithms have realistic or feasible instructions. When we write algorithms, remember that our
instruction must be feasible. Suppose you write an algorithm for cooking adobo, and your first
instruction says that you must gather all ingredients. You cannot execute this is if you have a
missing ingredient. In a computer, go to download an algorithm that demands the impossible
will not be executed. For example, mathematical operations that involve division by zero is
impossible and will not be executed by the computer.
4. Algorithms solve a problem or produce a result. The purpose of designing a program and
writing an algorithm is to solve a problem by producing a result. If the result satisfies the
problem, then the solution is good. If it does not, then the solution is not good.
5. Algorithms always end in a given amount of time. Our algorithms should have a finite number
of instructions. We must not write an algorithm that has no end. For example, if we want to
print all even integers, we might write an algorithm that appears like this:
2. Looping consists of instructions that are repeated a number of times to get a resolution.
There are different types of loops: repeat until, while, and for.
Repeat until - this is a loop that carries out instructions repeatedly, and stops when given
the until command.
Example
Problem: wash the glass windowpane
Algorithm: 1. Repeat
2.Wash with warm soapy water
3. Until the window is clear
While - this is a loop where instructions are carried out while the condition is true;
otherwise, the loop stops indicated by the end while statement.
Example:
Problem: to ask for a number less than 5, then stop when such a number is given.
Algorithm: 1. While number given is less than 5
2. Ask for a number less than 5
3. End while
For - this is a loop that keeps on carrying out a command or commands, for a given
number of times. In this type of loop the number of times the instructions are repeated must be
declared. The instructions to be repeated are placed in between the four and end for commands.
Example:
Problem: to print the numbers from one to 10
Algorithm: 1. For n =1 to 10
2. Print n
3. End for
In For loop, the value of N starts at one and goes up to 10. Its value increases by one
each time it passes through the loop until it gets to 10. Since the value of N changes or varies, the
number is called a variable.
3. Choosing or selection method is used if the problem requires decision-making. The if-
then or if-then-else command for this conditional statement. The if command is followed
by an action or instruction to be executed. If the condition is satisfied, it is followed with
the then command. Is the condition is not satisfied, it is followed by else command.
Example:
Problem: to decide to go home
Algorithm: 1. If the time is 5:00 PM or later than 5:00 PM
2. Then go home
Pseudocode
Pseudo codes are short phrases that describe the instructions of an algorithm. These are
supposed to be human readable phrases instead of computer codes. Using pseudo codes allows us
to concentrate more on the logic and organization of our codes instead of the syntax of a
programming language. It saves time and resources.
Rules In Writing Pseudo Codes
Suppose you want to design a program that will obtain 2 numbers from the user. The
program shall calculate and print the total of those numbers. Your pseudocode will look something
like this:
1. Prompt the user to enter the first number.
2. Prompt the user to enter the second number.
3. Calculate the sum of the two numbers.
4. Display the result.
In writing an algorithm, analyze first the problem and decide what is needed to do to solve
the problem. Use the top-down design where the steps is simply written down in the correct order.
Creating Pseudo Codes
It was already discussed that pseudo codes are short phrases that define instructions for
algorithms. Before writing algorithms, the program should be designed in pseudo codes. Pseudo
codes follow three logic structures or control structures called sequence, selection, and iteration.
1. Sequence is a control structure that performs the actions from top to bottom or one after
another. The instructions are written in an order. It is the simplest form of program design.
Example
1. Read the first number
2. Read the second number
3. Compute the sum of the two numbers
4. Display the result
Here are the common action keywords used to indicate input, output, and process.
Input: read, obtain, get
Output: print, display, show
Process
Compute: compute, calculate, determine
Initialize: set, init
Add one: increment
2. The selection control structure is indicated by using if then else statement.
Syntax:
IF [condition] THEN
Learning Modules in IT Page 8 of 11
[statement 1]
ELSE
[statement 2]
ENDIF
If the condition yields true, statement 1 will be performed, or else, statement 2 will be done.
Example:
ELSE
Display “You are a Minor”
ENDIF
3. Iteration or loop structure performs the action repeatedly while the condition returns true.
Syntax:
WHILE [condition]
[Statement]
ENDWHILE
Example:
Ask a number
ENDWHILE
The loop structure will ask the user a number until the given number is greater than 10. It
will stop asking if the number is lower than 10.
Let’s Ponder!
1. What do you think is the highest three-digit number you can have with the hexadecimal
system? Explain.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
2. In an octal system, how will you represent a higher number if you exhaust all the available
digits from 0 to 7?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
3. Why do we need to learn number system conversion?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
4. What is the advantage in programming when we learn number system conversion?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
5. Why is it important to learn mathematical operations applied to binary numbers?