Repetition – For and Do While

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

Looping Structures: Do Loops
Ch. 3 Variables VB.Net Programming. Data Types Boolean – True or False values Short – Small integers (+/- 32,767) Integer – Medium-size integers (+/-
Applications of Simple Interest Example 1: Eight years ago Ann put some money in a savings account at 5% interest. She checked her account balance and.
What is Compound Interest? Compound interest is interest that is compounded at certain intervals or earned continuously (all the time). Annually: A = P(1.
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 5: Control Structures II (Repetition)
Computer Science 1620 Programming & Problem Solving.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Microsoft Access Using Visual Basic Routines. Visual Basic Datatypes Boolean Byte Currency Date Double Integer Long Object Single String Variant Hyperlink.
Problem Solving with the Sequential Logic Structure Lesson 5 McManusCOP10061.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Nested LOOPS.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Visual Basic Programming
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Lesson 3-1 Example Example 2 Find the sum of 311 and 452 using expanded form. 1.Write the first number in expanded form
solve x + (-16) = -12 solve x + (-16) = X = 4.
Repetition Structures
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
Overview Go over parts of quiz? Another iteration structure for loop.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Lesson 5 McManus COP  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062.
Math – Solving Problems Involving Interest 1.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
You deposit $950 into an account that earns 4 % interest compounded annually. Find the balance in the account after five years. In your last calculation,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 61 Example : For … To… Step For index = 0 To n Step s lstValues.Items.Add(index) Next Control variable Start value Stop value Amount to add to.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
مقدمة في البرمجة Lecture 7. Write VB.net project using the for loop to calculate : 1- the sum of numbers from 1 to (A) numbers. 2- the sum of Odd numbers.
1.2 Adding Integers Adding 3 or more terms. Strategy You can add everything going from left to right Or you can use Commutative and Associative Properties.
Repetition Yonglei Tao. Display Numbers num = 1 lblDisplay.Text = num & ControlChars.NewLine num = 2 * 2 lblDisplay.Text = lblDisplay.Text & num & ControlChars.NewLine.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Week 13 Simple Interest. Lesson Objectives After you have completed this lesson, you will be able to: Represent or solve simple interest problems. Solve.
Perform the Integer operation 1st then a problem
8.3 Compound Interest HW: (1-21 Odds, Odds)
Selected Topics From Chapter 6 Iteration
Consecutive Integer Problems
مراجعة الحاسب.
Outline Altering flow of control Boolean expressions
Lesson 7.7 Simple and Compound Interest
Simple and Compound Interest
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Unit 3 Review (Calculator)
INC 161 , CPE 100 Computer Programming
Chapter 5: Control Structures II (Repetition)
Warm Up Graph each number on a number line. 1. – – –5 –4 –3 –2 –
Fundamental Programming
form the ordered pair solution
Compounded and Continuous Interest
Common pattern/tool: Accumulator loops
Repetition - Counting and Accumulating
Ch 3 Type Conversion Yonglei Tao.
Interest.
Repetition – The For Loop
Presentation transcript:

Repetition – For and Do While Yonglei Tao

Future Balance You put $10,000 into a bank account that earns 5% interest per year. How much will your account balance be in five years?

Calculation After Year Calculation Balance 0 $10,000.00 1 balance = balance + balance * 0.05 $10,500.00 2 balance = balance + balance * 0.05 $11,025.00 3 balance = balance + balance * 0.05 $11,576.25 4 balance = balance + balance * 0.05 $12,155.06 5 balance = balance + balance * 0.05 $12,762.82

The For Loop Dim balance, interest As Double balance = 10000.00 For year As Integer = 1 To 5 interest = balance * 0.05 balance = balance + interest Next year lblDisplay.Text = Format (balance, “currency”)

The Do While Loop Dim balance, interest As Double Dim year As Integer ‘ declare control variable balance = 10000.00 year = 1 ‘ initialize control varaible Do While year <= 5 ‘ test control variable interest = balance * 0.05 balance = balance + interest year = year + 1 ‘ update control variable Loop lblDisplay.Text = Format (balance, “currency”)

A Problem You put $10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original?

Calculation ….. After Year Calculation Balance 0 $10,000.00 0 $10,000.00 1 balance = balance + balance * 0.05 $10,500.00 2 balance = balance + balance * 0.05 $11,025.00 3 balance = balance + balance * 0.05 $11,576.25 4 balance = balance + balance * 0.05 $12,155.06 5 balance = balance + balance * 0.05 $12,762.82 …..

The Do While Loop Dim balance, interest As Double Dim year As Integer = 1 balance = 10000.00 Do While year <= 5 interest = balance * 0.05 balance = balance + interest year = year + 1 Loop lblDisplay.Text = Format (balance, “currency”) balance < 20000

Find the Number of Iterations i = 0.5 Do While i <= 1.3 lblDisplay.Text = lblDisplay.Text & i & “ “ i = i + 0.2 Loop Dim sum As Integer = 0 For i As Integer = 2 To 20 Step 5 sum = sum + i Next i