CHAPTER SIX Loop Structures.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
5.05 Apply Looping Structures
Office XP Introductory Concepts and Techniques Windows XP Edition M i c r o s o f t Windows XP Project An Introduction to Windows XP Professional and Office.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Copyright 2007, Information Builders. Slide 1 Maintain & JavaScript: Two Great Tools that Work Great Together Mark Derwin and Mark Rawls Information Builders.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Office 2013 and Windows 8: Essential Concepts and Skills
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Microsoft Windows Vista Chapter 1 Fundamentals of Using Microsoft Windows Vista.
WINDOWS Part 1 – Start Up Basics
Microsoft Windows 7 Part 1 Fundamentals of Using Windows 7.
Chapter 12: How Long Can This Go On?
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Windows Tutorial Common Objects ACOS: 1, 4. Using the Taskbar 1. Using the taskbar, you can switch between open programs and between open documents within.
© Paradigm Publishing Inc. MICROSOFT WINDOWS XP MAINTAINING FILES AND CUSTOMIZING WINDOWS Section 2.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Project 2 Working on the Windows XP Desktop Windows XP Service Pack 2 Edition Comprehensive Concepts and Techniques.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Microsoft Visual Basic 2010 CHAPTER TWO Program and Graphical User Interface Design.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Creating and Editing a Web Page
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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()
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Excel Tutorial 8 Developing an Excel Application
Chapter 2: The Visual Studio .NET Development Environment
CHAPTER FIVE Decision Structures.
Using Procedures and Exception Handling
CHAPTER FIVE Decision Structures.
Variables and Arithmetic Operations
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

CHAPTER SIX Loop Structures

Objectives Add a MenuStrip object Use the InputBox function Display data using the ListBox object Understand the use of counters and accumulators Understand the use of compound operators Chapter 6: Loop Structures

Objectives Repeat a process using a For…Next loop Repeat a process using a Do loop Avoid infinite loops Prime a loop ( initialize LCV ) Validate data ( isNumeric function ) Chapter 6: Loop Structures

Objectives Create a nested loop Select the best type of loop Do While For Debug using DataTips at breakpoints Publish a finished application using ClickOnce deployment technology Chapter 6: Loop Structures

Program Design Chapter 6: Loop Structures

Program Design Chapter 6: Loop Structures

Event Planning Document Chapter 6: Loop Structures

Introduction A fundamental process in a computer program is to repeat a series of instructions either while a condition is true (or not true) or until a condition is true (or not true) The process of repeating a set of instructions while a condition is true or until a condition is true is called looping Synonyms for looping include repetition and iteration Chapter 6: Loop Structures

MenuStrip Object A menu bar is a strip across the top of a window that contains one or more menu names A menu is a group of commands, or items, presented in a list File Clear Exit Chapter 6: Loop Structures

MenuStrip Object [ configuration ] Click the Type Here box on the menu bar. Type &File to identify the File menu, and then press the ENTER key Click File in the MenuStrip object to select it, scroll in the Properties window to the (Name) property, and then change the name to mnuFileMenu To add a menu item to the File menu, click the Type Here box below the File menu name. Type &Clear and then press ENTER to create a new menu item named Clear with C as the hot key On the File menu, click Clear to select it, scroll in the Properties window until the (Name) property is visible, and then change the name to mnuClearItem Chapter 6: Loop Structures

MenuStrip Object Chapter 6: Loop Structures

Event Handlers for Menu Items In Design view, double-click the Exit menu item to open the code editing window ( assumes we have added an exit command to our menu… ) Using IntelliSense, enter the Close procedure call to close the window and terminate the application Chapter 6: Loop Structures

Standard Items for a Menu Visual Basic 2008 contains an Action Tag that allows you to create a full standard menu bar commonly provided in Windows programs Action tags provide a way for you to specify a set of actions, called smart actions, for an object as you design a form With a new Windows Form object open, drag the MenuStrip .NET component onto the Windows Form object. Click the Action Tag on the MenuStrip object Click Insert Standard Items on the MenuStrip Tasks menu Click File on the menu bar to view the individual menu items and their associated icons on the File menu Chapter 6: Loop Structures

Standard Items for a Menu Chapter 6: Loop Structures

InputBox Function The InputBox function displays a dialog box that consists of: a message asking for input, an input area, a title bar with a caption, an OK button and a Cancel button When the user enters the text and clicks the OK button, the InputBox function returns this text as a string If the user clicks the Cancel button, the function returns a null string ("") ( aka empty string ) Chapter 6: Loop Structures

InputBox Object Default Value The InputBox object can be assigned a default value Chapter 6: Loop Structures

InputBox Object for Highway Radar Checkpoint Application Chapter 6: Loop Structures Default Value

Displaying Data Using a ListBox Object Drag the ListBox object from the Toolbox to the Windows Form object where you want to place the ListBox object. When the pointer is in the correct location, release the left mouse button With the ListBox object selected, scroll in the Properties window to the (Name) property. Name the ListBox object lstRadarSpeed Chapter 6: Loop Structures

Displaying Data Using the Items Property of a ListBox Object Chapter 6: Loop Structures

Adding Items During at Design Time Assume the lstStores ListBox object already has been placed and named on the Windows Form object. Select the ListBox object on the Windows Form object and then click the Items property in the Properties window Click the ellipsis button in the right column of the Items property Click in the String Collection Editor window. Type the following items to represent popular retail stores, pressing ENTER at the end of each line: Abercrombie & Fitch Aeropostale American Eagle Express Hollister Click the OK button Chapter 6: Loop Structures

Adding Items at Design Time Chapter 6: Loop Structures

ListBox SelectedItem Property No caption, no icons, no buttons… Chapter 6: Loop Structures

Accumulators, Counters, and Compound Operators A variable that contains an accumulated value such as the total of all the speeds is called an accumulator A variable that always is incremented by a constant value is called a counter For this application, a counter is used to keep track of how many vehicle speeds the user has entered Chapter 6: Loop Structures

Accumulators, Counters, and Compound Operators A compound operator allows you to add, subtract, multiply, divide, use modulus or exponents, or concatenate strings, storing the result in the same variable Chapter 6: Loop Structures

Accumulators, Counters, and Compound Operators Chapter 6: Loop Structures

Accumulators, Counters, and Compound Operators Chapter 6: Loop Structures

Using Loops to Perform Repetitive Tasks In the Highway Radar Checkpoint application, the user enters up to 10 vehicle speeds using the InputBox function The repetitive process of entering 10 vehicle speeds can be coded within a loop to simplify the task with fewer lines of code Each repetition of the loop is called an iteration Chapter 6: Loop Structures

Repeating a Process Using the For…Next Loop You can use a For...Next loop when a section of code is to be executed an exact number of times Chapter 6: Loop Structures

Repeating a Process Using the For…Next Loop Chapter 6: Loop Structures

Step Value in a For…Next Loop A Step value is the value in a For...Next loop that is added to or subtracted from the beginning value on each iteration of the loop Default step value is 1 Can be positive or negative, contain decimals, or include variables and mathematical expressions Chapter 6: Loop Structures

Entering the For…Next Loop Code Chapter 6: Loop Structures

Repeating a Process Using a Do Loop In a Do loop, the body of the loop is executed while or until a condition is true or false The Do While loop executes as long as the condition is true The Do Until loop executes until the condition becomes true A top-controlled loop is tested before the loop is entered ( aka pre-test loop ) Body might not be executed at all Bottom-controlled loops test the condition at the bottom of the loop, so the body of a bottom-controlled loop is executed at least once ( aka post-test loop ) Body executes at least once Chapter 6: Loop Structures

Top-Controlled Do While Loops A top-controlled Do While loop begins with the keywords Do While. Next, the condition is specified The body of the loop contains the instructions that are executed as long as the condition is true A loop that does not end is called an infinite loop Chapter 6: Loop Structures

Top-Controlled Do While Loops Chapter 6: Loop Structures

Entering a Do Loop Using IntelliSense In the code editing window, enter the intScore variable declaration and then press the ENTER key. Type Do While, a space, and then press CTRL+SPACEBAR to display the IntelliSense list. Type intS to highlight intScore in the list Type < 5 and then press the ENTER key. Press CTRL+SPACEBAR to display the IntelliSense list. Type intS to highlight the intScore variable. Complete the statement by typing += 1 and then pressing the ENTER key. Press the DELETE key to delete the blank line Chapter 6: Loop Structures

Entering a Do Loop Using IntelliSense Chapter 6: Loop Structures

Bottom-Controlled Do While Loop A bottom-controlled loop works the same way as the top-controlled Do While loop The body of the loop is executed before the condition is checked the first time, guaranteeing at least one iteration of a loop will be completed Chapter 6: Loop Structures

Bottom-Controlled Do While Loop Chapter 6: Loop Structures

Do Until Loops Chapter 6: Loop Structures

User Input using a Loop Do loops often are written to end the loop when a certain value is entered by the user ( known as a sentinel value ), or the user performs a certain action such as clicking the Cancel button in an input box Chapter 6: Loop Structures

Avoiding Infinite Loops An infinite loop is a loop that never ends Chapter 6: Loop Structures

Priming the Loop Starting a loop with a preset value in the variable(s) tested in the condition is called priming the loop ( initializing ) Chapter 6: Loop Structures

Validating Data Chapter 6: Loop Structures

Creating a Nested Loop Any loop can be placed within another loop under the following conditions: Interior loops must be completely contained inside the outer loop Must have a different loop control variable Chapter 6: Loop Structures

Selecting the Best Loop Use a Do loop if the number of repetitions is unknown and is based on a condition changing; a For...Next loop is best if the exact number of repetitions is fixed If a loop condition must be tested before the body of the loop is executed, use a top-controlled Do While or Do Until loop. If the instructions within a loop must be executed at least one time regardless of the status of a condition, use a bottom-controlled Do While or Do Until loop Use the keyword While if you want to continue execution of the loop while the condition is true. Use the keyword Until if you want to continue execution until the condition is true Chapter 6: Loop Structures

Using a DataTip with Breakpoints Resolving defects ( logic errors ) in code is called debugging A good way to collect information is to pause the execution of the code where a possible error could occur Breakpoints are stop points placed in the code to tell the Visual Studio 2008 debugger where and when to pause the execution of the application The line flagged with the breakbpoint is NOT executed While in break mode, you can examine the values in all variables that are within the scope of execution through the use of DataTips ( work like tool tips ) Chapter 6: Loop Structures

Using a DataTip with Breakpoints With the program open in the code editing window, right-click line 49, which contains the code where you want to set a breakpoint, and then point to Breakpoint on the shortcut menu Click Insert Breakpoint on the submenu To run and test the program with the breakpoint, click the Start Debugging button on the Standard toolbar Click the Enter Speed button. Type 75 as the speed of the first vehicle Click the OK button in the input box Chapter 6: Loop Structures

Using a DataTip with Breakpoints Point to the variable decVehicleSpeed on line 49 You can view the value in any other variable within execution scope by pointing to that variable. To illustrate, point to the variable decTotalOfAllSpeeds on line 49 Continue the program by clicking the Continue button on the Standard toolbar. Notice that the Continue button is the same as the Start Debugging button Point to the decTotalOfAllSpeeds variable Chapter 6: Loop Structures

Using a DataTip with Breakpoints Chapter 6: Loop Structures

Using a DataTip with Breakpoints To remove a breakpoint, right-click the statement containing the breakpoint, and then point to Breakpoint on the shortcut menu Click Delete Breakpoint on the Breakpoint submenu Chapter 6: Loop Structures

Publishing an Application with Click Once Deployment After an application is completely debugged and working properly, you can deploy the project Deploying a project means placing an executable version of the program on your hard disk, on a Web server, or on a network server When programming using Visual Basic 2008, you can create a deployed program by using ClickOnce Deployment The deployed version of the program you create can be installed and executed on any computer that has the .NET framework installed Chapter 6: Loop Structures

Publishing an Application with ClickOnce Deployment With the program open, click Build on the menu bar Click Publish Radar on the Build menu Change the default location from publish\ to a file location. To publish to a USB drive, type the drive letter. In this example, enter e: for a USB drive Click the Next button. If necessary, click the From a CD-ROM or DVDROM radio button Click the Next button. If necessary, click the The application will not check for updates radio button Chapter 6: Loop Structures

Publishing an Application with ClickOnce Deployment Click the Next button Click the Finish button To view the finished result, minimize the Visual Studio window, and then double-click the My Computer icon on the desktop. Double-click the USB drive icon to view the published installation folder To install the application, double-click the setup file After installation, the program will run. To run the installed application again, click the Start button on the Windows taskbar. Point to All Programs, point to Radar on the All Programs menu, and then click Radar on the Radar submenu Chapter 6: Loop Structures

Publishing an Application with ClickOnce Deployment Chapter 6: Loop Structures

Program Design Chapter 6: Loop Structures

Program Design Chapter 6: Loop Structures

Event Planning Document Chapter 6: Loop Structures

Summary Add a MenuStrip object Use the InputBox function Display data using the ListBox object Understand the use of counters and accumulators Understand the use of compound operators Chapter 6: Loop Structures

Summary Repeat a process using a For…Next loop Repeat a process using a Do loop Avoid infinite loops Prime a loop ( initialize variables ) Validate data Chapter 6: Loop Structures

Summary Create a nested loop Select the best type of loop Debug using DataTips at breakpoints Publish a finished application using ClickOnce technology Chapter 6: Loop Structures

CHAPTER SIX COMPLETE Loop Structures