Procedural Programming

Download Report

Transcript Procedural Programming

Control Structures
(Structured Programming)
for procedural aspects
of programming
2 Programming Paradigms
1. OOP (Object Oriented Programming)
2. PP (Procedural Programming)
(though contents of methods & constructors
in OOP use PP)
Procedural Programming
Procedure (= processing = algorithm)
Steps to solve a problem:
1st step
2nd step
...
last step
Object Oriented Programming
a class includes:
1. STORAGE for object’s ATTRIBUTES (= variables)
- all objects of this class type HAVE these attributes
- but each object has different VALUES for these attributes
2. BEHAVIORS (= methods)
- all objects of this class type can DO these behaviors
(if requested)
= services which this type of object can provide
A program consists of…
[both PP programs & OOP programs]
0) Header code for
– packages, classes
– methods & their arguments / parameters
– classes to be imported from external library
(of commonly-used, pre-written
methods / procedures / functions)
1) Declarations
-
request memory locations for data storage
give a name & data type
to each of those locations
possibly give an initial value to be stored
(i.e., declare variables & constants & objects)
2) Action statements to be executed:
– operate on data
• Arithmetic (on numeric types)
• Concatenate (string types)
+
+
-
*
– move data around
• in memory
(assignment)
• memory to/from console/keyboard
(or a file, or window) (i.e., I/O)
=
/
%
3) Control statements to
control the order in which actions execute
- transfer control to somewhere
other than the next statement in sequence
- then afterwards, control (usually) goes back
to just after the transfer statement
Transfer control (options)
1. next statement (in physical sequence) (default)
2. skip 1+ actions - if some condition is FALSE
– or choose which action to do next from set of several choices,
based on the value of some control variable
3. loop around & do 1+ actions again (& again & again …)
– either a fixed number of times
– or until some event happens
(based on checking if some condition is TRUE)
4. call a method & return here when it’s done
– method itself does some actions
& it may transfer control further
5. go to some statement & do NOT return here when done
Control Structures
Traditionally:
1. sequence – the default
2. selection (decision, conditional execution)
3. repetition (looping)
Also:
4. call method (& go back)
5. goto / break / return (& not go back)
Sequence
1 statement after another,
in order (1st, 2nd, 3rd, ...)
[start at the top &
continue til you hit the end]
Selection (if-then-else)
DECISION:
determine which of
2 paths to follow
(1 or more statements
in each path)
Looping
Repeat 1+
statements
based on
whether a
specified
condition is
true or false
loop UNTIL condition
becomes true, then
stop looping
WHILE condition is
true, keep looping –
stop looping when false
Types of Loops
1. while loop
2. for loop
3. do until loop
4. do while loop
5. go to loop
(in most languages, incl. Java)
(in most languages, incl. Java)
(in some languages, NOT Java)
(in some languages, incl. Java)
[bad programming practice]
Combine the control structures
Stack them
or
Nest them