170 likes | 325 Views
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 Excessive use of the GOTO statement can lead to spaghetti code Definition of a Real Programmer
E N D
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 Programmer http://www.multicians.org/thvv/realprogs.html Programming Languages, Third Edition
The Friendship Algorithm(from The Big Bang Theory)http://www.youtube.com/watch?v=k0xgjUhEG3U
Structure Programmingand Flowcharts Only need 3 patterns: Sequence Selection (Decision) Repetition (Loop) Each pattern has a single entry point and a single exit point http://www.rff.com/structured_flowchart.htm
A Fortran Example of Spaghetti CodeWhat does this actually do …… and can we rewrite it in Java/C++ or Python? Programming Languages, Third Edition
Spaghetti Code Links • http://encyclopedia2.thefreedictionary.com/spaghetti+code • http://en.wikipedia.org/wiki/Fortran
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
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
The GOTO Controversy and Loop Exits (cont’d.) 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) 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 ("\nEnternumber (-1 to quit): ") num= int(x) Programming Languages, Third Edition
Evaluation of Boolean Expressions 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!) Programming Languages, Third Edition
Evaluation of Boolean Expressions 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!) Programming Languages, Third Edition
Evaluation of Boolean Expressions • 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 Programming Languages, Third Edition
Evaluation of Boolean Expressions • 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 Programming Languages, Third Edition
Evaluation of Boolean Expressions Consider : 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)
Evaluation of Boolean Expressions Is 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 booleanevaluation? (ans: call to function g IS NOT executed!) under full booleanevaluation? (ans: call to function g ISexecuted!)
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)