Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100 Introduction to Computing Seminar October 7, 2015.

Similar presentations


Presentation on theme: "CS 100 Introduction to Computing Seminar October 7, 2015."— Presentation transcript:

1 CS 100 Introduction to Computing Seminar October 7, 2015

2 Topics Introduction to Repetition Structures The while Loop: a Condition-Controlled Loop The for Loop: a Count-Controlled Loop Calculating a Running Total Sentinels Input Validation Loops Nested Loops

3 Introduction to Repetition Structures Often have to write code that performs the same task multiple times  Disadvantages to duplicating code Makes program large Time consuming May need to be corrected in many places Repetition structure: makes computer repeat included code as necessary  Includes condition-controlled loops and count- controlled loops

4 The while Loop: a Condition- Controlled Loop while loop: while condition is true, do something  Two parts: Condition tested for true or false value Statements repeated as long as condition is true  In flow chart, line goes back to previous part  General format: while condition: statements

5 The while Loop: a Condition- Controlled Loop (cont’d.)

6 In order for a loop to stop executing, something has to happen inside the loop to make the condition false Iteration: one execution of the body of a loop while loop is known as a pretest loop  Tests condition before performing an iteration  Will never execute if condition is false to start with  Requires performing some steps prior to the loop

7

8 Infinite Loops Loops must contain within themselves a way to terminate  Something inside a while loop must eventually make the condition false Infinite loop: loop that does not have a way of stopping  Repeats until program is interrupted  Occurs when programmer forgets to include stopping code in the loop

9 Examples (1) count = 0 if count < 5: print ("Hello, I am an if statement and count is", count) while count < 5: print ("Hello, I am a while and count is", count) count = count + 1

10 Examples (2) password = " " while password != "cs_100": password = input("Please enter the password: ") if password == "cs_100": print("You have entered the correct password") else: print("Sorry the value entered in incorrect - try again")

11 Examples (3) flag = True while flag: number = int(input("Please enter a number from 0 to 100: ")) if number >= 0 and number <= 100: flag = False else: print("Sorry number must be between 0 and 100") print("Please try again") print("You entered: ", number) print("This is a valid number. ")

12 Infinite Loop count = 0 while count < 5: print ("Hello, I am a while and count is", count) count = count - 1

13 Let’s Work A Problem (1) A student takes 5 exams. Write a program that using looping to collect the test scores. The determine the student’s average for the 5 exams. What do we do first?

14 Let’s Work A Problem (2) Running on a treadmill burns 6.6 calories per minute. Write a program that uses a loop to display the number of calories burned after 5, 10, 15, 20, 25, and 30 minutes. What do we do first?


Download ppt "CS 100 Introduction to Computing Seminar October 7, 2015."

Similar presentations


Ads by Google