CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.

Slides:



Advertisements
Similar presentations
Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.
Advertisements

Chapter 6 - VB 2005 by Schneider1 Do Loop Syntax Do While condition statement(s) Loop Condition is tested, If it is True, the loop is run. If it is False,
Looping Structures: Do Loops
CS0004: Introduction to Programming Repetition – Do Loops.
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
CS 0004 –Lecture 8 Jan 24, 2011 Roxana Gheorghiu.
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:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Chapter 12: How Long Can This Go On?
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Visual Basic Programming
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
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.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Chapter 6 - VB 2005 by Schneider1 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:
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
CS 4 Intro to Programming in Visual Basic Files and Loops (2) Patchrawat Uthaisombut University of Pittsburgh 1.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Adding Code to the Option Button. Open VB 1.Double click the Calculate button and select General from the Object list box. 2.Add the following code to.
Data and variables in Visual Basic. Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download: 
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
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.
Chapter 6 Controlling Program Flow with Looping Structures.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Chapter 6 - VB 2008 by Schneider1 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:
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.
Visual Basic Fundamental Concepts
Chapter 6: Loops.
Visual Basic 6 (VB6) Data Types, And Operators
3rd prep. – 2nd Term MOE Book Questions.
Introducing Do While & Do Until Loops & Repetition Statements
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 5 Structures.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Visual Basic..
Department Array in Visual Basic
Objectives After studying this chapter, you should be able to:
Chapter (3) - Looping Questions.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Chapter 6 - VB.Net by Schneider
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Presentation transcript:

CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider

Chapter 6 - VB 2005 by Schneider2 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

How to write the following program? The user inputs a number x. The program displays the multiplication table for x. P. Uthaisombut3

User Interface Design Use a Text Box to accept a number Use a Button to accept the “print table” command from the user Use a List Box to show the multiplication table. Put a Label near the Text Box to give a brief instruction to the user P. Uthaisombut4

User Interface Design P. Uthaisombut5

Expected Results P. Uthaisombut6

Technical issues When does the program respond to the user? How do we convert text input from the Text Box into an integer? How do we add a line into a List Box? How do we construct the right string on each line of the List Box? (How do we construct the string “5 * 1 = 5”, etc. ?) P. Uthaisombut7

Technical Issues How do we add exactly 12 lines into the List Box? P. Uthaisombut8

One solution Put 12 lstTable.Items.Add(…) in the code But there is a better solution … P. Uthaisombut9

Chapter 6 - VB 2005 by Schneider 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 Schneider11 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 Schneider12 Pseudocode and Flow Chart for a Do Loop

Simple Illustration of Do Loop When the user click the button, display 1, 2,…, 7 on the List Box. P. Uthaisombut13

Chapter 6 - VB 2005 by Schneider14 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

Back to our multiplication table Public Class Form1 Private Sub btnDisplay_Click(…) Handles btnDisplay.Click Dim x As Integer Dim y As Integer x = CInt(txtInput.Text) y = 1 Do While y <= 12 lstTable.Items.Add(x & " * " & y & " = " & x * y) y += 1 Loop End Sub End Class P. Uthaisombut15

Chapter 6 - VB 2005 by Schneider16 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 Schneider17 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 Schneider18 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 Schneider19 Pseudocode and Flowchart for a Post-Test Loop

Chapter 6 - VB 2005 by Schneider20 Example 4: Form txtAmount txtWhen

Chapter 6 - VB 2005 by Schneider21 Example 4: Code Private Sub btnCalculate_Click(...) Handles btnCalculate.Click Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text) Do While balance < 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 Schneider22 Example 4: Output

Chapter 6 - VB 2005 by Schneider23 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.