Lists, Loops, Validation, and More

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 5 Lists, Loops, Validation, and More.
Chapter 5 Lists, Loops, Validation and More Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Week 6 Lists, Loops, Validation, and More. 2 Introduction This chapter covers the Visual Basic looping statements Do … While Do … Until For … Next It.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Controlling Program Flow with Looping Structures
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Loops ISYS 350. Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number.
Chapter 6 Controlling Program Flow with Looping Structures.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Java Programming: Guided Learning with Early Objects
Topics Introduction to Repetition Structures
Chapter 5 – Control Structures: Part 2
Repeating Program Instructions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Outline Altering flow of control Boolean expressions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4: Loops and Files
CIS 16 Application Development Programming with Visual Basic
Chapter 6: Repetition Statements
Tony Gaddis Kip Irvine STARTING OUT WITH Visual Basic 2008
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Presentation transcript:

Lists, Loops, Validation, and More

This chapter covers the Visual Basic .NET looping statements Do … While Do … Until For … Next

Input Boxes Input Boxes Provide a Simple Way to Gather Input Without Placing a Text Box on a Form

Format of the InputBox Function InputBox(Prompt [,Title] [,Default] [,Xpos] [,Ypos]) Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos - X coordinate for the box's position Ypos - Y coordinate for the box's position Title and beyond are optional arguments

Sample InputBox Usage userInput = InputBox("Enter the distance.", "Provide a Value", "150")

Xpos, Ypos, and Twips Xpos specifies the distance from the left of the screen to the left side of the box Ypos, from the top of the screen to the top of the box Both are specified in twips One twip is 1/440 inch

A Loop Is Part of a Program That Repeats The Do While Loop A Loop Is Part of a Program That Repeats

Repetition Structure (or Loop) Visual Basic .NET has three structures for repeating a statement or group of statements Do While Do Until For Next

Do While Flowchart The Do While loop If/While the expression is true, the statement(s) are executed Expression statement(s) True False

Do While Syntax "Do", "While", and "Loop" are new keywords The statement, or statements are known as the body of the loop Do While expression statement(s) Loop

Do While Example Private Sub btnRunDemo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRunDemo.Click ' Demonstrate the Do While loop Dim count As Integer = 0 Do While count < 10 lstOutput.Items.Add("Hello") count += 1 Loop End Sub

Infinite Loops Generally, if the expression is true and, hence the starts executing: Something with the body of the loop must eventually make the test expression false Otherwise, the Do While loop will continuously loop forever - called an infinite loop

Counters Variables called counters are frequently used to control Do While loops (see count in the previous example Counters are invariably initialized before the loop begins (above: Dim count As Integer = 0) They are also usually modified within the body of the loop (above: count += 1)

Pretest vs. Posttest Loops The preceding Do While loops were written in their pretest syntax The expression is always tested before the body of the loop is executed Do While loops also have a posttest form In these, the body of the loop is always executed first, then the expression is evaluated to check to see if additional iterations are needed

Posttest Do While Syntax and Flowchart statement(s) Loop While expression statement(s) The statement(s) will always be done once, irrespective of the expression used Expression True False

Example: Keeping a Running Total count = 1 ' Initialize the counter total = 0 ' Initialize total Do input = InputBox("Enter the sales for day " & _ count.ToString, "Sales Amount Needed") If input <> "" Then sales = CDec(input) total += sales ' Add sales to total count += 1 ' Increment the counter End If Loop While count <= 5

The Do Until and For Next Loops The Do Until Loop Iterates Until Its Test Expression Is True The For...Next Loop Is Designed to Use a Counter Variable and Iterates a Specific Number of Times

Do Until: Pretest and Posttest Forms Do Until expression statement(s) Loop Do statement(s) Loop Until expression

Pretest Do Until Example input = InputBox("How many test scores do you want " & _ "to average?", "Enter a Value") numScores = Val(input) total = 0 count = 1 Do Until count > numScores input = InputBox("Enter the value for test score " & _ count.ToString, "Test Score Needed") total = total + Val(input) count = count + 1 Loop

For … Next Loop, I The syntax is Where 'For', 'To', and 'Next' are keywords Other details follow … For CounterVariable = StartValue To EndValue _[Step] statement Next [CounterVariable]

For … Next Loop, II CounterVariable is a numeric counter variable StartValue is the initial value of the counter EndValue gives the number to test for completion Step indicates the increment for count at the end of each iteration; it is optional and defaults to 1 if not specified

For…Next Flowchart False statement(s) True set counter to StartValue EndValue? statement(s) increment counter False True

For…Next Example For count = 1 To 10 square = count ^ 2 str = "The square of " & count.ToString & " is " & _ square.ToString lstOutput.Items.Add(str) Next count

More on the StepValue It is optional, if not specified, it defaults to 1 It may be negative, in which case the loop counts downwards For x = 0 To 100 Step 10 MessageBox.Show("x is now " & x.ToString) Next x For x = 10 To 1 Step -1 MessageBox.Show("x is now " & x.ToString) Next x

Exiting a Loop Prematurely In some situations it is convenient to be able to gracefully end a loop early Exit Do (used in Do While or Until loops) Exit For (used in For Next loops) Will accomplish that task Since these override the normal loop termination mechanism, they should be used sparingly

Exiting a Loop Prematurely, Example maxNumbers = CInt(InputBox("How many numbers do " & _ "you wish to sum?")) total = 0 For x = 1 to maxNumbers input = InputBox("Enter a number.") If input = "" Then Exit For Else num = CDbl(input) total += num End If Next x MessageBox.Show("The sum of the numbers is " & total.ToString)

When to Use the Do While Loop Use the Do While loop when you wish the loop to repeat as long as the test expression is true You can write the Do While loop as a pretest or posttest loop Pretest Do While loops are ideal when you do not want the loop to iterate if the test expression is false from the beginning Posttest loops are ideal when you always want the loop to iterate at least once

When to Use the Do Until Loop Use the Do Until loop when you wish the loop to repeat until the test expression is true You can write the Do Until loop as a pretest or posttest loop Pretest Do Until loops are ideal when you do not want the loop to iterate if the test expression is true from the beginning Posttest loops are ideal when you always want the loop to iterate at least once

When to Use the For Next Loop The For...Next loop is a pretest loop that first initializes a counter variable to a starting value It automatically increments the counter variable at the end of each iteration The loop repeats as long as the counter variable is not greater than an end value The For...Next loop is primarily used when the number of required iterations is known

Nested Loops Nested Loops Are Necessary When a Task Performs a Repetitive Operation and That Task Itself Must Be Repeated

Nested Loop Example, I For hours = 0 To 24 lblHours.Text = hours.ToString For minutes = 0 To 59 lblMinutes.Text = minutes.ToString For seconds = 0 To 59 lblSeconds.Text = seconds.ToString Next seconds Next minutes Next hours