Procedural Programming - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Procedural Programming

Description:

Procedural Programming – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 37
Provided by: donnaka1
Category:

less

Transcript and Presenter's Notes

Title: Procedural Programming


1
Procedural Programming Structured Programming
  • Control structures
  • Sequence the default
  • Conditional in these slides
  • Looping in other slides

2
Procedural Programming
  • OOP vs. PP (Procedural ) paradigm ?
  • - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - -
  • OOP class packaging of
  • 1) storage for attributes of objects of this
    class type
  • 2) behaviors (methods) of objects of this class
    type
  • Methods ( constructors get/set accessors)
  • procedural programming

3
Procedural Programming
  • Procedure (algorithm)
  • steps to take to solve a problem
  • 1st . . .
  • 2nd . . .
  • . . .
  • last . . .

4
Program/method consists of
  • 1) Action statements to execute
  • operate on data
  • Arithmetic (on numeric types) - /
  • Concatenate (string types)
  • move data around
  • in memory (assignment)
  • memory to/from screen/keyboard (or a file, )
  • WriteLine, ReadLine

5
  • 2) statements to Control the order in
    which actions execute
  • transfer control
  • To next statement (in physical sequence) ( the
    default)
  • Possibly skip an action(s), based on a condition
  • Choose next action from several, based on a
    condition
  • Loop around do actions again
  • A fixed number of times
  • A variable number of times, based on if condition
    is true or false
  • Call a method (a procedure) return here when
    its done
  • Method itself does actions may transfer control
    further
  • Go to some statement dont return when done

6
3 control structures
  • Structured Programming
  • Sequence (the default)
  • Selection (conditional execution)
  • Repetition (looping)

7
Sequence
1 statement after another, in order (1st,
2nd, 3rd, ...) start at the top continue til
you hit the end
8
Selection (if-then-else)
DECISION determine which of 2 paths to follow
(1 or more statements in each path)
9
Loop
Repeat 1 statements based on whether specified
condition is true or false also a for loop
UNTIL there are no more students
WHILE there are more students
10
Combine the 3 structures
  • Stack them
  • or
  • Nest them

11
Selection options (in C)
  • selection guard, decision, conditional
  • Single selection if
  • Double selection if ... else
  • Multiple selection
  • switch (case in some languages)
  • or
  • if else if else if else

12
Repetition options (in C)
  • Event-controlled
  • (loop as long as condition is true)
  • while (pre test)
  • do . . . while (post test)
  • Count-controlled
  • (loop a specified number of times)
  • for
  • foreach

13
Selection Control Structure
  • if, if/else
  • conditions

14
Plain ifs (3 variations)
  • if (condition)
  • action
  • ------------------
  • if (condition)
  • action
  • if (condition)
  • action1
  • . . .
  • actionN

15
if else (3 variations)
  • if (condition)
  • action1
  • else
  • action2
  • ------------------
  • if (condition)
  • action1
  • else
  • action2
  • if (condition)
  • action1a
  • . . .
  • action9a
  • else
  • action1b
  • . . .
  • action9b

16
Each action could be
  • A simple action
  • Assignment state
  • with arithmetic expression or function call
  • I/O
  • Call to another method
  • Another selection statement if or if. .
    .else or switch
  • A while or do while or for loop
  • do absolutely NOTHING

17
Conditions
  • Comparison (equality, relational) operators
  • ! lt gt lt gt
  • NOTE compare for equality
  • is the assignment operator
  • Compare 2 things (2 operands) which are
  • - variables, constants, arithmetic expressions,
  • returned values from a method call,
  • . . .

18
Conditions are true or false
  • (a lt b)
  • (a ! 25)
  • ( (a 2 3) lt ( (b - 4) 3) )
  • ( (Math.PI r r) lt maxSize )
  • NOTE need ( ) around whole condition

19
Logic operators in conditions
  • (and) (or) !
    (not)
  • ( !(a 25) ) same as (a ! 25)
  • ( (a lt b) (c lt d) )
  • ( (a 3) (c 1) )
  • Note use Truth tables

20
Order of precedence ?
  • ( (a b) (c gt -14) ) typical
  • (a b) (c gt -14) WRONG
  • ( a b c gt -14 ) OK
  • students with gpas gt 3.0 from Michigan and
    Ohio? NO
  • (gpa gt 3.0 state MI state
    OH)
  • ( (gender F) (age gt 21)
  • (gpa gt 2.5) (!(major CS)) )

21
Order of precedence of operators
  • Unary operators - !
  • Arithmetic operators /
  • -
  • Relational operators lt gt lt gt
  • equality operators !
  • Logic operators AND
  • OR
  • Assignment operator

22
Actions e.g.,
  • total total 1
  • counter
  • Console.WriteLine(blah blah)
  • n Convert.ToInt32
  • ( Console.ReadLine() )
  • if (state MI)
  • . . .
  • if (state MI)
  • . . .
  • . . .
  • else
  • . . .
  • // meaning do NOTHING !!!

23
Empty block of actions
  • UNUSUAL (bad?)
  • if (a lt b)
  • Console.Write(hi)
  • // empty if so Write will ALWAYS happens
  • COMMON
  • if (a lt b) // NO here
  • Console.Write(hi)
  • // empty if so Write MAY happen

24
Nested if/else
  • if (a 4) // note formatting
  • if (b 5)
  • answer 1
  • else
  • answer 2
  • else
  • if (b 5)
  • answer 3
  • else
  • answer 4
  • // trace this code !

25
Empty statement
  • if (a 4)
  • if (b 5)
  • // OK, do nothing here
  • else
  • answer 2
  • else
  • if (b 5)
  • answer 3
  • else
  • answer 4

26
Dangling else ?
  • if (a 4)
  • if (b 5)
  • answer 1
  • // WRONG ?
  • else // this else paired with if (b5)
  • if (b 5)
  • answer 3
  • else
  • answer 4
  • // NOTE compiler ignores formatting and
    does what instructions actually say

27
Prior example actually says
  • if (a 4)
  • if (b 5)
  • answer 1
  • else // so b ! 5 here
  • if (b 5)
  • answer 3
  • else
  • answer 4

28
Dangling else - the FIX
  • if (a 4)
  • if (b 5)
  • answer 1
  • else
  • if (b 5)
  • answer 3
  • else
  • answer 4

29
Nested if/else if/else . . .
  • NOT TYPICAL FORMATTING
  • if (total gt 90)
  • Console.WriteLine(A)
  • else
  • if (total gt 80)
  • Console.WriteLine(B)
  • else
  • if (total gt 70)
  • Console.WriteLine(C)
  • else
  • if (total gt 60)
  • Console.WriteLine(D)
  • else
  • Console.WriteLine(E)

30
Nested generally written as
  • if (total gt 90)
  • Console.WriteLine(A)
  • else if (total gt 80)
  • Console.WriteLine(B)
  • else if (total gt 70)
  • Console.WriteLine(C)
  • else if (total gt 60)
  • Console.WriteLine(D)
  • else
  • Console.WriteLine(E)

31
Nested if/elsesvs. Stacked ifs
  • NESTED if/elses
  • control goes to ONLY 1 category (condition)
  • ONLY 1 set of actions is done (or none)
  • used for these situations
  • mutually exclusive categories - state example
  • use 1st category that applies - grades example
  • STACKED ifs
  • control goes to ALL conditions)
  • ALL sets of actions MIGHT be done
  • used for these situations
  • use all categories that apply - cummulative
    bonus example

32
Nested if/elses
  • control goes to ONLY 1 category
  • the 1st one that applies
  • and none of the rest of the else if s
  • so the ORDER may be important
  • NO for mutually exclusive categories - state
    example
  • YES for use 1st category that applies - grades
    example
  • if (state MI)
  • . . .
  • else if (state IN)
  • . . .
  • NOTE final else is optional

33
Stacked ifs
  • bonus 0
  • if (attendance gt 25)
  • bonus bonus 5
  • if (labPoints gt 900)
  • bonus bonus 35

34
Stacked - WRONG
  • if (total gt 90)
  • Console.WriteLine(A)
  • if (total gt 80)
  • Console.WriteLine(B)
  • if (total gt 70)
  • Console.WriteLine(C)
  • if (total gt 60)
  • Console.WriteLine(D)
  • Console.WriteLine(E)

35
if/else in a box
  • Special conditional operator ?
  • Console.Write (age gt 21 ? OK NO)
  • 1st Condition ? TRUE or FALSE
  • 2nd VALUE of parameter when condition is TRUE
  • 3rd VALUE of parameter when condition is FALSE

36
Switch statement
  • Equivalent to nested if/else
  • Later
Write a Comment
User Comments (0)
About PowerShow.com