0% found this document useful (0 votes)
8 views

Cc102 Lesson 1

Uploaded by

jhegs tindugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Cc102 Lesson 1

Uploaded by

jhegs tindugan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

I.

Introduction to Computer Programming

A. Programming Concepts
• VARIABLE DECLARATION
• BASIC SYNTAX
• DATA TYPE AND STRUCTURE
• FLOW CONTROL STRUCTURES (CONDITONAL ABD LOOPS)
• FUNCTIONAL PROGRAMMING
• OBJECT-ORIENTED PROGRAMMING
• DEBUGGING
• IDEs AND CODING ENVIRONMENT

- VARIABLE DECLARATION

Variables are containers for storing data values, a memory location for a
data type. Variables are created using a declaration or keyword that varies
across languages.

Variable names are usually alphanumeric, that is, they contain a-z and 0-9.
They can also include special characters like underscore or the dollar sign.

Variables can hold values of any data type supported by the programming
language. This value may change during program execution.
- BASIC SYNTAX

Every programming language has its syntax, and you must learn the
fundamental syntax of the language you are learning.

Syntax refers to the set of rules that define the structure of a language. It is
almost impossible to read or understand a programming language without
its syntax.
- DATA TYPES AND STRUCTURES

Data types refer to the classification of data. The most common data types
include:

• String
• Boolean (true or false)
• Numbers, which includes integers (whole numbers from 1) and floating-
point numbers (decimal-base)
• Characters (includes single alphabets or numbers)
• Arrays (a collection of data, usually of the same data type)

A Data Structure is a collection of data values. These structures include


operations that can be applied to that data. Data structures are important in
computer programming for organizing, managing, and storing data quickly and
efficiently.

Some common types of data structures include:

• Stacks
• Heaps
• Trees
• Linked lists
• Queues
• Arrays
• Tables
• Graphs

- FLOW CONTROL STRUCTURES

Flow Control Structures are the fundamental components of computer


programs. They are commands that allow a program to “decide” to take one
direction or another.

There are three basic types of control structures: sequential, selection, and
iteration.

Sequential
The most basic control flow is sequential control flow. It involves the execution
of code statements one after the other. A real-world example is following a
cooking recipe.
Selection (conditionals)

The basic premise of selection flow control is, the computer decides what action
to perform based on the result of a test or condition equalling true or false.
Iteration (Loops)

A loop is a programming structure that allows a statement or block of code to


be run repeatedly until a specified condition is no longer true (will return
Boolean, true or false). It is one of the most powerful and fundamental
programming concepts.

- FUNCTIONAL PROGRAMMING

Functions are containers that take in a set of inputs and return an output. It is
not required for a function to return a value. Pure functions will always give the
same result for the same set of inputs.

Functional Programming is a straightforward method of building software that


involves using pure functions. This method eliminates the occurrence of data
mutation or side effects.
- OBJECT-ORIENTED PROGRAMMING

Object-Oriented Programming (OOP) is a programming concept that revolves


around ‘objects’ and ‘methods’.

There are four principles of OOP:

• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

- DEBUGGING

Debugging is a crucial skill. It involves detecting and removing existing and


potential errors, defects, or ‘loopholes’ in one’s code.

- IDEs AND CODING ENVIRONMENT

IDE stands for Integrated Development Environment – they are applications


programmers use to write code and organize text groups. It increases a
programmer’s efficiency and productivity, and has added features like code
completion, code compilation, debugging, syntax highlighting, etc.

Some common examples of IDE’s are:

• Visual Studio code


• IntelliJ IDEA
• NetBeans
• Eclipse
B. Generations of Programming Language

There are five generations of Programming languages. They are:


First-Generation Languages :
These are low-level languages like machine language.
Second-Generation Languages :
These are low-level assembly languages used in kernels and hardware drives.
Third-Generation Languages :
These are high-level languages like C, C++, Java, Visual Basic, and JavaScript.
Fourth Generation Languages :
These are languages that consist of statements that are similar to statements in the human
language. These are used mainly in database programming and scripting. Examples of these
languages include Perl, Python, Ruby, SQL, and MatLab(MatrixLaboratory).
Fifth Generation Languages :
These are the programming languages that have visual tools to develop a program. Examples of
fifth-generation languages include Mercury, OPS5, and Prolog.

The first two generations are called low-level languages. The next three generations are called
high-level languages.

1. First-Generation Language :

The first-generation languages are also called machine languages/ 1G language. This language is
machine-dependent. The machine language statements are written in binary code (0/1 form)
because the computer can understand only binary language.

Advantages :

1. Fast & efficient as statements are directly written in binary language.


2. No translator is required.

Disadvantages :

1. Difficult to learn binary codes.

2. Difficult to understand – both programs & where the error occurred.

2. Second Generation Language :

The second-generation languages are also called assembler languages/ 2G languages. Assembly
language contains human-readable notations that can be further converted to machine language
using an assembler.

Assembler – converts assembly level instructions to machine-level instructions.

Programmers can write the code using symbolic instruction codes that are meaningful
abbreviations of mnemonics. It is also known as low-level language.

Advantages :

1. It is easier to understand if compared to machine language.

2. Modifications are easy.

3. Correction & location of errors are easy.

Disadvantages :

1. Assembler is required.

2. This language is architecture /machine-dependent, with a different instruction set for different
machines.

3. Third-Generation Language :

The third generation is also called procedural language /3 GL. It consists of the use of a series of
English-like words that humans can understand easily, to write instructions. It’s also called High-
Level Programming Language. For execution, a program in this language needs to be translated
into machine language using a Compiler/ Interpreter. Examples of this type of language are C,
PASCAL, FORTRAN, COBOL, etc.

Advantages :

1. Use of English-like words makes it a human-understandable language.


2. Lesser number of lines of code as compared to the above 2 languages.

3. Same code can be copied to another machine & executed on that machine by using compiler-
specific to that machine.

Disadvantages :

1. Compiler/ interpreter is needed.

2. Different compilers are needed for different machines.

4. Fourth Generation Language :

The fourth-generation language is also called a non – procedural language/ 4GL. It enables users
to access the database. Examples: SQL, Foxpro, Focus, etc.

Advantages :

1. Easy to understand & learn.

2. Less time is required for application creation.

3. It is less prone to errors.

Disadvantages :

1. Memory consumption is high.

2. Has poor control over Hardware.

3. Less flexible.

5. Fifth Generation Language :

The fifth-generation languages are also called 5GL. It is based on the concept of artificial
intelligence. It uses the concept that rather than solving a problem algorithmically, an
application can be built to solve it based on some constraints, i.e., we make computers learn to
solve any problem. Parallel Processing & superconductors are used for this type of language to
make real artificial intelligence.

Examples: PROLOG, LISP, etc.

Advantages :

1. Machines can make decisions.


2. Programmer effort reduces to solve a problem.

3. Easier than 3GL or 4GL to learn and use.

Disadvantages :

1. Complex and long code.

2. More resources are required & they are expensive too.

C. Programming Process

The Programming Process

All programming involves creating something that solves a problem. The problems
can range from something of great scientific or national importance, through to
something as trivial as relieving personal boredom!

This section describes one approach to solving such problems - think of it as a rough
guide to the things you should do when entering the land of programming.

In broad terms, those things are:

1. Identify the Problem


2. Design a Solution
3. Write the Program
4. Check the Solution

Of these, only the third step is usually called "programming", but as you'll see later,
it's probably the least important stage of the process.

Identify the Problem

In fact, this stage should really be called identifying the solution because what you're
really trying to do is to tie down exactly what it is that you're trying achieve.

There are two stages to identifying a solution:

• Requirements
• Specification
Requirements

The first step is to examine the problem carefully to try to identify what qualifies as a
solution. A single problem may have many different solutions, but they will all have
something in common. So here you're trying to work out exactly what your program
will be required to do.

For example, if we were asked to write a calculator program, we could choose many different
ways for the user to enter calculations - from entering equations, pressing buttons or even
writing them on the screen - but if the software can't add up correctly then it won't have
solved the problem. Therefore our first few requirements must be that:

• the user can enter sums (we don't care how they do this)
• and that the program will then evaluate those sums correctly
and display the result for the user.

We also have to decide what sort of sums our calculator will be required to evaluate. Again,
we have a fair amount of choice - we could be ambitious and ask it to solve simultaneous
equations or complex expressions, however since this is our first program we should probably
make the requirements more simple. So the third requirement is that:

• The calculator must be able to evaluate sums made up of two whole numbers (integer
operands) and one addition (+), subtraction (-), multiplication (*) or division (/) sign
(operator).

Note that computer scientists traditionally use * instead of x and / instead of � to indicate multiplication and
division respectively.

Thus our calculator must be able to deal with sums like 1 + 1, 10 - 6, 43 * 5 and 42 / 7.
However it won't have to handle 67.345 + 6¼, the cube root of PI or 152.

Specification

The second step is to then look at the list of requirements and to decide exactly what
your solution should do to fulfil them. As we mentioned above, there are usually
many different solutions to a single problem; here, your aim is to decide on which of
those solutions you want. Therefore, you're trying to specify, in a fairly accurate
manner, just what it is your final program will do.

For example, for the calculator, we've already decided that the program must allow us to enter
simple sums and then must evaluate them correctly and display an answer. We must now tie
down exactly what this means.
Therefore, we have to decide which method of entering sums to use. We could specify any
one of a number of methods, but for now, we'll choose a simple method. We should also
specify what other behaviour we're expecting the program to have:

• When the program runs it will display a welcome message, followed by some simple
instructions.
• The program will then display a prompt sign ([number]>) and the user can then type the
first number of their sum at the keyboard followed by the RETURN (<-') key.
• The program will display a second prompt sign ([+-/*]>) and the user can then enter the
operator that they wish to use, followed by RETURN.
• A third prompt sign will be displayed ([number]>) and the user will then enter the second
number, again followed by RETURN.
• The calculator program will then display the mathematically correct answer to the sum on
the screen and end.

By the time you have worked out your specification, you should have a very clear idea
of what your final program will do: your goal.

Design a Solution

Once you've identified the things required to solve your problem, and specified what
form your solution will take, the next step is to work out just how you're going to turn
that specification into a working program. This is usually the hardest task!

As mentioned before, a program is simply a list of steps describing to the computer


what it should do. A design is simply a higher-level description of those steps. In
effect it's a program written as if the computer was a person. So, it doesn't have to
completely spell out every step - because humans know how to do a lot of things
already and have a lot of common sense, meaning that they can work the simple steps
out for themselves. It also doesn't have to be written in any special programming
language - English will do (although people often use special notations like
pseudocode or flow charts for the more complicated sections).

Another way of looking at is that a programmer should be able to take a design and
write the program from it without having to think too hard. It's a bit like an architect's
drawing: it contains all the important structures without showing every bit of brick
and mortar.

Working out a design to fulfil a particular specification can be difficult for several
reasons:
1. You may need to learn a bit more about the capabilities of your computer and
your chosen programming language/environment to see what things it makes
easy or difficult.
2. You may also need to learn some extra information about the problem or find
a technique to solve it before you can work out how to build the program.
3. Finally, you may be able to think of several ways to build the program, but
they will all have different strengths and weaknesses and so some choices will
have to be made.

We'll return to these problems a bit later on in the course.

For our calculator, we have a fairly comprehensive specification and since it is a fairly simple
program we can turn the that quite easily into a design:

1. BEGIN
2. PRINT welcome message
3. PRINT instructions
4. PRINT [number]>
5. READ first_number
6. PRINT [+-/*]>
7. READ the_operator
8. PRINT [number]>
9. READ second_number
10. calculate result of using the_operator on
the first_number and the second_number
11. PRINT result
12. END

Here we assume that PRINT means 'put something on the screen' and READ means 'get something typed on the
keyboard' - both fairly standard programming operations.

Notice how step ten is actually hiding quite a complicated procedure. Although we (as
humans) could work out which operator was which and do the appropriate arithmetic, the
computer itself will need to be told exactly how to do this - but we'll leave that until the
programming stage.

Notice also how the design includes all of the important steps needed to fulfil our
specification - but that doesn't go into too much unnecessary detail. This is called abstraction.

When your design is completed you should have a very clear idea of how the
computer is going to fulfil your specification, which in turn meets your requirements,
which in turn should solve your original problem.
Program

Programming is then the task of describing your design to the computer: teaching it
your way of solving the problem.

There are usually three stages to writing a program:

1. Coding
2. Compiling
3. Debugging

Coding

Coding is the act of translating the design into an actual program, written in some
form of programming language. This is the step where you actually have to sit down
at the computer and type!

Coding is a little bit like writing an essay (but don't let that put you off). In most cases
you write your program using something a bit like a word processor. And, like essays,
there are certain things that you always need to to include in your program (a bit like
titles, contents pages, introductions, references etc.). But we'll come on to them later.

When you've finished translating your design into a program (usually filling in lots of
details in the process) you need to submit it to the computer to see what it makes of it.

As an example, we shall develop and present the code for the calculator later on.

Compiling

Compilation is actually the process of turning the program written in some


programming language into the instructions made up of 0's and 1's that the computer
can actually follow. This is necessary because the chip that makes your computer
work only understands binary machine code - something that most humans would
have a great deal of trouble using since it looks something like:
01110110
01101101
10101111
00110000
00010101

Early programmers actually used to write their programs in that sort of a style - but
luckily they soon learnt how to create programs that could take something written in a
more understandable language and translate it into this gobbledy gook. These
programs are called compilers and you can think of them simply as translators that can
read a programming language, translate it and write out the corresponding machine
code.

Compilers are notoriously pedantic though - if you don't write very correct programs,
they will complain. Think of them as the strictest sort of English teacher, who picks
you up on every single missing comma, misplaced apostrophe and grammatical error.

Debugging

This is where debugging makes it first appearance, since once the compiler has looked
at your program it is likely to come back to you with a list of mistakes as long as your
arm. Don't worry though, as this is perfectly normal - even the most experienced
programmers make blunders.

Debugging is simply the task of looking at the original program, identifying the
mistakes, correcting the code and recompiling it. This cycle of code -> compile ->
debug will often be repeated many many times before the compiler is happy with it.
Luckily, the compiler never ever gets cross during this process - the programmer on
the other hand...

It should also be said at this point that it isn't actually necessary to write the entire
program before you start to compile and debug it. In most cases it is better to write a
small section of the code first, get that to work, and then move on to the next stage.
This reduces the amount of code that needs to be debugged each time and generally
creates a good feeling of "getting there" as each section is completed.

Finally though, the compiler will present you with a program that the computer can
run: hopefully, your solution!

Solution!

The final step in the grand programming process is that of testing your creation to
check that it does what you wanted it to do. This step is unfortunately necessary
because although the compiler has checked that your program is correctly written, it
can't check whether what you've written actually solves your original problem.

This is because it is quite possible to write a sentence in any language that is perfectly
formed with regards to the language that it's written in (syntacticly correct) but at the
same time be utter nonsense (semantically incorrect). For example, 'Fish trousers go
sideways.' is a great sentence - it's got a capital letter and a full stop - but it doesn't
mean a lot. Similarly, 'Put the ice cube tray in the oven.' has verbs and nouns and so
on - but it's pretty useless if you wanted to make ice cubes.

So your program needs to be tested, and this is often initially done informally (or
perhaps, haphazardly) by running it and playing with it for a bit to see if it seems to be
working correctly. After this has been done, it should also be checked more
thoroughly by subjecting it to carefully worked out set of tests that put it through its
paces, and check that it meets the requirements and specification - but we shall
discuss this more later on in the course.

Where mistakes are identified, it is a case of donning a Sherlock Holmes hat and
trying to figure out where in the code the mistake is. Once identified, the problem
should be fixed by changing the code and recompiling. Care should be taken at this
point that this fix doesn't break something else, so careful retesting is important. This
process is also known as debugging.

Once all the testing and debugging has been completed, you should be pretty certain
that your program works according to your requirements and your specification and so
you should finally have a solution to your problem!

Easy isn't it?!

Summary

1. Identify the Problem - What Are You Trying To Do?


o Requirements
o Specification
2. Design a Solution - How Is It Going To Be Done?
3. Write the Program - Teaching the Computer
o Code
o Compile
o Debug
4. Check the Solution - Testing it Understands You

While this may sound like a great deal of effort to go to to build a simple program,
don't worry, as after a while it will become second nature, and for small programs,
most of the stages can be done in your head.
The next section will introduce you to your first programming language, and your
very first program!

You might also like