Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While-...

28
Computer Science Computer Science 101 101 While Statement While Statement

Transcript of Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While-...

Page 1: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Computer Science Computer Science 101101

While StatementWhile Statement

Page 2: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Iteration: The While-Iteration: The While-StatementStatement

The syntax for the The syntax for the While-Statement isWhile-Statement is

while <condition> :while <condition> : <list of <list of statements>statements>

Note the colon and Note the colon and indentationindentation

T

F

Cond

Page 3: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Example: Print first 10 positive Example: Print first 10 positive integersintegers

Page 4: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Example: Print even positives thru Example: Print even positives thru 2020

Page 5: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Example: Print even positives thru Example: Print even positives thru 2020

Page 6: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Function rollDieFunction rollDie

Page 7: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Function rollDie (cont.)Function rollDie (cont.)

Page 8: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Function rollDie (cont.)Function rollDie (cont.)

Page 9: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Function Function checkDiecheckDie

Page 10: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

JES Input FunctionsJES Input Functions

The Python The Python inputinput function works only function works only for numerical input. for numerical input.

JES provides some special input JES provides some special input functions. These functions allow us to functions. These functions allow us to get input for other types, via a dialog get input for other types, via a dialog box that pops up with given prompt.box that pops up with given prompt.– requestIntegerrequestInteger for integers for integers– requestNumberrequestNumber for floats for floats– requestStringrequestString for strings for strings

Page 11: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
Page 12: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

JES printNowJES printNow

With JES, print statements within a function do With JES, print statements within a function do not display their output until the function has not display their output until the function has completed its execution. Sometimes this is ok, completed its execution. Sometimes this is ok, but often the user needs to see intermediate but often the user needs to see intermediate results for purposes of decision making. JES results for purposes of decision making. JES provides a function provides a function printNowprintNow that displays its that displays its output immediately. It is of the formoutput immediately. It is of the form

printNow(<whatever>)printNow(<whatever>)

Note that it only prints one thing.Note that it only prints one thing.

Page 13: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

strstr strstr is a Python function that will convert a number to is a Python function that will convert a number to

a string. a string.

This is often useful when we want to combine string This is often useful when we want to combine string and numerical data for output.and numerical data for output.

Page 14: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

RecommendationRecommendation

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

Page 15: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Function averageScoresFunction averageScores

Page 16: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
Page 17: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

More on ConditionsMore on Conditions

andand In Python we can In Python we can combine two combine two conditions with conditions with andand to form a new to form a new condition that is true condition that is true only if both or the only if both or the original conditions original conditions are true.are true.

Page 18: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

More on ConditionsMore on Conditions

oror In Python we can In Python we can combine two combine two conditions with conditions with oror to to form a new condition form a new condition that is true provided that is true provided at least one of the at least one of the original conditions is original conditions is true.true.

Page 19: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

More on ConditionsMore on Conditions

notnot In Python we can In Python we can make a new condition make a new condition from a given condition from a given condition by placing by placing notnot in front in front of the original of the original condition. The truth condition. The truth value of the new value of the new condition is the condition is the opposite of the original opposite of the original condition.condition.

Page 20: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Count-controlled LoopsCount-controlled Loops

// General form<initialize a counter variable>while <test counter for termination condition> : <do something> <change the value of counter>

Page 21: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Count-controlled LoopsCount-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 = 1while counter <= 10 : <do something> counter = counter + 1

Page 22: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Count-controlled LoopsCount-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 = 10while (counter > 0): <do something> counter = counter - 1

Page 23: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Increment and DecrementIncrement and Decrement

counter = 1while counter <= 10 : <do something> counter += 1

counter = 10while counter > 0 : <do something> counter -= 1

Page 24: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Designing Correct LoopsDesigning Correct Loops Initialize all variables properlyInitialize all variables properly

– Plan how many iterations, then set the Plan how many iterations, then set the counter and the limit accordinglycounter and the limit accordingly

Check the logic of the termination Check the logic of the termination conditioncondition

Update the loop control variable Update the loop control variable properlyproperly

Page 25: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Off-by-One ErrorOff-by-One Errorcounter = 0while (counter < 10) : // Executes 10 passes <do something> counter = counter + 1

counter = 1while (counter < 10) : // Executes 9 passes <do something> counter = counter + 1;

counter = 1while (counter <= 10) : // Executes 10 passes <do something> counter = counter + 1

counter = 0while (counter <= 10) : // Executes 11 passes <do something> counter = counter + 1

Page 26: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Infinite LoopInfinite Loop

counter = 1while (counter <= 10) : // Executes 5 passes <do something> counter = counter + 2

counter = 1while (counter != 10) : // Runs forever <do something> counter = counter + 2;

In general, avoid using != in loop termination conditions with count-controlled loops.

Page 27: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.

Testing LoopsTesting Loops Can vary the limit or the control Can vary the limit or the control

variable, or bothvariable, or both

Use a negative value, zero, and a Use a negative value, zero, and a positive valuepositive value

Display a trace if things aren’t Display a trace if things aren’t workingworking

Page 28: Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.