Arithmetic Expressions - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Arithmetic Expressions

Description:

Evaluate to string values ... See table 4.6 in text book (page 171) for precedence rules. ... There may be rare and obscure reasons to use a goto, but ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 31
Provided by: stude442
Category:

less

Transcript and Presenter's Notes

Title: Arithmetic Expressions


1
Arithmetic Expressions
  • Used to do arithmetic.
  • Operations consist of , -, , /, , unary -,
    and function calls.
  • Evaluate to real or integer values.

2
String Expressions
  • Used to do string manipulation.
  • Operations consist of string methods
    ("object-oriented functions") and regular
    functions.
  • Evaluate to string values
  • String comparison is character-by-character based
    on ASCII values (see appendix A in textbook)
    including the null terminator. So 10 lt 2.

3
Relational Expressions
  • Used to test the vales of two arithmetic
    expressions against each other, or to test the
    vales of two string expressions against each
    other.
  • Operations consist of gt, lt, , !, lt, gt.
  • Evaluate to boolean values (ie, true or false).

4
Boolean Expressions
  • AKA logical expressions
  • Used to combine boolean values.
  • Operations consist of (and), (or), and !
    (not).
  • Evaluate to boolean values (ie, true or false).
  • See tables 4.3-4.5 in text book (page 170) for
    definitions of boolean operators.

5
Conditions
  • Operations consist of arithmetic, string,
    relational, and boolean operators.
  • Evaluate to boolean values (ie, true or false).
  • See table 4.6 in text book (page 171) for
    precedence rules.
  • Short-Circuit Evaluation evaluation of
    expression stops when value of expression is
    determined.

6
Basic Logic Control Structures
  • Sequence
  • Selection
  • Iteration

7
Sequence
  • Execution starts at the top of a block, and
    execution proceeds line-by-line through the
    block execution continues after the sequence
    structure.
  • ltblockgt

8
Selection (if)
  • A condition is evaluated, and if it is true, a
    block is executed execution continues after the
    selection structure.
  • if (ltconditiongt)
  • ltblockgt

9
Iteration (while)
  • A condition is evaluated, and if it is true, a
    block is executed this is repeated until the
    condition is false, then execution continues
    after the iteration structure. AKA indefinite
    iteration.
  • while (ltconditiongt)
  • ltblockgt

10
Block
  • A block consists of EITHER a single statement
    terminated by a semi-colon OR a followed by one
    or more statements each terminated by a
    semi-colon followed by a (technically, in
    either case, the statements are optional).

11
Nested Logic Control Structures
  • A statement in a block may be another logic
    control structure.
  • These are the only three logic control structures
    (with nesting) needed to develop any program.
  • Structured Programming
  • Programs that only use the above logic control
    structures, ie, do not use the goto statement.

12
Additional Logic Control Structures
  • These are not needed, but they are supplied
    because they are very useful
  • Two-Way Selection (if-else)
  • Multi-Way Selection (switch)
  • Iteration (for) (AKA definite iteration)
  • Iteration (do) (similar to while loop but loop is
    executed at least once)

13
Two-Way Selection (If-Else)
  • If (ltconditiongt)
  • lttrue-blockgt
  • if (!ltconditiongt)
  • ltfalse-blockgt
  • can be re-written as
  • if (ltconditiongt)
  • lttrue-blockgt
  • else
  • ltfalse-blockgt

14
Multi-Way Selection (Switch)
  • switch (ltexpressiongt)
  • case ltconstantgt
  • ltblockgt
  • Most of the time, a break statement should
  • be the last statement of each block.

15
For Loops
  • i 1 // initialization
  • while (i lt 10) // continuation condition
  • cout ltlt i ltlt \n // block
  • i // update
  • Can be re-written as
  • For (i 1 i lt 10 i)
  • cout ltlt i ltlt \n

16
Do Loop
  • do
  • ltblockgt // executed at least once
  • while (ltconditiongt)
  • string done
  • do
  • // some processing .
  • cout ltlt Done?
  • cin gtgt done
  • while (done ! yes)

17
Break Continue
  • The break statement branches control to the next
    statement after the logic control structure.
  • The continue statement branches control back to
    the condition in the logic control structure.
  • Neither is used that much, but rarely may be
    usefull.

18
Loop Control
  • Counters a variable counts the number of times
    to loop through data.
  • Sentinals a special value at the end of a data
    list signals when the loop should stop.
  • Flags a boolean variable is used to record a
    condition when the loop should stop.

19
Pseudo-code
  • A program design method where a program is
    initially written in a natural language (eg
    English), then is repeated re-written in more
    detail until it is written in a programming
    language (eg C).

20
Formatting
  • C does not impose any format.
  • But typically
  • if (ltconditiongt)
  • ltblockgt
  • if (ltconditiongt)
  • lttrue-blockgt
  • else
  • ltfalse-blockgt

21
Formatting
  • while (ltconditiongt)
  • ltblockgt
  • for (ltinitializationgt ltconditiongt ltupdategt)
  • ltblockgt

22
Formatting
  • if (c1)
  • while (c2)
  • if (c3)
  • do
  • ...
  • while (c4)
  • else
  • for (init c5 update)
  • ...

23
Formatting
  • Be consistent with whatever style you choose to
    make the code easier to understand!

24
Goto
  • Unconditional transfer of control to anywhere in
    the same function.
  • Format
  • goto ltlabelgt
  • ltlabelgt

25
When to use a goto
  • There may be rare and obscure reasons to use a
    goto, but generally it should not be used the
    result may be spaghetti code (incomprehensible
    logic).
  • NEVER, NEVER, NEVER, NEVER, NEVER, NEVER, NEVER,
    NEVER, NEVER, NEVER use a goto in this course!

26
Infinite Loops
  • A loop that never terminates on its own.
  • A logic error.
  • Example
  • // print the values 1 to 5
  • i 1
  • while (i lt 5)
  • // notice the lack of
  • cout ltlt i ltlt \n // incrementing i - this
  • // actually prints 1s forever

27
Development Pointers
  • Use top-down design, incremental design, and
    pseudo-code.
  • Think about how you would solve the problem using
    paper and pencil. The computer solution will
    probably be similar. After determining the steps
    you would take, then determine the equivalent
    operations in the programming language. Then code
    those operations.
  • The patterns in the output to be achieved
    frequently will point you towards the logic of
    the program. Think about the operations to
    produce the output, the calculations needed to do
    that, and finally the input needed to do that.
    Work backwards from output to input. You know
    what you want to produce how do you get there?

28
State of the Machine
  • The state of the machine refers to all of the
    variables used in a program what the values
    mean at any point during the execution of that
    program. Understanding this is key to
    understanding the program, because it leads to
    understanding what is happening at any one point
    in the program.

29
Examples
  • ch4pp5.cpp
  • ch5pp1.cpp
  • ch5pp3.cpp
  • ch5pp5.cpp
  • ch5pp9.cpp
  • ch5pp11.cpp

30
Reading
  • Chapter 4 5 cover these same concepts.
    Reviewing them may help clarify the material.
Write a Comment
User Comments (0)
About PowerShow.com