SlideShare a Scribd company logo
1 of 14
An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #7:
WHILE Loop
How Do You Do Things
Over And Over Again?
• In Python, loop statements repeatedly execute
instructions until a goal has been reached.
• In Python, the while statement implements such a
repetition. It has the form:
while condition :
statement1
statement 2
• As long as the condition remains true, the
statements inside the while statement are executed.
Image Credit: etc.usf.edu
Body
Condition
Remember Mr. Bank Account
Interest Rate Problem?
Problem:
You put $10,000 into a bank account that earns 5 percent interest per year.
How many years does it take for the account balance to be double the original?
(over and over & over again)
Some people call this loop an event-controlled loop.
How Can I Loop For A Given
Number Of Times?
• You can use a while loop that is controlled by a
counter:
counter = 1 # Initialize the counter.
while counter <= 10 : # Check the counter.
print(counter)
counter = counter + 1 # Update the loop variable
Note: Some people call this loop count-controlled.
Image Credit: www.dreamstime.com
Loop Challenge!
• Create a program to print the sum of all odd
numbers between a and b (inclusive).
Image Credit: www.dreamstime.com
Infinite Loops
• Def: a loop that runs forever and can be
stopped only by killing the program or
restarting the compute
year = 1
while year <= 20 :
interest = balance * RATE / 100
balance = balance + interest
Image Credit: www.canstockphoto.com
The Loop Problem:
“Off By-One Errors”
year = 0
while balance < TARGET :
year = year + 1
interest = balance * RATE / 100
balance = balance + interest
print("The investment doubled after", year, "years.")
• Should year start at 0 or at 1?
• Should you test for balance < TARGET or for
balance <= TARGET?
• It is easy to be off by one in these expressions.
• Think through simple test cases to avoid this type of error.
Image Credit: www.wisegeek.org
Sentinel Values
• One common programming task is
to read and process a sequence of
input values.
• You need to have some method of indicating the end of the
sequence.
– 0?
– -1?
• Such a value, which is not an actual input,
but serves as a signal for termination, is called a sentinel
Sentinel Value Example:
Average Salary
• Create a program that computes the average of a set
of salary values.
• In our sample program, we will use any negative
value as the sentinel.
• Any negative number can end the loop, but we
prompt for a sentinel of –1 so that the
user need not ponder which negative number to
enter.
Image Credit: www.clipartpanda.com
What Do We Use Loops For?
• Sum and Average Value
• Counting Matches
– How many values fulfill a particular condition.
• Prompting Until a Match is Found
• Maximum and Minimum
• Comparing Adjacent Values
Image Credit: www.clipartpanda.com
Remember
The Python ATM Machine?
Image Credit: www.canstockphoto.com
Create software that will provide an ATM user
with the proper change for any dollar amount
up to $200.
Example: Run the code for $200 and for $19
What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While
What We Covered Today
1. While Loop
2. Infinite Loops
3. Sentinel Values
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
What We’ll Be Covering Next Time
1. For Loop
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

More Related Content

Viewers also liked

An Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed DesignAn Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed DesignBlue Elephant Consulting
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With DataBlue Elephant Consulting
 
An Introduction To Python - Python Midterm Review
An Introduction To Python - Python Midterm ReviewAn Introduction To Python - Python Midterm Review
An Introduction To Python - Python Midterm ReviewBlue Elephant Consulting
 
An Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and MaintenanceAn Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and MaintenanceBlue Elephant Consulting
 
An Introduction To Python - Nested Branches, Multiple Alternatives
An Introduction To Python - Nested Branches, Multiple AlternativesAn Introduction To Python - Nested Branches, Multiple Alternatives
An Introduction To Python - Nested Branches, Multiple AlternativesBlue Elephant Consulting
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathBlue Elephant Consulting
 
An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsBlue Elephant Consulting
 
An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()Blue Elephant Consulting
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationBlue Elephant Consulting
 

Viewers also liked (12)

An Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed DesignAn Introduction To Software Development - Architecture & Detailed Design
An Introduction To Software Development - Architecture & Detailed Design
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
 
An Introduction To Python - Python Midterm Review
An Introduction To Python - Python Midterm ReviewAn Introduction To Python - Python Midterm Review
An Introduction To Python - Python Midterm Review
 
An Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and MaintenanceAn Introduction To Software Development - Software Support and Maintenance
An Introduction To Software Development - Software Support and Maintenance
 
An Introduction To Python - Nested Branches, Multiple Alternatives
An Introduction To Python - Nested Branches, Multiple AlternativesAn Introduction To Python - Nested Branches, Multiple Alternatives
An Introduction To Python - Nested Branches, Multiple Alternatives
 
An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, Math
 
An Introduction To Python - Graphics
An Introduction To Python - GraphicsAn Introduction To Python - Graphics
An Introduction To Python - Graphics
 
An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1
 
An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List Algorithms
 
An Introduction To Python - Lists, Part 1
An Introduction To Python - Lists, Part 1An Introduction To Python - Lists, Part 1
An Introduction To Python - Lists, Part 1
 
An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()An Introduction To Python - Python, Print()
An Introduction To Python - Python, Print()
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - Implementation
 

Similar to An Introduction To Python - WHILE Loop

classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxssusere336f4
 
Stop Flying Blind! Quantifying Risk with Monte Carlo Simulation
Stop Flying Blind! Quantifying Risk with Monte Carlo SimulationStop Flying Blind! Quantifying Risk with Monte Carlo Simulation
Stop Flying Blind! Quantifying Risk with Monte Carlo SimulationSam McAfee
 
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...Blue Elephant Consulting
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewBlue Elephant Consulting
 
An Introduction To Software Development - Software Development Midterm Review
An Introduction To Software Development - Software Development Midterm ReviewAn Introduction To Software Development - Software Development Midterm Review
An Introduction To Software Development - Software Development Midterm ReviewBlue Elephant Consulting
 
1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loopsallenbailey
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolvingMd. Ashikur Rahman
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxAmy Nightingale
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxssusere336f4
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGERathnaM16
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Blue Elephant Consulting
 
more loops lecture by Professor Evan korth
more loops  lecture by Professor Evan korth more loops  lecture by Professor Evan korth
more loops lecture by Professor Evan korth hammad ali
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Blue Elephant Consulting
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Blue Elephant Consulting
 
Service Levels and Error Budgets - Paweł Kucharski
Service Levels and Error Budgets - Paweł KucharskiService Levels and Error Budgets - Paweł Kucharski
Service Levels and Error Budgets - Paweł KucharskiPROIDEA
 
Software Testing Introduction (Part 1)
Software Testing Introduction (Part 1)Software Testing Introduction (Part 1)
Software Testing Introduction (Part 1)Thapar Institute
 
Being Right Starts By Knowing You're Wrong
Being Right Starts By Knowing You're WrongBeing Right Starts By Knowing You're Wrong
Being Right Starts By Knowing You're WrongData Con LA
 

Similar to An Introduction To Python - WHILE Loop (20)

classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Stop Flying Blind! Quantifying Risk with Monte Carlo Simulation
Stop Flying Blind! Quantifying Risk with Monte Carlo SimulationStop Flying Blind! Quantifying Risk with Monte Carlo Simulation
Stop Flying Blind! Quantifying Risk with Monte Carlo Simulation
 
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean...
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final Review
 
An Introduction To Software Development - Software Development Midterm Review
An Introduction To Software Development - Software Development Midterm ReviewAn Introduction To Software Development - Software Development Midterm Review
An Introduction To Software Development - Software Development Midterm Review
 
1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loops
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
 
Defining tasks for User Stories
Defining tasks for User StoriesDefining tasks for User Stories
Defining tasks for User Stories
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2
 
more loops lecture by Professor Evan korth
more loops  lecture by Professor Evan korth more loops  lecture by Professor Evan korth
more loops lecture by Professor Evan korth
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
Service Levels and Error Budgets - Paweł Kucharski
Service Levels and Error Budgets - Paweł KucharskiService Levels and Error Budgets - Paweł Kucharski
Service Levels and Error Budgets - Paweł Kucharski
 
Software Testing Introduction (Part 1)
Software Testing Introduction (Part 1)Software Testing Introduction (Part 1)
Software Testing Introduction (Part 1)
 
Being Right Starts By Knowing You're Wrong
Being Right Starts By Knowing You're WrongBeing Right Starts By Knowing You're Wrong
Being Right Starts By Knowing You're Wrong
 

An Introduction To Python - WHILE Loop

  • 1. An Introduction To Software Development Using Python Spring Semester, 2015 Class #7: WHILE Loop
  • 2. How Do You Do Things Over And Over Again? • In Python, loop statements repeatedly execute instructions until a goal has been reached. • In Python, the while statement implements such a repetition. It has the form: while condition : statement1 statement 2 • As long as the condition remains true, the statements inside the while statement are executed. Image Credit: etc.usf.edu Body Condition
  • 3. Remember Mr. Bank Account Interest Rate Problem? Problem: You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original? (over and over & over again) Some people call this loop an event-controlled loop.
  • 4. How Can I Loop For A Given Number Of Times? • You can use a while loop that is controlled by a counter: counter = 1 # Initialize the counter. while counter <= 10 : # Check the counter. print(counter) counter = counter + 1 # Update the loop variable Note: Some people call this loop count-controlled. Image Credit: www.dreamstime.com
  • 5. Loop Challenge! • Create a program to print the sum of all odd numbers between a and b (inclusive). Image Credit: www.dreamstime.com
  • 6. Infinite Loops • Def: a loop that runs forever and can be stopped only by killing the program or restarting the compute year = 1 while year <= 20 : interest = balance * RATE / 100 balance = balance + interest Image Credit: www.canstockphoto.com
  • 7. The Loop Problem: “Off By-One Errors” year = 0 while balance < TARGET : year = year + 1 interest = balance * RATE / 100 balance = balance + interest print("The investment doubled after", year, "years.") • Should year start at 0 or at 1? • Should you test for balance < TARGET or for balance <= TARGET? • It is easy to be off by one in these expressions. • Think through simple test cases to avoid this type of error. Image Credit: www.wisegeek.org
  • 8. Sentinel Values • One common programming task is to read and process a sequence of input values. • You need to have some method of indicating the end of the sequence. – 0? – -1? • Such a value, which is not an actual input, but serves as a signal for termination, is called a sentinel
  • 9. Sentinel Value Example: Average Salary • Create a program that computes the average of a set of salary values. • In our sample program, we will use any negative value as the sentinel. • Any negative number can end the loop, but we prompt for a sentinel of –1 so that the user need not ponder which negative number to enter. Image Credit: www.clipartpanda.com
  • 10. What Do We Use Loops For? • Sum and Average Value • Counting Matches – How many values fulfill a particular condition. • Prompting Until a Match is Found • Maximum and Minimum • Comparing Adjacent Values Image Credit: www.clipartpanda.com
  • 11. Remember The Python ATM Machine? Image Credit: www.canstockphoto.com Create software that will provide an ATM user with the proper change for any dollar amount up to $200. Example: Run the code for $200 and for $19
  • 12. What’s In Your Python Toolbox? print() math strings I/O IF/Else elif While
  • 13. What We Covered Today 1. While Loop 2. Infinite Loops 3. Sentinel Values Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
  • 14. What We’ll Be Covering Next Time 1. For Loop Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

Editor's Notes

  1. New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.