Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Computer Programming 2

INTRODUCTION

A computer programming is a way of giving computers instructions about


what they should do next. These instructions are known as code, and
computer programmers write code to solve problems or perform a task. The
end goal is to create something: that could mean anything from a web page,
or a piece of software, or even just a pretty picture. That’s why computer
programming is often described as a mix between art and science; it’s
technical and analytical, yet creative at the same time.
What is Computer Programming?
A computer itself is not smart. They are powerful and have the
potential to carry out tasks much faster than a human. But computers
need a human to write instructions and tell them what to do.
Therefore, programming is the process of writing those instructions.
We use a programming language to do this. These instructions are
translated to a readable format which a computer can understand. The
instructions are then carried out by the computer.
Programming how to make a cup of tea
Let’s take making a cup of tea as an example. If you were to give
instructions on how to make a cup of tea, it would look like the following:

1. Boil some water

2. Pour hot water in a cup with a teabag

3. Let the tea brew

4. Remove the teabag

5. Add milk and/or sugar (if desired) as simple as that!


What we take for granted is that communication with a human being is
different than communicating with a computer. A human has prior knowledge
and life experience – they may know where to find the tea. We assume they
know that milk is stored in a fridge.

Humans also have intuition. If you can't find a cup you might then search
the cupboards instead. There’s also reading people’s non-verbal cues like
body language.
When it comes to programming, you have to be very specific. Continuing
with how to make a cup of tea, you might write instructions
in pseudocode like this:

1. Go to the kitchen
2. Locate the kettle
3. Open the lid of the kettle
4. Fill the kettle with water
5. Turn the kettle on
6. Wait for it to boil to 100 degrees Celsius
7. Find a cup
What if instructions like the ones above are not enough? You may
need to add some logic to account for all scenarios. For example: 2)
Locate the kettle. Well, is it an electric kettle or is it a kettle you put on
a hob? You’ll need to add a condition that if it is an electric kettle, then
do xyz. Otherwise, do xyz for a kettle that you put on a hob.
Even when you think that you’ve accounted for every possible
condition and given very specific instructions, there are things that you
may not foresee. You start making your cup of tea and something
goes wrong.
What happened? There’s a bug in your code! A bug is an error or flaw in your code
which might lead to unexpected results. In order to fix your code, you go through a
process of “debugging”, which is where you find the problems in your code and resolve
the issues. In this case, your instructions didn’t include filling up your kettle to 0.8 liters
to cover the heating element. So, the kettle switches off as a safety measure.

To prevent errors from happening after you run your program, developers carry out
testing and unit-testing on their programs. Unit-testing is where you write tests for
parts of your code. The tests either fail or pass.
For example, you write a function which adds two numbers:

1 + 1. You then write a unit test where the expected output is 2. All answers
will fail unless it's 2.

You go through your code until everything runs without any unexpected
problems. Programming is therefore a detailed oriented and iterative
process where you are continuously improving what you have previously
written.
How does your computer understand your code?
What most programmers write as “code” is a high level programming language. It
is abstract by design. Abstraction in this context means that we are moving further
away from machine code and programming languages are closer to spoken languages.

But a computer can’t understand text-based code. It needs to be compiled


(translated) into machine code. Machine code is a set of instructions which can be
understood by a computer’s central processing unit (CPU). Think of the CPU as the
brain of a computer. Machine code is made up of ones and zeros. This is called binary.
For example, this is how you would write “Hello World” in binary:

01001000 01100101 01101100 01101100 01101111 00100000 01010111


01101111 01110010 01101100 01100100

As you can see, binary is not easily readable for humans, so we tend to
avoid programming in machine code!
What exactly is a programming language?

Programming languages fall both within the spectrum of low-


level languages, such as assembly, and high-level programming
languages, such as JavaScript.
What exactly is a programming language?
But what is a programming language exactly? The best analogy for this
is the spoken language we use today. All languages express the same idea,
but in different ways to another person:

English: Hello
French: Bonjour
Spanish: Hola
Programming languages are different ways of expressing the same idea,
but to a computer instead. The following will print out “Hello” in three different
programming languages:

JavaScript: alert(“Hello”);
Python: print(“Hello”)
Perl: print "Hello";

Each programming language has its own syntax. In English, we have


grammar. The same applies to programming languages – they each have
their own set of rules.
Thank You
Prepared by: Miss Marie Celia R. Aglibot

You might also like