A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN 0-13-046709-X Chapter 13 (Reed) - Conditional.

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CSE115/ENGR160 Discrete Mathematics 02/28/12
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Unsolvability and Infeasibility. Computability (Solvable) A problem is computable if it is possible to write a computer program to solve it. Can all problems.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University Conditional Execution.
Algorithms.
CSE15 Discrete Mathematics 03/06/17
The Acceptance Problem for TMs
Lecture 4b Repeating With Loops
Introduction to Computing Science and Programming I
Chapter 5: Structured Programming
REPETITION CONTROL STRUCTURE
Introduction To Repetition The for loop
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Topics Introduction to Repetition Structures
Python: Control Structures
Programming Logic and Design Fourth Edition, Comprehensive
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CS1010 Programming Methodology
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Algorithm Analysis CSE 2011 Winter September 2018.
JavaScript: Control Statements.
Topics Introduction to Repetition Structures
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
CS 2210 Discrete Structures Algorithms and Complexity
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
CSCE 411 Design and Analysis of Algorithms
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Alternate Version of STARTING OUT WITH C++ 4th Edition
How Hard Can It Be?.
Repetition Structures
Iteration: Beyond the Basic PERFORM
Computing Fundamentals
Analysis of Algorithms
Topics Introduction to Repetition Structures
Impossible problems.
Chapter 11 Conditional Execution
Analysis of Algorithms
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Chapter 13 Conditional Repetition
The while Looping Structure
Computers and Scientific Thinking David Reed, Creighton University
Analysis of Algorithms
Looping and Repetition
Presentation transcript:

A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN 0-13-046709-X Chapter 13 (Reed) - Conditional Repetition Chapter 23 (Snyder) – Unsolvability: Limits to Computation

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

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

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 ) { . . . }

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

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

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

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?

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

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

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.  

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.

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.

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.

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.

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.