ECS15 while.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Objectives In this chapter, you will learn about:
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Mastery Objective: Students will understand how to use while loops in computer programming.
Chapter 5 Loops.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CS101 Computer Programming I Chapter 4 Extra Examples.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
CSCI-100 Introduction to Computing
CS 100 Introduction to Computing Seminar October 7, 2015.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
PHP : Validating forms and loops. Some things for helping you to validate input  The isset function can be used to test if a php variable is passed.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Repetition In today’s lesson we will look at: why you would want to repeat things in a program different ways of repeating things creating loops in Just.
Sophomore Scholars Java
Chapter 4 Repetition Statements (loops)
Control Structures II Chapter 3
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
7.6.7 Simple Interest.
ECS10 10/10
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
While Loops in Python.
Topics Introduction to Repetition Structures
Selected Topics From Chapter 6 Iteration
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Logical Operators and While Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
While Loops.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is.
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Loop Strategies Repetition Playbook.
Python programming exercise
A LESSON IN LOOPING What is a loop?
Logical Operators and While Loops
For loops Taken from notes by Dr. Neil Moore
FLUENCY WITH INFORMATION TECNOLOGY
Repetition Statements (Loops) - 2
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
While Loops in Python.
Software Development Techniques
Presentation transcript:

ECS15 while

Overview Keep going Use a while loop. while Boolean: while loop iterates through block Do statements in block

While loop example count = 0 while count < 10: count = count+1 print count What is count at the end? How many times is the blue block executed? Talk about sentry variable count, how to initialize, compare, and update What if we have the following count=0 while count<10: count=count+1 print count Count here is called a sentry variable, a variable used in the condition and compared to some other value or values. It needs to be updated (usually).

While loop example How to use while loop to detect correct inputs?

While loop example choosing = True while choosing: answer = raw_input("Choose h or t: ") if answer == "h" or answer == "t": choosing = False I tried a few other examples choosing=False while choosing: print “hello!” Will hello be printed?

In rps program How to keep playing until the user decides to quit? Valid inputs: “r”, “p”, “s”, or “q” Boolean variable picking True while we still need to pick r,p,s A bad input or a good input that is not “q” Set it to False when the user decides to quit Handles BOTH bad inputs and a regular game See rsp2.py Open rps-2 Show error with indentation From “decide who wins” to “You lose!” – won’t end until computer chooses paper. undo it, show proper behavior.

Infinite loop One of the classic programming bugs Get out of it using CRTL-c (hold down control key and type c) Repeat after me: CRTL-c In IDLE: >>>choosing = True >>> while choosing: print "Stuck!“ Do it a couple of times, make it stop !!!

Compound interest Say you invest $100 and make 7% annually After one year you have: $100 + $100*7/100 = $107 You made $7.00 If you leave it invested, and make another 7% the next year, you have: $107 + $107*7/100 = $114.48 You made $7.49

Compound interest The more you have the more you make balance

“Compounded monthly” Instead of computing and adding interest every year, do it every month. Use interest rate of (7/12)% = 0.583% Because you add a little to the balance each month, you make a bit more when compounded monthly than when compounded annually. This is called the Effective Interest Rate

Compute Effective Interest Rate Write a program to calculate it. Use a while loop to iterate through 12 months What about when the principal is not $100? EIR = (balance-principal) *(100/principal) Read eir.py Start with: # Program to figure out Effective Interest Rate # when an annual interest rate is compounded monthly principal = 100.00 print "Principal:",principal annualRate = 9.0 print "Annual interest rate: ",annualRate monthlyRate = annualRate/12.0 Then add: month = 0 balance = principal while month < 12: # This block is done twelve times interest = balance * monthlyRate/100.0 balance = balance + interest month = month+1 print "Balance after one year is:", balance print "Effective Interest Rate is :",\ (balance-principal) Change the indentation of “month=month+1”, see what happens?

Interest on a debt When you are paying interest, compound interest is a bad thing! Credit cards compound monthly, so if your rate is 15%, you really are paying… Change program to

Compound interest program Original debt is $10,000 Every month, make a payment and pay interest Loop until debt is paid off Run my debt program.

Similar to calculators on Web