General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.

Slides:



Advertisements
Similar presentations
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
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.
Debt 1. Interest rate: Annual Percentage Rate APR 2.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 4 Loops Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X.
Fixed Rate and Fixed Duration Loans You need to know three things about a fixed rate, fixed duration loan: How much do you need to borrow? What is the.
1 Repetition structures Overview while statement for statement do while statement.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Loop Exercise 1 Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
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.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Decision Making and Branching (cont.)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
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#
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
UCT Department of Computer Science Computer Science 1015F Iteration
Control Structures II Chapter 3
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Learning About the Loop Structure (continued)
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.
Outline Altering flow of control Boolean expressions
Java Programming Loops
Module 4 Loops.
Repetition Control Structure
Looping and Repetition
Presentation transcript:

General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change some value so that the condition becomes false. Otherwise, if the loop does run, it becomes an infinite loop.

Example int a = 19; while (a > 0) { System.out.println(a % 2); a = a / 2; }

Loop Exercise 1 Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will it take you to pay off the loan and how much interest will you pay? An amortization table will show you how much you owe each month, the number of months to pay off the loan, and the total interest.

Amortization Table Mth Payment Amount Total Interest =

Amortization Calculation The interest owed at the end of a month is: interest = amount * APR/1200 The amount owed at the end of a month is: amount += interest The payment is the either the fixed payment amount ($1,200 in this case) or the total amount, if the total amount is less than the fixed payment. The new amount is the amount - payment.

Program Write a program that will read in the amount of the loan, the Annual Percentage Rate (APR), and the amount of the fixed payment, and then will calculate and print out the amortization table and then the total amount of interest paid on the loan. The program should prompt the user for the inputs and print out the table formatted properly with labeled columns.

Sample Output Enter amount of loan> Enter APR> 18 Enter monthly payment> 1200 Mth Payment Amount Total Interest =

Loop Exercise 2 A prime number is an integer greater 1 than is divisible only by 1 and itself. For example, 2 is a prime, 3 is a prime, but 4 is not a prime (4 is divisible by 2). Write a program to print out all the prime numbers between 1 and 100, printing ten numbers per line.

Sample Output Primes between 1 and

Prime Calculation To check if one number divides another, just check if the remainder after division is 0. For example if (i % j == 0) then j is a factor of i (and if j is neither i n or 1, i is not prime). Hint: It would be helpful to use a boolean variable, isPrime, to record whether or not the number is prime.

Do-While Loop The third Java statement for implementing loops in the Do-While statement, which looks like: do while( ) The only difference between the Do-While statement and the While Statement is that the test is checked after the loop body is executed. Thus, the loop body is always done at least one time.

Example One common usage of the Do-While statement is to read in input until a valid value is read. For example, in the guessing game, ask the user for a number be 1 and 10 (inclusive): do { System.out.print("Pick a number between 1 and 10 >"); n = sc.nextInt(); } while(n 10);