Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

21
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg

description

if, elif, else, the Process Evaluate boolean expressions until:  The boolean expression returns True  None of the boolean expressions return True If a boolean returns True, run the corresponding suite. Skip the rest of the if If no boolean returns True, run the else suite, the default suite

Transcript of Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Page 1: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Repetition

Intro to Computer ScienceCS1510

Dr. Sarah Diesburg

Page 2: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Today’s Agenda

From last week Selection statements Conditional statements Compound conditional statements

Repetition Reasons for repetition Tools

Page 3: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

if, elif, else, the Process

Evaluate boolean expressions until: The boolean expression returns True None of the boolean expressions return True

If a boolean returns True, run the corresponding suite. Skip the rest of the if

If no boolean returns True, run the else suite, the default suite

Page 4: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Compound Expressions

Logical Operators (lower case) and or not

Page 5: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Compound Expressions

Examples if (a==b) and (c<d): if (a==b) or (c<d): if not (a==b):

Remember, evaluate each statement first before looking at and, or, not

5

Page 6: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Chained Comparisons

You are going to be tempted to write:0 <= myInt <= 5

But you will need to be very careful

Page 7: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Compound Evaluation

Logically 0 < X < 3 is actually(0 < X) and (X < 3)

Evaluate using X with a value of 2: (0< X) and (X< 3)

Parenthesis first: (True) and (True) Final value: True

(Note: parentheses are not necessary in this case.)

Page 8: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Compound Evaluation

BUT, I’ve seen students write:3 < X < 0

Meaning they want to know if x is outside of that range.

But this is actually(3 < X) and (X < 0)

Does any number fit in this range?

Page 9: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

How can we introduce error checking? Suppose that students are entering numbers

into your miles per gallon program that aren’t valid

Page 10: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

How can we introduce error checking? Suppose that students are entering numbers

into your miles per gallon program that aren’t valid Values not above 0 Ending value that is “smaller” than the starting

value Maybe even values above some upper limit

How can we correct them and ask them to try again?

Page 11: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Repeating Statements

Besides selecting which statements to execute, a fundamental need in a program is repetition repeat a set of statements under some conditions

Between selection and repetition, we have the two most necessary programming statements

Page 12: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

While and For Statements

The while statement is the more general repetition construct. It repeats a set of statements while some condition is True. Often called a sentinel controlled loop

The for statement is useful for iteration, moving through all the elements of data structure, one at a time. Often called a count controlled loop

Page 13: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

while Loop

Top-tested loop (pretest) test the boolean before running Run the program suite test the boolean before each iteration of the loop

while boolean expression:statementSuite

Page 14: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Page 15: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Repeat While the Boolean is True while loop will repeat the statements in the

suite while the boolean is True (or its Python equivalent)

If the boolean expression never changes during the course of the loop, the loop will continue forever.

Page 16: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

x_int = 0

while x_int < 10: print (x_int) x_int = x_int + 1

print()print( "Final value of x_int: ", x_int)

What is the Final Value printed by this code?

While Loop Example

Page 17: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

General Approach to a While outside the loop, initialize the boolean somewhere inside the loop you perform some

operation which changes the state of the program, eventually leading to a False boolean and exiting

the loop Have to have both!

Page 18: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

For and Iteration

One of Python’s strength’s is it’s rich set of built-in data structures

The for statement is a common statement for manipulation of a data structure for each element in the datastructure

perform some operation on that element

Page 19: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Page 20: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

For Loop Example

numbers = [0,1,2,3,4,5,6,7,8,9]for xInt in numbers: print (xInt)

print()print ("Final value of xInt: " + str(xInt) )

Page 21: Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

How can we introduce error checking? Suppose that students are entering numbers

into your miles per gallon program that aren’t valid Values not above 0 Ending value that is “smaller” than the starting

value Maybe even values above some upper limit

How can we correct them and ask them to try again?