ALGORITHM
Week02 – Computer Programming I
Instructor: Jennifer G Rabang
Algorithms
The solution to any computing problem involves
executing a series of actions in a specific
order. A procedure for solving a problem in terms
of
1. the actions to be executed, and
2. the order in which these actions are to be
executed
is called an ALGORITHM.
RISE-AND-SHINE ALGORITHM
• Get out of bed • Get out of bed
• Take off pajamas • Take off pajamas
• Take a shower • Get dressed
• Get dressed • Take a shower
• Eat breakfast • Eat breakfast
• Go to school • Go to school
Specifying the order in which statements are to be
executed in a computer program is called program
control.
Pseudocode
Is an artificial and informal language that
helps programmers develop algorithms.
It is similar to everyday English; it is
convenient and user-friendly although it is
not an actual programming language.
It is not executed in computers
Example: get the value of A
Flowchart
Is a graphical representation of an
algorithm or of a portion of an algorithm.
Use special-purpose symbols such as
rectangles, diamonds, ovals, and small
circles etc.
Is the blueprint of the program
Basic Symbols used in
Flowcharting
Start / End Used to signify the beginning and end of
TERMINAL flowchart
Indicates the preparation of data, used to
select initial condition
INITIALIZATI
ON
Shows input and output
INPUT/OUTP
UT
Performs any calculations that are to be
done
PROCESS
Two alternative execution paths are possible.
The path to be followed is selected during the
execution by testing whether or not the
DECISION condition specified within the outline is fulfilled.
Shows the entry or exit point of the
ON PAGE flowchart
CONNECTOR
Designates entry to or exit from one page
when a flowchart requires more than one
OFF PAGE page.
CONNECTOR
Signifies the process that is to be executed
next
FLOW LINES
Programming
Programming is the act or process of planning or
writing a program.
A computer program (also a software program,
or just a program) is a sequence of instructions
written to perform a specified task for a computer.
A programming language is an artificial language
designed to express computations that can be
performed by a machine, particularly a computer.
Programming languages can be used to create
programs that control the behavior of a machine
and/or to express algorithms precisely.
Identifiers
are the names you supply for variables, types,
functions, and labels in your program
must differ in spelling and case from any
keywords. You cannot use keywords as
identifiers; they are reserved for special use. You
create an identifier by specifying it in the
declaration of a variable, type, or function.
Identifiers are composed of a sequence of
letters, digits and the special character _
(underscore).
Keywords
• keyword: An identifier that you cannot use
because it already has a reserved meaning in
Java. PYTHON
abstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return
transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized
Variables
Variables are identifiers that can store a
changeable value. These can be different
data types.
Is a location in memory where a value can
be stored for use by a program.
We have to declare all variables and
constants, before we can use them in our
main program.
Data Types
• Data type is a data storage format that can
contain a specific type or range of values.
Rules for defining or
naming variables:
1. It must consist only letters, digits and
underscore.
_total, num_1
2. It should not begin with a digit
1name, 3to3, 4
3. Keywords are not allowed to be a variable
float, default
4. It is a case sensitive
Ans ≠ ans
5. Do not include embedded blanks (white
space)
first name
6. Do not call your variable by other name as other
functions.
main
Variable Declaration
• SYNTAX
Data type variable terminator (;)
• EXAMPLE
int _total;
float num1, ex_2, file;
char letter;
Operators
• Operator is a symbol that tells the
computer to perform specific mathematical
or logical manipulations.
• 3 classes of Operators
1. Arithmetic
2. Relational and Logical
3. Bitwise
Arithmetic Operators
Operator Action Sample
+ addition 5+3 = 8
- subtraction 5-3=2
* Multiplication 5*3=15
/ Division 5/3 = 1
% Modulus 5%3 = 2
-- Decrement
++ Increment
RELATIONAL OPERATORS (T/F)
OPERATORS MEANING SAMPLE
> Greater than 5>3
>= Greater than or 5>=5
equal
< Less than 7<10
<= Less than or equal 7<=9
== Equal 5==7
!= Not equal 5!=6
LOGICAL OPERATORS (T/F)
OPERATORS MEANING
&& AND
|| OR
! NOT
TRUE (1) |FALSE (0)
• OR (||) at least one of the conditions is true
• AND (&&) two conditions must be true
A B A&&B A||B !A !B
1 1 1 1 0 0
1 0 0 1 0 1
0 1 0 1 1 0
0 0 0 0 1 1
PYTHON
Converting Mathematical Formula to JAVA
Expression`
• To solve mathematical problems using the
computer, the formula should be translated
to the programming language to be used.
• Example:
b2 – 4ac b*b-4*a*c
a+b (a+b)/(c+d)
c+d
x3 x*x*x
Sample Program Problem
• Write a program that will ask the user to enter
two variables. Get the sum of this two
variables and display the result.
Strategy
Feeding a data Processed data
Input PROCESS Output
Manipulation
Calculation
Computation
Input
Reviewing the problem, the program requires
two variables need to be entered by the user.
Write a program that will ask the user to enter two
variables. Get the sum of this two variables and display the
result.
Answer:
Get variables from the user
Process
After the user enter two variables, the
program needs to get the sum of the two
variables. This means that the computation
will be addition.
Answer:
Sum = number1 + number2
Output
• The problem requires to display the output
Pseudocode
Step1: Enter two variables for
number1 and number2.
Step2: Add the variables
Step3: Display the sum.
Start
You may create software
diagrams like flowchart
using Lucidart online. All
you have to do is to create
or sync your corportate
account so that you can
draw them.
End