Download presentation
Presentation is loading. Please wait.
Published byTauno Pesonen Modified over 5 years ago
1
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional Repetition Chapter 23 (Snyder) – Unsolvability: Limits to Computation
2
Conditional Repetition
many problems involve repeating some task over and over until a specific condition is met e.g., putting names in order e.g., repeatedly prompting the user for a valid input in JavaScript, while loops provide for conditional repetition
3
While Loops a while loop is dependent on a boolean condition.
the statements inside a while loop’s curly braces (a.k.a. the loop body) are executed repeatedly as long as the condition remains true general form: when the browser encounters a while loop, it first evaluates the boolean test if the test succeeds, then the statements inside the loop are executed in order once all the statements have been executed, program control returns to the beginning of the loop the loop test is evaluated again, and if it succeeds, the loop body statements are executed again this process repeats until the boolean test fails
4
Loop Tests note: the loop test defines the condition under which the loop continues this is often backwards from the way we think about loops e.g., read input until you get a positive number (i.e., until input > 0) while (input <= 0) { } e.g., keep executing the loop until two numbers are equal while (x != y ) { }
5
Counter-Driven Loops since a while loop is controlled by a condition, it is usually impossible to predict the number of repetitions that will occur a while loop can also be used to repeat a task some fixed number of times implemented by using a while loop whose test is based on a counter general form of counter-driven while loop: the counter is initially set to 0 before the loop begins, and is incremented at the end of the loop body the counter keeps track of how many times the statements in the loop body have executed when the number of repetitions reaches the desired number, the loop test fails and the loop terminates
6
Counter-Driven Loops <body> <script language="Javascript"> loop = prompt ("How many times should the loop run"); loop = parseFloat(loop); count = 1; while (count <= loop) { document.write("This is loop " + count+ "<br />"); count = count + 1; } </script> </body> This is loop 1 This is loop 2 This is loop 3 This is loop 4 This is loop 5 This is loop 6 This is loop 7 This is loop 8
7
Variables and Repetition
any variable can be employed to control the number of loop repetitions and the variable can be updated in various ways example: countdown
8
Unsolvability and Non-feasability
The loops we’ve seen so far will stop at some point But it is possible for a loop to never stop while (count > 0) { document.write("This is loop " + count+ "<br />"); count = count + 1; } For which values of count will the loop stop? For which values of count will the loop never stop?
9
Infinite Loops the browser will repeatedly execute statements in the body of a while loop as long as the loop test succeeds (evaluates to true) it is possible that the test will always succeed and the loop will run forever a loop that runs forever is known as an infinite loop (or a black hole loop) to guard against infinite loops, make sure that some part of the loop test changes inside the loop in the above example, repCount is not updated in the loop so there is no chance of terminating once the loop starts an infinite loop may freeze up the browser sometimes, clicking the Stop button will suffice to interrupt the browser other times, you may need to restart the browser
10
Unsolvability and Non-feasability
Computability: A problem is computable if it is possible to write a computer program to solve it. Example: take the average of 30 numbers, sort a list of names. A noncomputable problem is also called unsolvable (since it is unsolvable by a computer). Is every clearly defined function computable? NO. The Halting Problem is such a problem
11
The Halting Problem Algorithms may contain loops which may be infinite or finite in length. The amount of work done in an algorithm usually depends on the data input. Algorithms may consist of various numbers of loops, nested or in sequence. The Halting problem asks the question. Given a program and an input to the program, determine if the program will eventually stop when it is given that input. Trial solution Just run the program with the given input. If the program stops, we know the program stops. But if the program doesn't stop in a reasonable amount of time, we cannot conclude that it won't stop. Maybe we didn't wait long enough.
12
The Halting Problem STEP 1:
Sketch of a proof that the Halting Problem is unsolvable This proof by contradiction was devised by Alan Turing, 1936 STEP 1: Suppose you have a solution to the halting problem called H. H takes two inputs: a program P and an input I for the program P. If H says that P never halts with input I, enter an infinite loop If H says that P halts with input I, write “halt” and stop.
13
The Halting Problem We can also run program H to take P as both inputs (the program and its input) H will now tell us whether or not program P will halt with program P as the input (a program can read another program as input) STEP 2 Let us construct a new algorithm K that takes H's output as its input and does the following if H enters an infinte loop then K halts, If H outputs “halt”, then K enters an infinite loop That is, K will do the opposite of H's output.
14
The Halting Problem STEP 3
Since K is a program, let us use K as the input to K. If H says that K halts then K itself would loop (that's how we constructed it). If H says that K loops then K will halt. In either case H gives the wrong answer for K. Thus H cannot work in all cases. We've shown that it is possible to construct an input that causes any solution H to fail.
15
Unsolvability and Non-feasability
Feasibility: Although it is possible to write a computer program to solve a given problem, this is not necessarily always practical. Sometimes, we can write a short, clear program which correctly solves a given problem, but the time it takes to execute this program is infeasible. Non-Feasible (or infeasible) – a computable problem that takes too long to solve. Non-feasible functions take exponential time input=64 (264 =18,446,744,073,709,551,616) playing chess considering every possible move, considering every possible seating arrangement for the class In general, any solution that considers every possible subset of a set is exponential.
16
Feasible Functions Feasible Functions: Searching a list
Given n items, need to do about n operations Sorting (input size n) Search for the smallest element, put it in the beginning (costs n time) Do this about n times - Overall: about n2 operations If you are doing 2n work, that is not feasible. Notice the difference between n2 and 2n. n2 has n in the base, and a constant exponent, while 2n has n in the exponent! Analysis of Algorithms – analyzing the running times of algorithms (or programs). This field of computer science studies algorithms and their efficiency, in terms of the amount of work (and space) needed to execute an algorithm.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.