SlideShare ist ein Scribd-Unternehmen logo
1 von 59
CHAPTER SIX Loop Structures
Chapter 6: Loop Structures 2 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 3 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 4 Objectives 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 5 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 6 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
Chapter 6: Loop Structures 7 MenuStrip Object With a Windows Form object open in the Visual Studio window, scroll in the Toolbox until the Menus & Toolbars category is visible. If the category is not open, click the expand icon (right-pointing triangle) next to the Menus & Toolbars category name. Drag the MenuStrip .NET component from the Menus & Toolbars category in the Toolbox to the Windows Form object Release the mouse button With the MenuStrip object selected, scroll in the Properties window until the (Name) property is visible. Change the MenuStrip object name to mnuHighwayRadarCheckpoint
Chapter 6: Loop Structures 8 MenuStrip Object 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 mnuFile 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
MenuStrip Object Chapter 6: Loop Structures 9
Chapter 6: Loop Structures 10 Event Handlers for Menu Items In Design view, double-click the Exit menu item to open the code editing window Using IntelliSense, enter the Close procedure call to close the window and terminate the application
Chapter 6: Loop Structures 11 Standard Items for a Menu Visual Basic 2010 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
Standard Items for a Menu Chapter 6: Loop Structures 12
Chapter 6: Loop Structures 13 InputBox Function The InputBox 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 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 ("")
Chapter 6: Loop Structures 14 InputBox Object Default Value The InputBox object can be assigned a default value
Chapter 6: Loop Structures 15 InputBox Object for Highway Radar Checkpoint Application
Chapter 6: Loop Structures 16 Displaying Data Using the 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
Displaying Data Using the ListBox Object Chapter 6: Loop Structures 17
Chapter 6: Loop Structures 18 Adding Items During Design 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
Adding Items During Design Chapter 6: Loop Structures 19
SelectedItem Property Chapter 6: Loop Structures 20
Chapter 6: Loop Structures 21 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 How many vehicle speeds the user has entered
Chapter 6: Loop Structures 22 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
Accumulators, Counters, and Compound Operators Chapter 6: Loop Structures 23
Accumulators, Counters, and Compound Operators Chapter 6: Loop Structures 24
Chapter 6: Loop Structures 25 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 26 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
Repeating a Process Using the For…Next Loop Chapter 6: Loop Structures 27
Chapter 6: Loop Structures 28 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 decumals, or include variables and mathematical expressions
Chapter 6: Loop Structures 29 Entering the For…Next Loop Code
Chapter 6: Loop Structures 30 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, so the body of a bottom-controlled loop is executed at least once Body executes at least once
Chapter 6: Loop Structures 31 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
Top-Controlled Do While Loops Chapter 6: Loop Structures 32
Chapter 6: Loop Structures 33 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 an IntelliSense list is displayed. Type ints to highlight intScore in the list Type < 5 and then press the ENTER key. 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
Entering a Do Loop Using IntelliSense Chapter 6: Loop Structures 34
Chapter 6: Loop Structures 35 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
Bottom-Controlled Do While Loop Chapter 6: Loop Structures 36
Do Until Loops Chapter 6: Loop Structures 37
Chapter 6: Loop Structures 38 User Input Loops Do loops often are written to end the loop when a certain value is entered by the user, or the user performs a certain action such as clicking the Cancel button in an input box
Chapter 6: Loop Structures 39 Avoiding Infinite Loops An infinite loop is a loop that never ends
Chapter 6: Loop Structures 40 Priming the Loop Starting a loop with a preset value in the variable(s) tested in the condition is called priming the loop
Validating Data Chapter 6: Loop Structures 41
Chapter 6: Loop Structures 42 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 43 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 44 Using a DataTip 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 2010 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 DataTips
Chapter 6: Loop Structures 45 Using a DataTip with Breakpoints With the program open in the code editing window, right-click line 47, 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 46 Using a DataTip with Breakpoints Point to the variable decVehicleSpeed on line 47 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 47 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
Using a DataTip with Breakpoints Chapter 6: Loop Structures 47
Chapter 6: Loop Structures 48 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 49 Publishing an Application with ClickOnce 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 2010, 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 50 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 publishto 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 DVD-ROM radio button Click the Next button. If necessary, click the The application will not check for updates radio button
Chapter 6: Loop Structures 51 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 Computer on the Windows 7 Start menu. 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, click Radar on the All Programs menu, and then click Radar on the Radar submenu
Publishing an Application with ClickOnce Deployment Chapter 6: Loop Structures 52
Program Design Chapter 6: Loop Structures 53
Program Design Chapter 6: Loop Structures 54
Event Planning Document Chapter 6: Loop Structures 55
Chapter 6: Loop Structures 56 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 57 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 58 Summary Create a nested loop Select the best type of loop Debug using DataTips at breakpoints Publish a finished application using ClickOnce technology
CHAPTER SIX COMPLETE Loop Structures

Weitere ähnliche Inhalte

Was ist angesagt?

Cookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesCookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesEmiel Paasschens
 
Open Office Calc : Lesson 05
Open Office Calc : Lesson 05Open Office Calc : Lesson 05
Open Office Calc : Lesson 05thinkict
 
Open Office Calc : Lesson 07
Open Office Calc : Lesson 07Open Office Calc : Lesson 07
Open Office Calc : Lesson 07thinkict
 
Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007
Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007
Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007ITVAMP, LLC
 
Spreadsheets Introduction using RM Number Magic
Spreadsheets Introduction using RM Number MagicSpreadsheets Introduction using RM Number Magic
Spreadsheets Introduction using RM Number MagicMalcolm Wilson
 
Open Office Calc : Level 1
Open Office Calc : Level 1Open Office Calc : Level 1
Open Office Calc : Level 1thinkict
 
Xamarin.Forms Hands On Lab (Begineer)
Xamarin.Forms Hands On Lab (Begineer)Xamarin.Forms Hands On Lab (Begineer)
Xamarin.Forms Hands On Lab (Begineer)Cheah Eng Soon
 
Creating a Schematic Design in OrCAD Capture CIS
Creating a Schematic Design in OrCAD Capture CISCreating a Schematic Design in OrCAD Capture CIS
Creating a Schematic Design in OrCAD Capture CISMd.Maruf Ahamed
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesSami Mut
 
Domains in IBM Maximo Asset Management
Domains in IBM Maximo Asset ManagementDomains in IBM Maximo Asset Management
Domains in IBM Maximo Asset ManagementRobert Zientara
 

Was ist angesagt? (19)

Cookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesCookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business Rules
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
Open Office Calc : Lesson 05
Open Office Calc : Lesson 05Open Office Calc : Lesson 05
Open Office Calc : Lesson 05
 
4.C#
4.C#4.C#
4.C#
 
Open Office Calc : Lesson 07
Open Office Calc : Lesson 07Open Office Calc : Lesson 07
Open Office Calc : Lesson 07
 
SPF WinForm Programs
SPF WinForm ProgramsSPF WinForm Programs
SPF WinForm Programs
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007
Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007
Microsoft Project 2003 Configuration Training Level 2 Itvamp 2007
 
Spreadsheets Introduction using RM Number Magic
Spreadsheets Introduction using RM Number MagicSpreadsheets Introduction using RM Number Magic
Spreadsheets Introduction using RM Number Magic
 
Open Office Calc : Level 1
Open Office Calc : Level 1Open Office Calc : Level 1
Open Office Calc : Level 1
 
Xamarin.Forms Hands On Lab (Begineer)
Xamarin.Forms Hands On Lab (Begineer)Xamarin.Forms Hands On Lab (Begineer)
Xamarin.Forms Hands On Lab (Begineer)
 
Creating a Schematic Design in OrCAD Capture CIS
Creating a Schematic Design in OrCAD Capture CISCreating a Schematic Design in OrCAD Capture CIS
Creating a Schematic Design in OrCAD Capture CIS
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slides
 
06 procedures
06 procedures06 procedures
06 procedures
 
Visual basic
Visual basicVisual basic
Visual basic
 
Vb%20 tutorial
Vb%20 tutorialVb%20 tutorial
Vb%20 tutorial
 
Domains in IBM Maximo Asset Management
Domains in IBM Maximo Asset ManagementDomains in IBM Maximo Asset Management
Domains in IBM Maximo Asset Management
 
Tugas testing
Tugas testingTugas testing
Tugas testing
 
Ms Access
Ms AccessMs Access
Ms Access
 

Ähnlich wie Chapter 06

Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6cmontanez
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5Akhil Mittal
 
Qtp interview questions3
Qtp interview questions3Qtp interview questions3
Qtp interview questions3Ramu Palanki
 
Qtp interview questions3
Qtp interview questions3Qtp interview questions3
Qtp interview questions3Ramu Palanki
 
Test Case Creation in Katalon Studio
Test Case Creation in Katalon StudioTest Case Creation in Katalon Studio
Test Case Creation in Katalon StudioRapidValue
 
qtp 9.2 features
qtp 9.2 featuresqtp 9.2 features
qtp 9.2 featureskrishna3032
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorialsasidhar
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769subhasis100
 
Qtp 92 Tutorial Anil
Qtp 92 Tutorial AnilQtp 92 Tutorial Anil
Qtp 92 Tutorial Anilguest3373d3
 
Qtp 9.2 Tutorial
Qtp 9.2 TutorialQtp 9.2 Tutorial
Qtp 9.2 Tutorialguest37ae7f
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769subhasis100
 
Dynamic Function Call in PI Sheet (XStep)
Dynamic Function Call in PI Sheet (XStep)Dynamic Function Call in PI Sheet (XStep)
Dynamic Function Call in PI Sheet (XStep)Ankit Sharma
 
Interview questions in qtp
Interview questions in qtpInterview questions in qtp
Interview questions in qtpRamu Palanki
 
Automation anywhere Training Materials
Automation anywhere Training MaterialsAutomation anywhere Training Materials
Automation anywhere Training MaterialsShekar S
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3Akhil Mittal
 
GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4zenonas
 
003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdf003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdfKhushal Chate
 

Ähnlich wie Chapter 06 (20)

Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5
 
Qtp interview questions3
Qtp interview questions3Qtp interview questions3
Qtp interview questions3
 
Qtp interview questions3
Qtp interview questions3Qtp interview questions3
Qtp interview questions3
 
Test Case Creation in Katalon Studio
Test Case Creation in Katalon StudioTest Case Creation in Katalon Studio
Test Case Creation in Katalon Studio
 
qtp 9.2 features
qtp 9.2 featuresqtp 9.2 features
qtp 9.2 features
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorial
 
Ppt Qtp
Ppt QtpPpt Qtp
Ppt Qtp
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769
 
Qtp 92 Tutorial Anil
Qtp 92 Tutorial AnilQtp 92 Tutorial Anil
Qtp 92 Tutorial Anil
 
Qtp 9.2 Tutorial
Qtp 9.2 TutorialQtp 9.2 Tutorial
Qtp 9.2 Tutorial
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769
 
Dynamic Function Call in PI Sheet (XStep)
Dynamic Function Call in PI Sheet (XStep)Dynamic Function Call in PI Sheet (XStep)
Dynamic Function Call in PI Sheet (XStep)
 
Interview questions in qtp
Interview questions in qtpInterview questions in qtp
Interview questions in qtp
 
Chapter 07
Chapter 07Chapter 07
Chapter 07
 
Automation anywhere Training Materials
Automation anywhere Training MaterialsAutomation anywhere Training Materials
Automation anywhere Training Materials
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3
 
GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4
 
003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdf003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdf
 

Chapter 06

  • 1. CHAPTER SIX Loop Structures
  • 2. Chapter 6: Loop Structures 2 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
  • 3. Chapter 6: Loop Structures 3 Objectives Repeat a process using a For…Next loop Repeat a process using a Do loop Avoid infinite loops Prime a loop Validate data
  • 4. Chapter 6: Loop Structures 4 Objectives Create a nested loop Select the best type of loop Debug using DataTips at breakpoints Publish a finished application using ClickOnce technology
  • 5. Chapter 6: Loop Structures 5 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
  • 6. Chapter 6: Loop Structures 6 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
  • 7. Chapter 6: Loop Structures 7 MenuStrip Object With a Windows Form object open in the Visual Studio window, scroll in the Toolbox until the Menus & Toolbars category is visible. If the category is not open, click the expand icon (right-pointing triangle) next to the Menus & Toolbars category name. Drag the MenuStrip .NET component from the Menus & Toolbars category in the Toolbox to the Windows Form object Release the mouse button With the MenuStrip object selected, scroll in the Properties window until the (Name) property is visible. Change the MenuStrip object name to mnuHighwayRadarCheckpoint
  • 8. Chapter 6: Loop Structures 8 MenuStrip Object 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 mnuFile 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
  • 9. MenuStrip Object Chapter 6: Loop Structures 9
  • 10. Chapter 6: Loop Structures 10 Event Handlers for Menu Items In Design view, double-click the Exit menu item to open the code editing window Using IntelliSense, enter the Close procedure call to close the window and terminate the application
  • 11. Chapter 6: Loop Structures 11 Standard Items for a Menu Visual Basic 2010 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
  • 12. Standard Items for a Menu Chapter 6: Loop Structures 12
  • 13. Chapter 6: Loop Structures 13 InputBox Function The InputBox 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 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 ("")
  • 14. Chapter 6: Loop Structures 14 InputBox Object Default Value The InputBox object can be assigned a default value
  • 15. Chapter 6: Loop Structures 15 InputBox Object for Highway Radar Checkpoint Application
  • 16. Chapter 6: Loop Structures 16 Displaying Data Using the 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
  • 17. Displaying Data Using the ListBox Object Chapter 6: Loop Structures 17
  • 18. Chapter 6: Loop Structures 18 Adding Items During Design 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
  • 19. Adding Items During Design Chapter 6: Loop Structures 19
  • 20. SelectedItem Property Chapter 6: Loop Structures 20
  • 21. Chapter 6: Loop Structures 21 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 How many vehicle speeds the user has entered
  • 22. Chapter 6: Loop Structures 22 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
  • 23. Accumulators, Counters, and Compound Operators Chapter 6: Loop Structures 23
  • 24. Accumulators, Counters, and Compound Operators Chapter 6: Loop Structures 24
  • 25. Chapter 6: Loop Structures 25 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
  • 26. Chapter 6: Loop Structures 26 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
  • 27. Repeating a Process Using the For…Next Loop Chapter 6: Loop Structures 27
  • 28. Chapter 6: Loop Structures 28 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 decumals, or include variables and mathematical expressions
  • 29. Chapter 6: Loop Structures 29 Entering the For…Next Loop Code
  • 30. Chapter 6: Loop Structures 30 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, so the body of a bottom-controlled loop is executed at least once Body executes at least once
  • 31. Chapter 6: Loop Structures 31 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
  • 32. Top-Controlled Do While Loops Chapter 6: Loop Structures 32
  • 33. Chapter 6: Loop Structures 33 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 an IntelliSense list is displayed. Type ints to highlight intScore in the list Type < 5 and then press the ENTER key. 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
  • 34. Entering a Do Loop Using IntelliSense Chapter 6: Loop Structures 34
  • 35. Chapter 6: Loop Structures 35 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
  • 36. Bottom-Controlled Do While Loop Chapter 6: Loop Structures 36
  • 37. Do Until Loops Chapter 6: Loop Structures 37
  • 38. Chapter 6: Loop Structures 38 User Input Loops Do loops often are written to end the loop when a certain value is entered by the user, or the user performs a certain action such as clicking the Cancel button in an input box
  • 39. Chapter 6: Loop Structures 39 Avoiding Infinite Loops An infinite loop is a loop that never ends
  • 40. Chapter 6: Loop Structures 40 Priming the Loop Starting a loop with a preset value in the variable(s) tested in the condition is called priming the loop
  • 41. Validating Data Chapter 6: Loop Structures 41
  • 42. Chapter 6: Loop Structures 42 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
  • 43. Chapter 6: Loop Structures 43 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
  • 44. Chapter 6: Loop Structures 44 Using a DataTip 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 2010 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 DataTips
  • 45. Chapter 6: Loop Structures 45 Using a DataTip with Breakpoints With the program open in the code editing window, right-click line 47, 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
  • 46. Chapter 6: Loop Structures 46 Using a DataTip with Breakpoints Point to the variable decVehicleSpeed on line 47 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 47 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
  • 47. Using a DataTip with Breakpoints Chapter 6: Loop Structures 47
  • 48. Chapter 6: Loop Structures 48 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
  • 49. Chapter 6: Loop Structures 49 Publishing an Application with ClickOnce 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 2010, 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
  • 50. Chapter 6: Loop Structures 50 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 publishto 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 DVD-ROM radio button Click the Next button. If necessary, click the The application will not check for updates radio button
  • 51. Chapter 6: Loop Structures 51 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 Computer on the Windows 7 Start menu. 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, click Radar on the All Programs menu, and then click Radar on the Radar submenu
  • 52. Publishing an Application with ClickOnce Deployment Chapter 6: Loop Structures 52
  • 53. Program Design Chapter 6: Loop Structures 53
  • 54. Program Design Chapter 6: Loop Structures 54
  • 55. Event Planning Document Chapter 6: Loop Structures 55
  • 56. Chapter 6: Loop Structures 56 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
  • 57. Chapter 6: Loop Structures 57 Summary Repeat a process using a For…Next loop Repeat a process using a Do loop Avoid infinite loops Prime a loop Validate data
  • 58. Chapter 6: Loop Structures 58 Summary Create a nested loop Select the best type of loop Debug using DataTips at breakpoints Publish a finished application using ClickOnce technology
  • 59. CHAPTER SIX COMPLETE Loop Structures