1 / 28

Computer Science 101

Computer Science 101. While Statement. T. Cond. F. Iteration: The While-Statement. The syntax for the While-Statement is while <condition> : <list of statements> Note the colon and indentation. Example: Print first 10 positive integers. Example: Print even positives thru 20.

jerry-dale
Download Presentation

Computer Science 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 101 While Statement

  2. T Cond F Iteration: The While-Statement • The syntax for the While-Statement iswhile <condition> : <list of statements> • Note the colon and indentation

  3. Example: Print first 10 positive integers

  4. Example: Print even positives thru 20

  5. Example: Print even positives thru 20

  6. Function rollDie

  7. Function rollDie (cont.)

  8. Function rollDie (cont.)

  9. Function checkDie

  10. JES Input Functions • The Python input function works only for numerical input. • JES provides some special input functions. These functions allow us to get input for other types, via a dialog box that pops up with given prompt. • requestInteger for integers • requestNumber for floats • requestString for strings

  11. JES printNow • With JES, print statements within a function do not display their output until the function has completed its execution. Sometimes this is ok, but often the user needs to see intermediate results for purposes of decision making. JES provides a function printNowthat displays its output immediately. It is of the formprintNow(<whatever>)Note that it only prints one thing.

  12. str • stris a Python function that will convert a number to a string. This is often useful when we want to combine string and numerical data for output.

  13. Recommendation • Unless specified otherwise, you should use the “request” forms for input and printNow for output in your functions.

  14. Function averageScores

  15. More on Conditions • and In Python we can combine two conditions with and to form a new condition that is true only if both or the original conditions are true.

  16. More on Conditions • or In Python we can combine two conditions with or to form a new condition that is true provided at least one of the original conditions is true.

  17. More on Conditions • not In Python we can make a new condition from a given condition by placing not in front of the original condition. The truth value of the new condition is the opposite of the original condition.

  18. Count-controlled Loops // General form <initialize a counter variable> while <test counter for termination condition> : <do something> <change the value of counter>

  19. Count-controlled Loops // General form <initialize a counter variable> while <test counter for termination condition> : <do something> <change the value of counter> // Count up <initialize a counter variable> while <counter is less than a limit value> : <do something> <increase the value of counter> counter = 1 while counter <= 10 : <do something> counter = counter + 1

  20. Count-controlled Loops // General form <initialize a counter variable> while <test counter for termination condition> : <do something> <change the value of counter> // Count down <initialize a counter variable> while <counter is greater than a limit value>: <do something> <decrease the value of counter> counter = 10 while (counter > 0): <do something> counter = counter - 1

  21. Increment and Decrement counter = 1 while counter <= 10 : <do something> counter += 1 counter = 10 while counter > 0 : <do something> counter -= 1

  22. Designing Correct Loops • Initialize all variables properly • Plan how many iterations, then set the counter and the limit accordingly • Check the logic of the termination condition • Update the loop control variable properly

  23. Off-by-One Error counter = 0 while (counter < 10) : // Executes 10 passes <do something> counter = counter + 1 counter = 1 while (counter < 10) : // Executes 9 passes <do something> counter = counter + 1; counter = 1 while (counter <= 10) : // Executes 10 passes <do something> counter = counter + 1 counter = 0 while (counter <= 10) : // Executes 11 passes <do something> counter = counter + 1

  24. Infinite Loop counter = 1 while (counter <= 10) : // Executes 5 passes <do something> counter = counter + 2 counter = 1 while (counter != 10) : // Runs forever <do something> counter = counter + 2; In general, avoid using != in loop termination conditions with count-controlled loops.

  25. Testing Loops • Can vary the limit or the control variable, or both • Use a negative value, zero, and a positive value • Display a trace if things aren’t working

  26. Cows just have two toes you know!

More Related