Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 While Statement.

Similar presentations


Presentation on theme: "Computer Science 101 While Statement."— Presentation transcript:

1 Computer Science 101 While Statement

2 The While-Statement T F Cond The syntax for the While-Statement is while <condition> : <list of statements> Note the colon and indentation

3 While statement example
In this example, we want a program where the user inputs an upper limit. Then we want to think of adding consecutive integers … until the sum reaches or exceeds the limit. The program should print out how many terms were in the sum. For example, if the limit is 7, gives 6, and gives 10; so we should print 4.

4 While-Statement example
limit = input (“Enter limit for sum”) total = 0 num = 1 while total < limit : total = total + num num = num print num Do example Discount now.

5 While-Statement example
Do example Discount now.

6 While-Statement example
Do example Discount now.

7 While-Statement example
Do example Discount now.

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

9 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

10 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

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

12 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

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

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

15 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

16 GCD The largest integer that evenly divides into two non-negative integers Example: gcd(32, 24) = 8 Algorithm discovered by Euclid was one of the first algorithms ever discovered

17 Flash from the Past Euclid’s Algorithm for Greatest Common Divisor
Get two positive integers as input, call them I and J. Divide I by J, and call the Remainder R. If R is not 0, then reset I to the value of J, reset J to the value of R, and go back to step 2. Print out the answer, which is the value of J. Stop

18 Example GCD(206, 40)

19 Example GCD(206, 40) = GCD(40, 6)

20 Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4)

21 Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2)

22 Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2) = GCD(2, 0)

23 Example GCD(206, 40) = GCD(40, 6) = GCD(6, 4) = GCD(4, 2) = GCD(2, 0)
= 2

24 Pseudocode Get I, J Set R to remainder of I divided by J While R≠0 Do Set I to J Set J to R Set R to remainder of I divided by J end-of-loop Print J

25 Code

26 Some comments on programming style
Use meaningful variable names. Use comments for your name, purpose of program, date. Include a pseudocode algorithm in comments, if appropriate. Use comments to mark off sections of code and to clarify “tricky” code.

27 Keep lookin, boys, we'll findem somewhere


Download ppt "Computer Science 101 While Statement."

Similar presentations


Ads by Google