Professional Documents
Culture Documents
Computers can't think, they just carry out commands, and if they think, it's us.
When thinking about calculations, we use the following concepts to get into the
problem:
1. If you are reading this, then you have opened this slide.
2. You are reading this.
3. So you've opened this slide. (Result)
Let's say one of your friends printed this slide on a piece of paper and gave it to you to
read. In this case, the logical argument is still valid because its form is correct, but the
result may be questionable because the first reasoning is incorrect.
This shows what logic is and what is not. Logic is not a magic that always gives you
the right answer, it is simply a formal system that turns many considerations into valid
results within the system.
Some deficiencies in formal logic
Confirming the result:
Consideration 1 consists of 2 parts: the beginning and the end. The result does not conform to the rules
of logic because here the IF-THEN relationship is reversed.
The first consideration is directly related to its outcome, but the opposite is never intended and therefore
cannot be accepted.
What is an algorithm?
We call an algorithm a systematic logical approach that allows a computer to solve
a problem, consisting of well-defined, step-by-step procedures.
Of course, this is not the only and perfect definition of the algorithm. However, if
we look at it in terms of a logical approach, it would be more appropriate to define
the algorithm as above.
If the solution of any problem can be expressed in the form of a set of executable
instructions , it is called an algorithmic problem.
Data Structures refer to the types of data used and how the data are organized in
the program.
You are required to design a complete system which will enable the sum of two values
to be calculated.
As the computer is also a device similar to the way in which the human brain functions,
the process of calculating the sum of two values can also be easily performed by the
computer.
Problem Solving
Problem Solving
Problem Solving
Problem Solving
As shown previously, the example values (5 and 10) have been specified explicitly. As the brain is
flexible enough in calculating a wide range of numbers, the two input values have to be
generalised.
Notice that instead of using specific numbers, variables are used to represent these values.
What Are Variables?
Variables are memory locations within the computer which allows pieces of data to
be stored. The word variable comes from the word vary, which means that whatever
you place within a variable can be changed. A variable can be viewed as a container
used to store things.
Problem Solving
Now that we have an exact idea about how the problem is solved, let us represent
this in a clearer manner, using the defining diagram.
Value1
? Sum
Value2
Problem Solving
The next step is to identify the actual processing steps required to convert the input to
become the output.
Once the defining diagram has been developed, the next logical step is to develop the
algorithm (which is much more detailed).
Algebraic operators
The basic mathematical operators used in algorithms are as follows:
+ addition
- subtraction
* multiplication
/ division
= assignment
Computer science textbooks often use pseudocode in their examples so that all
programmers can understand them, even if they do not all know the same
programming languages.
Pseudocode, by nature, exists in various forms, although most borrow syntax from
popular programming languages (like C, Lisp, or FORTRAN)
Control Structures
The key to better algorithm design and thus to programming lies in limiting the control
structure to only three constructs. These are illustrated below:
❏ Sequence Structure
❏ Selection (or Decision) Structure
❏ Repetition (or Iteration) Structure
Sequence Control Structure
The Sequence Control Structure:
The sequence control structure is a series of steps or statements that are executed in the order
in which they are written in an algorithm.
For Example:
For example, suppose you are required to design an algorithm for finding the average of six
numbers, and the sum of the numbers is given. The pseudocode will be as follows:
1. Start
2. Get the sum
3. Average = sum / 6
4. Output the average
5. Stop
The Selection Control Structure:
The selection control structure defines two courses of action, depending on the
outcome of a condition. A condition is an expression that, when evaluated, computes
to either true or false.
Syntax is:
Decision Making
Being able to mimic the way the human brain works, the computer also has the ability
to make decisions.
The expression is a comparison between two values which evaluates to either true or
false.
Example:
= Equals To
For example,
A potential employer is waiting for you to give a reply (on the spot) about the job offer
with a salary of RM2000. Your decision would be to only take a job worth more than
4000 $. What would you say?
Decision Making with compound expressions
Certain conditions may give rise to more than one expression being evaluated. These
are known as compound expressions.
Example:
You are interested in taking up a job which pays more than 4000 $ and that the
company must also provide a credit card.
Decision Making
● AND Every expression must evaluate to be true in order for the whole
expression to be true.
● OR As long as any one of the expression can be true, the entire IF
statement will be true.
● NOT The inverse (opposite) of the entire expression.
....... ensure that statements are properly indented to indicate block of statements
which belong together.
Repetition Control Structure
The repetition control structure specifies a block of one or more statements that are
repeatedly executed until a condition is satisfied.
Syntax is:
ENDFOR
Looping Constructs
You are required to develop a complete system which will allow the
total payroll to be calculated.
The moment the system receives an input value of -99, the system
is required to stop and display the total payroll.
Looping Constructs
Looping Constructs
READ letter
ENDWHILE
Looping Constructs
Example 2: Write an algorithm that will output the square of any number input until the number input is zero.
READ number
READ number
ENDWHILE
Looping Constructs
Sum = 0
READ n
FOR (I = 1, I < n , I = I + 1)
READ value
ENDFOR
Output sum
Looping Constructs
REPEAT...UNTIL - it runs the statements inside loop until we get the condition true. So while it is false, the
repetition will be continued.
Examle: Write an algorithm which displays all the positive even integer numbers until 100.
even = 2
REPEAT
DISPLAY even
even = even + 2
OUTPUT even
As humans are more inclined towards understanding diagrams and pictures rather than words,
pseudocodes tends to become tedious to understand if too lengthy. Program flowcharts,
because they are represented graphically, makes understanding easier.
Definition:
A flowchart is a graph consisting of geometrical shapes that are connected by flow lines.
The following are the commonly used symbols for drawing program flowcharts.
Program Flowcharts (example)
Program Flowcharts (example)
Sequence Structure
Selection Structure 1
Selection Structure 2
Repetition Structure
Program Flowcharts (exercise 1)
Flowchart: t
Program Flowcharts (exercise 2)
Determine Whether A Student Passed the Exam or Not: Flowchart:
Program Flowcharts (exercise 2)
Flowchart:
Summary of Main Teaching Points
NEXT CLASS:
Chapter 2: Introduction to Java
Brief history.
Introduction to execution of java programs.
Java compiler.
JIT, JVM, JRE, JDK.
Javac vs java commands.
.java vs .class extensions.