Microsoft Visual Basic 2012 CHAPTER SIX Loop Structures

Microsoft Visual Basic 2012 CHAPTER SIX Loop Structures

6 Objectives ►Add a Menu. Strip object ►Use the Input. Box function ►Display data using the List. Box object ►Understand the use of counters and accumulators ►Understand the use of compound operators Chapter 6: Loop Structures 2

6 Objectives ►Repeat a process using a For…Next loop ►Repeat a process using a Do loop ►Avoid infinite loops ►Prime a loop ►Validate data Chapter 6: Loop Structures 3

6 Objectives ►Create a nested loop ►Select the best type of loop ►Debug using Data. Tips at breakpoints ►Publish a finished application using Click. Once technology Chapter 6: Loop Structures 4

6 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 • Another term for looping is iteration Chapter 6: Loop Structures 5

6 User Interface Design ►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 Chapter 6: Loop Structures 6

6 User Interface Design ► With a Windows Form object open in the Visual Studio window, scroll in the Toolbox to display the Menus & Toolbars category. If the category is not open, tap or click the expand icon (the right-pointing triangle) next to the Menus & Toolbars category name. Drag the Menu. Strip. NET component to the Windows Form object ► Release the mouse button ► With the Menu. Strip object selected, scroll in the Properties window until the (Name) property is visible. Change the Menu. Strip object name to mnu. Fitness ► Tap or click the Type Here box on the menu bar. Type &File to identify the File menu, and then press the ENTER key Chapter 6: Loop Structures 7

6 User Interface Design ► Tap or click File in the Menu. Strip object to select it, scroll in the Properties window to the (Name) property, and then change the name to mnu. File ► To add a menu item to the File menu, tap or 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, tap or click Clear to select it, scroll in the Properties window until the (Name) property is visible, and then change the name to mnu. Clear Chapter 6: Loop Structures 8

6 User Interface Design Chapter 6: Loop Structures 9

6 Event Handlers for Menu Items ►In Design view, double-tap or double-click the Exit menu item to open the code window ►Using Intelli. Sense, enter the Close procedure call to close the window and terminate the application Chapter 6: Loop Structures 10

6 Inserting Standard Items for a Menu ► Visual Basic 2012 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 Menu. Strip. NET component onto the Windows Form object. Tap or click the Action Tag on the Menu. Strip object ► Tap or click Insert Standard Items on the Menu. Strip Tasks menu ► Tap or click File on the menu bar to view the individual menu items, their associated icons, and their shortcut keys Chapter 6: Loop Structures 11

6 Inserting Standard Items for a Menu Chapter 6: Loop Structures 12

6 Input. Box Function ►The Input. Box function displays a dialog box that consists of a message asking for input, an input area, a title, an OK button, and a Cancel button ►When the user enters the text and taps or clicks the OK button, the Input. Box function returns this text as a string ►If the user taps or clicks the Cancel button, the function returns a null string ("") Chapter 6: Loop Structures 13

6 Input. Box Function Chapter 6: Loop Structures 14

6 Creating the Input. Box Object for Fitness Challenge Application Chapter 6: Loop Structures 15

6 Displaying Data Using the List. Box Object ►Drag the List. Box object from the Toolbox to where you want to place the List. Box object on the Windows Form object. When the pointer is in the correct location, release the left mouse button ►With the List. Box object selected, scroll in the Properties window to the (Name) property. Name the List. Box object lst. Weight. Loss Chapter 6: Loop Structures 16

6 Displaying Data Using the List. Box Object Chapter 6: Loop Structures 17

6 Adding List. Box Items During Design ► Assume the lst. Stores List. Box object already has been placed and named on the Windows Form object. Select the List. Box object on the Windows Form object and then tap or click the Items property in the Properties window ► Tap or click the ellipsis button in the right column of the Items property ► Tap or 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 ► Tap or click the OK button Chapter 6: Loop Structures 18

6 Adding List. Box Items During Design Chapter 6: Loop Structures 19

6 Selected. Item Property Chapter 6: Loop Structures 20

6 Accumulators, Counters, and Compound Operators ►A variable that contains an accumulated value such as the total of all the weight loss values is called an accumulator ►A variable that always is incremented by a constant value is called a counter Chapter 6: Loop Structures 21

6 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 22

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

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

6 Using Loops to Perform Repetitive Tasks ►In the Fitness Challenge application, the user enters weight loss values in an Input. Box for up to eight team members ►The repetitive process of entering eight weight loss values 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 25

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

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

6 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 28

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

6 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 • Body might not be executed ► Bottom-controlled loops test the condition at the bottom of the loop • Body executes at least once Chapter 6: Loop Structures 30

6 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 31

6 Top-Controlled Do While Loops Chapter 6: Loop Structures 32

6 Entering a Do Loop Using Intelli. Sense ►In the code window, enter the int. Score variable declaration and then press the ENTER key. Type Do While and a space to display the Intelli. Sense list. Type ints to highlight int. Score in the list ►Type < 5 and then press the ENTER key. ►Type ints to highlight the int. Score 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 33

6 Entering a Do Loop Using Intelli. Sense Chapter 6: Loop Structures 34

6 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 35

6 Bottom-Controlled Do While Loop Chapter 6: Loop Structures 36

6 Do Until Loops Chapter 6: Loop Structures 37

6 User Input Loops ►Do loops often are written to end the loop when the user enters a certain value or performs a certain action such as tapping or clicking the Cancel button in an input box Chapter 6: Loop Structures 38

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

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

6 Validating Data Chapter 6: Loop Structures 41

6 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 control variable Chapter 6: Loop Structures 42

6 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 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 43

6 Using a Data. Tip with Breakpoints ►Resolving defects 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 2012 debugger where and when to pause the execution of the application ►While in break mode, you can examine the values in all variables that are within the scope of execution through the use of Data. Tips Chapter 6: Loop Structures 44

6 Using a Data. Tip with Breakpoints ► With the application open in the code window, press and hold or right-click line 46, which contains the code where you want to set a breakpoint, and then point to Breakpoint on the shortcut menu ► Tap or click Insert Breakpoint on the submenu ► To run and test the program with the breakpoint, tap or click the Start Debugging button on the Standard toolbar ► Tap or click the Enter Weight Loss button. Type 3. 4 as the weight loss amount of the first team member ► Tap or click the OK button in the input box Chapter 6: Loop Structures 45

6 Using a Data. Tip with Breakpoints ►Point to the variable dec. Weight. Loss on line 46 ►You can view the value in any other variable within execution scope by pointing to that variable. To illustrate, point to the variable dec. Total. Weight. Loss on line 46 ►Continue the program by tapping or clicking the Continue button on the Standard toolbar. Notice that the Continue button is the same as the Start Debugging button ►Point to the dec. Total. Weight. Loss variable Chapter 6: Loop Structures 46

6 Using a Data. Tip with Breakpoints Chapter 6: Loop Structures 47

6 Using a Data. Tip with Breakpoints ►To remove a breakpoint, press and hold or rightclick the statement containing the breakpoint, and then point to Breakpoint on the shortcut menu ►Tap or click Delete Breakpoint on the Breakpoint submenu Chapter 6: Loop Structures 48

6 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 2012, you can create a deployed program by using Click. Once 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 49

6 Publishing an Application with Click. Once Deployment ► With the application open, tap or click BUILD on the menu bar ► Tap or click Publish Fitness Challenge 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 ► Tap or click the Next button. If necessary, tap or click the From a CD-ROM or DVD-ROM radio button ► Tap or click the Next button. If necessary, tap or click the “The application will not check for updates” radio button Chapter 6: Loop Structures 50

6 Publishing an Application with Click. Once Deployment ► Tap or click the Next button ► Tap or click the Finish button ► To view the finished result, minimize the Visual Studio window and then open the Search charm. Type Computer in the Search box. Double-tap or double-click the USB drive icon to view the published installation folder ► To install the application, double-tap or double-click the setup file ► After installation, the program runs. To run the installed application again, open the Search charm, type Fitness, and then click the Fitness Challenge icon Chapter 6: Loop Structures 51

6 Publishing an Application with Click. Once Deployment Chapter 6: Loop Structures 52

6 Program Design Chapter 6: Loop Structures 53

6 Program Design Chapter 6: Loop Structures 54

6 Event Planning Document Chapter 6: Loop Structures 55

6 Summary ►Add a Menu. Strip object ►Use the Input. Box function ►Display data using the List. Box object ►Understand the use of counters and accumulators ►Understand the use of compound operators Chapter 6: Loop Structures 56

6 Summary ►Repeat a process using a For…Next loop ►Repeat a process using a Do loop ►Avoid infinite loops ►Prime a loop ►Validate data Chapter 6: Loop Structures 57

6 Summary ►Create a nested loop ►Select the best type of loop ►Debug using Data. Tips at breakpoints ►Publish a finished application using Click. Once technology Chapter 6: Loop Structures 58

Microsoft Visual Basic 2012 CHAPTER SIX COMPLETE Loop Structures
- Slides: 59