[4340] - 101 M.C.A. - I (Under Science Faculty) COMPUTER SCIENCE
Computer Science 101
-
Upload
athena-higgins -
Category
Documents
-
view
25 -
download
1
description
Transcript of Computer Science 101
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
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
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.
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.
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.
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.
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.
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.
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-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
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
Increment and DecrementIncrement and Decrement
counter = 1while counter <= 10 : <do something> counter += 1
counter = 10while counter > 0 : <do something> counter -= 1
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
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
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.
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