Download presentation

Presentation is loading. Please wait.

Published byLaurel Mangum Modified over 3 years ago

1
Computer Science 101 While Statement

2
Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement is while : Note the colon and indentation Note the colon and indentation T F Cond

3
Example: Print first 10 positive integers

4
Example: Print even positives thru 20

6
Function rollDie

7
Function rollDie (cont.)

9
Function checkDie

10
JES Input Functions The Python input function works only for numerical input. 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. 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

12
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 printNow that displays its output immediately. It is of the form printNow( ) Note that it only prints one thing. 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 printNow that displays its output immediately. It is of the form printNow( ) Note that it only prints one thing.

13
str str is 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. str is 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.

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

15
Function averageScores

17
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. 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.

18
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. 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.

19
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. 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.

20
Count-controlled Loops // General form while :

21
Count-controlled Loops // General form while : // Count up while : counter = 1 while counter <= 10 : counter = counter + 1

22
Count-controlled Loops // General form while : // Count down while : counter = 10 while (counter > 0): counter = counter - 1

23
Increment and Decrement counter = 1 while counter <= 10 : counter += 1 counter = 10 while counter > 0 : counter -= 1

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

25
Off-by-One Error counter = 0 while (counter < 10) : // Executes 10 passes counter = counter + 1 counter = 1 while (counter < 10) : // Executes 9 passes counter = counter + 1; counter = 1 while (counter <= 10) : // Executes 10 passes counter = counter + 1 counter = 0 while (counter <= 10) : // Executes 11 passes counter = counter + 1

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

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

Similar presentations

OK

Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.

Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on film industry bollywood life Ppt on computer science subject Ppt on infrared remote control system Ppt on principles of peace building in africa Download ppt on coordination in plants Ppt on cross docking charges Ppt on carbon and its compounds ppt Ppt on biodegradable and non biodegradable waste One act play ppt on tv Free download ppt on hydro power plant