a few chapter 9 topics. the goto controversy in the 1960s with structured control use increasing,...

16
A few Chapter 9 Topics

Upload: octavia-bond

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

A few Chapter 9 Topics

Page 2: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

The GOTO Controversy • In the 1960s with structured control use

increasing, debate began about the usefulness of the GOTO statement

• Excessive use of the GOTO statement can lead to spaghetti code

Definition of a Real Programmerhttp://www.multicians.org/thvv/realprogs.html

Programming Languages, Third Edition 2

Page 3: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

The Friendship Algorithm(from The Big Bang Theory)

http://www.youtube.com/watch?v=k0xgjUhEG3U

Page 4: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Structure Programmingand Flowcharts

Only need 3 patterns:SequenceSelection (Decision)Repetition (Loop)

Each pattern has a single entry point and a single exit point

http://www.rff.com/structured_flowchart.htm

Page 5: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

A Fortran Example of Spaghetti CodeWhat does this actually do …

… and can we rewrite it in Java/C++ or Python?

Programming Languages, Third Edition 5

Page 6: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Spaghetti Code Links

• http://encyclopedia2.thefreedictionary.com/spaghetti+code

• http://en.wikipedia.org/wiki/Fortran

Page 7: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

The GOTO Controversy • In 1966, Bohm and Jacopini produced theoretical

result that gotos were completely unnecessary• In 1968, Dijkstra published “GOTO Statement

Considered Harmful”– Proposed that its use be severely controlled or

abolished• Many considered gotos to be justified in certain

cases• In 1987, Rubin published ““Goto considered

harmful” considered harmful”

Programming Languages, Third Edition 7

Page 8: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

The GOTO Controversy and Loop Exits

• Still some debate on the propriety of unstructured exits from loops– Some argue there should only be one exit in a loop

and it should be at beginning or end of the loop– Others argue that may require more complicated

code for certain situations• Example: sentinel-based loop for processing a

series of input values– Called the loop and a half problem

Programming Languages, Third Edition 8

Page 9: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

The GOTO Controversy and Loop Exits (cont’d.)

Programming Languages, Third Edition 9

This code has a “loop and a half”

x = raw_input ("\nEnter number (-1 to quit): ")num = int(x) while num <> -1: print “\nDouble the number is” + str(2*num) x = raw_input ("\nEnter number (-1 to quit): ") num = int(x)

This code does not b/c of break

while True: x = raw_input ("\nEnter number (-1 to quit): ") num = int(x)

if num = -1: break

print “\nDouble the number is” + str(2*num)

Page 10: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean Expressions

Programming Languages, Third Edition 10

Consider the following compound condition:

cond1 and cond2 and cond3 and cond4

How many of the conditions need to be true for the entire expression to be true?

(ans: all of them!)

How many of the conditions need to false for the entire expression to be false?

(ans: only one!)

Page 11: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean Expressions

Programming Languages, Third Edition 11

Consider the following compound condition:

cond1 or cond2 or cond3 or cond4

How many of the conditions need to be true for the entire expression to be true?

(ans: only one!)

How many of the conditions need to be false for the entire expression to be false?

(ans: all of them!)

Page 12: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean Expressions

Programming Languages, Third Edition 12

Short-circuit evaluation:

Boolean expressions are evaluated left to right up to the point where the truth value of the entire expression becomes known, and then evaluation stops

Page 13: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean Expressions

Programming Languages, Third Edition 13

• According to text, C/C++ and Java have short-circuit boolean evaluation ( && , || )

• Python has short-circuit boolean evaluation

https://docs.python.org/2/library/stdtypes.html• Visual Basic offers the programmer a choice

(Ada has similar options)• “And”, “Or” are not short-circuit• “AndAlso”, “OrElse” are short-circuit

Page 14: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean ExpressionsConsider :

if (credits != 0 && qpts / credits > 2.0 )

What happens if credits is equal to zero?

under full boolean evaluation?(ans: runtime error, division by zero!)

under short-circuit boolean evaluation?(ans: no problem, division not performed!)

(See VB example code)

Page 15: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean ExpressionsIs short-circuit boolean evaluation ever bad?

Consider the expression: f(x) || g(x) (where f(x) and g(x) return boolean values)

What happens when f(x) is true?

under short-circuit boolean evaluation?(ans: call to function g IS NOT executed!)under full boolean evaluation?(ans: call to function g IS executed!)

Page 16: A few Chapter 9 Topics. The GOTO Controversy In the 1960s with structured control use increasing, debate began about the usefulness of the GOTO statement

Evaluation of Boolean Expressions

So what? Why does it matter?

Suppose function g has a side effect (i.e., modifies a global variable)

Then whether or not g gets executed has an effect on the remainder of the program!

(See VB example code)