Repetition. October 4, Chapter 6 - VB 2005 by Schneider 1

Similar documents
Chapter 6 Repetition. 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops

Review. October 20, 2006

Repetition and Loop Statements Chapter 5

Loop Structures. Loop Structures. Algorithm to record 5 TV programmes. Recall Structured Programming..3 basic control structures.

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Chapter 4: Control structures. Repetition

REPETITION CONTROL STRUCTURE LOGO

Chapter 6 Repetition

Chapter 4: Control structures

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Introduction. C provides two styles of flow control:

Loops / Repetition Statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

Repetition Structures

CS110D: PROGRAMMING LANGUAGE I

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

STUDENT LESSON A12 Iterations

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Unit 7. Lesson 7.1. Loop. For Next Statements. Introduction. Loop

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Programming Logic and Design Sixth Edition

A Beginner s Guide to Programming Logic, Introductory. Chapter 5 Looping

Structured Programming. Dr. Mohamed Khedr Lecture 9

Iteration and Arrays Dr. Abdallah Mohamed

Introducing Visual Basic

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Cambridge International Examinations Cambridge International Advanced Subsidiary and Advanced Level. Published

Lecture 7 Tao Wang 1

ADMIN STUFF. Assignment #1 due. Assignment #2. Midterm. Posted to website Due Oct 11. Review session Oct 16 Midterm in class Oct 18 [2 hours long]

Increment and the While. Class 15

Control Structures II. Repetition (Loops)

Loops. CSE 114, Computer Science 1 Stony Brook University

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Computational Physics - Fortran February 1997

Microsoft Visual Basic 2015: Reloaded

Additional Controls & Objects

Microsoft Visual Basic 2005: Reloaded

PLD Semester Exam Study Guide Dec. 2018

ECE 122. Engineering Problem Solving with Java

204111: Computer and Programming

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Repetition Algorithms

Chapter 5: Loops and Files

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Conditionals and Loops

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)


Loops (while and for)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Loops. Repeat after me

Principles of Computer Science I

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Test #2 October 8, 2015

PROBLEM SOLVING WITH LOOPS. Chapter 7

Why Is Repetition Needed?

Loops / Repetition Statements

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Introducing Visual Basic

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

Chapter 2B: Lists and Loops

Computers Programming Course 7. Iulian Năstac

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Text Input and Conditionals

Chapter Goals. Contents LOOPS

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

HOW TO DEVELOP A VB APPLICATION

3. Can every Do-Loop loop be written as a For-Next loop? Why or why not? 4. Name two types of files that can be opened and used in a VB program.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Microsoft Visual Basic 2015: Reloaded

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

CSE 123 Introduction to Computing

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Copyright 2014 Pearson Education, Inc. Chapter 5. Lists and Loops. Copyright 2014 Pearson Education, Inc.

Iteration -- Once Is Not Enough

LOOPS. Repetition using the while statement

Recap: Assignment as an Operator CS 112 Introduction to Programming

Chapter 5. Lists and Loops Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Lecture Transcript While and Do While Statements in C++

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Repetition, Looping. While Loop

Chapter 3: Program Statements

CSC180 Homework 3 (Due November 29, 2018, 6 pages)

Repetition CSC 121 Fall 2014 Howard Rosenthal

Transcription:

Repetition October 4, 2006 Chapter 6 - VB 2005 by Schneider 1

Chapter 6 Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan Chapter 6 - VB 2005 by Schneider 2

6.1 Do Loops A loop is one of the most important structures in programming. Used to repeat a sequence of statements a number of times. The Do loop repeats a sequence of statements either as long as or until a certain condition is true. Chapter 6 - VB 2005 by Schneider 3

Do Loop Syntax Do While condition statement(s) Loop Condition is tested, If it is True, the loop is run. If it is False, the statements following the Loop statement are executed. These statements are inside the body of the loop and are run if the condition above is True. Chapter 6 - VB 2005 by Schneider 4

Pseudocode and Flow Chart for a Do Loop Chapter 6 - VB 2005 by Schneider 5

Example 1 Private Sub btndisplay_click(...) _ Handles btndisplay.click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstnumbers.items.add(num) num += 1 'Add 1 to the value of num Loop End Sub Chapter 6 - VB 2005 by Schneider 6

Example: Repeat Request as Long as Response in Incorrect Dim password As String = "" Do While password <> "SHAZAM" password = InputBox("What is the password?") password = password.toupper Loop password is the loop control variable because the value stored in password is what is tested to determine if the loop should continue or stop. Chapter 6 - VB 2005 by Schneider 7

Post Test Loop Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run again. If it is True, the statements following the Loop statement are executed. Chapter 6 - VB 2005 by Schneider 8

Example: Repeat Request Until Proper Response is Given Do password = InputBox("What is the password?") password = password.toupper Loop Until password = "SHAZAM" Chapter 6 - VB 2005 by Schneider 9

Pseudocode and Flowchart for a Post-Test Loop Chapter 6 - VB 2005 by Schneider 10

Example 4: Form txtamount txtwhen Chapter 6 - VB 2005 by Schneider 11

Example 4: Code Private Sub btncalculate_click(...) Handles btncalculate.click Dim balance As Double, numyears As Integer balance = CDbl(txtAmount.Text) Do While balance < 1000000 balance += 0.06 * balance numyears += 1 Loop txtwhen.text = "In " & numyears & _ " years you will have a million dollars." End Sub Chapter 6 - VB 2005 by Schneider 12

Example 4: Output Chapter 6 - VB 2005 by Schneider 13

Comments Be careful to avoid infinite loops loops that never end. Visual Basic allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop. This textbook will use only While at the top and only Until at the bottom. Chapter 6 - VB 2005 by Schneider 14

6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags Nested Loops Chapter 6 - VB 2005 by Schneider 15

Processing Lists of Data with Do Loops Display all or selected items from lists Search lists for specific items Perform calculations on the numerical entries of a list Chapter 6 - VB 2005 by Schneider 16

Peek Method Data to be processed are often retrieved from a file by a Do loop To determine if we have reached the end of the file from which we are reading, we use the Peek method. Chapter 6 - VB 2005 by Schneider 17

Peek Example Suppose a file has been opened as a StreamReader object named sr. sr.peek is the ANSI value of the first character of the line about to be read with ReadLine. If the end of the file has been reached, the value of sr.peek is -1 Chapter 6 - VB 2005 by Schneider 18

Example 1: Display the Total Contents of a File Dim sr As IO.StreamReader = _ IO.File.OpenText("PHONE.TXT") lstnumbers.items.clear() Do While sr.peek <> -1 name = sr.readline phonenum = sr.readline lstnumbers.items.add(name & " " _ & phonenum) Loop sr.close() Chapter 6 - VB 2005 by Schneider 19

Pseudocode and Flowchart for Processing Data from a File Chapter 6 - VB 2005 by Schneider 20

Example 2: Form txtname txtnumber Chapter 6 - VB 2005 by Schneider 21

Example 2: Partial Code Do While (name <> txtname.text) _ Loop name = sr.readline phonenum = sr.readline And (sr.peek <> -1) As long as the name being searched for has not been found AND the end of the file has not been reached, the loop will continue Chapter 6 - VB 2005 by Schneider 22

Counters and Accumulators A counter is a numeric variable that keeps track of the number of items that have been processed. An accumulator is a numeric variable that totals numbers. Chapter 6 - VB 2005 by Schneider 23

File COINS.TXT 1 1 5 10 10 25 Count the number of coins and determine the total value Chapter 6 - VB 2005 by Schneider 24

Example 3: Partial Code sum is an accumulator. It is used to total up the values of the coins. Dim numcoins As Integer = 0 Dim sum As Integer = 0 Dim coin As String Do While sr.peek <> -1 Loop coin = sr.readline numcoins += 1 sum += CDbl(coin) numcoins is a counter, it increases by 1 each time through the loop Chapter 6 - VB 2005 by Schneider 25

Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean. Chapter 6 - VB 2005 by Schneider 26

Example 4: Form The file WORDS.TXT contains words from a spelling bee, one word per line. Count the words and determine whether they are in alphabetical order. Chapter 6 - VB 2005 by Schneider 27

Example 4: Partial Code Dim word1 As String = "" Dim orderflag As Boolean = True Do While (sr.peek <> -1) word2 = sr.readline wordcounter += 1 If word1 > word2 Then orderflag = False End If word1 = word2 Loop Chapter 6 - VB 2005 by Schneider 28

Nested Loops Statements inside a loop can contain another loop. Chapter 6 - VB 2005 by Schneider 29

More About Flags When flagvar is a variable of Boolean type, the statements If flagvar = True Then and If flagvar = False Then can be replaced by If flagvar Then and If Not flagvar Then Chapter 6 - VB 2005 by Schneider 30

Flags continued The statements Do While flagvar = True and Do While flagvar = False can be replaced by Do While flagvar and Do While Not flagvar Chapter 6 - VB 2005 by Schneider 31

6.3 For Next Loops Declarations of Control Variables Nested For Next Loops Chapter 6 - VB 2005 by Schneider 32

For Next Loops Used when we know how many times we want the loop to execute A counter controlled loop Chapter 6 - VB 2005 by Schneider 33

Sample Dim i As Integer For i = 1 To 5 lsttable.items.add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized to 1 Tested against the stop value, 5 Incremented by 1 at the Next statement Chapter 6 - VB 2005 by Schneider 34

Do While equivalent i = 1 Do While i <= 5 lsttable.items.add(i & " " & i ^ 2) i += 1 Loop Chapter 6 - VB 2005 by Schneider 35

For Next Loop Syntax Chapter 6 - VB 2005 by Schneider 36

Example 1: Output Chapter 6 - VB 2005 by Schneider 37

Example 1: Code Dim yr As Integer Dim pop as Double = 300000 Dim fmtstr As String = _ "{0,4}{1,12:N0}" For yr = 2006 To 2010 lstpop.items.add(string.format( _ Next pop += 0.03 * pop fmtstr, yr, pop) Chapter 6 - VB 2005 by Schneider 38

Example 2 Control variable Start value Stop value Amount to add to index For index = 0 To n Step s Next lstvalues.items.add(index) Chapter 6 - VB 2005 by Schneider 39

Example with Negative Step Dim j As Integer For j = 10 To 1 Step -1 lstbox.items.add(j) Next lstbox.items.add("blastoff") Chapter 6 - VB 2005 by Schneider 40

Declaration Inside For Statement Dim i As Integer For i = 1 To 5 lsttable.items.add(i & " " & i ^ 2) Next For i As Integer = 1 To 5 lsttable.items.add(i & " " & i ^ 2) Next Chapter 6 - VB 2005 by Schneider 41

Nested For Next Loops Chapter 6 - VB 2005 by Schneider 42

Outer loop Example: Seats on an Airplane For i As Integer = 65 To 70 Inner loop For j As Integer = 1 To 25 Next Next lstbox.items.add(chr(i) & j) OUTPUT: A1 A2 : A3 Chapter 6 - VB 2005 by Schneider 43

For and Next Pairs For and Next statements must be paired. If one is missing, the automatic syntax checker will complain with a wavy underline and a message such as A For must be paired with a Next. Chapter 6 - VB 2005 by Schneider 44

Start, Stop, and Step values Consider a loop beginning with For i As Integer = m To n Step s. The loop will be executed exactly once if m equals n no matter what value s has. The loop will not be executed at all if m is greater than n and s is positive, or if m is less than n and s is negative. Chapter 6 - VB 2005 by Schneider 45

Altering the Control Variable The value of the control variable should not be altered within the body of the loop. Doing so might cause the loop to repeat indefinitely or have an unpredictable number of repetitions. Chapter 6 - VB 2005 by Schneider 46

Non-integer Step Values Can lead to round-off errors with the result that the loop is not executed the intended number of times. Chapter 6 - VB 2005 by Schneider 47

Non-integer Step Example A loop beginning with For i As Double = 1 To 2 Step 0.1 will be executed only 10 times instead of the intended 11 times. It should be replaced with For i As Double = _ 1 To 2.01 Step 0.1 Chapter 6 - VB 2005 by Schneider 48