1 / 30

Programming Structures

Programming Structures. Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection - allows the program to make a decision/comparison and then select the appropriate action to take based on that decision/comparison

etoile
Download Presentation

Programming Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Structures • Sequence - program instructions are processed, one after another, in the order in which they appear in the program • Selection - allows the program to make a decision/comparison and then select the appropriate action to take based on that decision/comparison • Repetition - (looping or iteration) tells the computer to repeat one or more instructions either a specified number of times or until some condition is met Tutorial 5

  2. Forms of the Repetition Structure • For Next • Do While • Do Until Tutorial 5

  3. For Next Loop • Tells the computer to repeat one or more statements a specified number of times. Called a pretest loop because the loop is evaluated before the instructions are processed • Syntax: For counter = startvalue To endvalue [Step stepvalue] [instructions you want repeated] Next counter • counter is the name of a numeric variable and it keeps track of how many times the loop instructions are repeated • startvalue, endvalue, and stepvalue must be numeric and they can be either positive or negative, integer or non-integer (default stepvalue is 1) Tutorial 5

  4. For Next Loop Tasks • The loop initializes the counter to the startvalue (done only once, at the beginning of the loop) • If the stepvalue is positive (negative), the loop checks if the value in counter is greater than (less than) the endvalue. If it is, the loop stops; otherwise the instructions within the loop are processed and the next task is performed • The loop adds the stepvalue to the counter. It then repeats the prior two steps Tutorial 5

  5. Hexagon Repeat for intCount = 1 to 3 by 1 Print intCount Next intCount Flowchart and Pseudocode intCount > 3 1 1 Print intCount Tutorial 5

  6. Do While loop repeats a block of instructions while a condition is true You also can use a Do While loop to repeat a block of instructions a specified number of times Do Until loop repeats a block of instructions until a condition becomes true You also can use a Do Until loop to repeat a block of instructions a specified number of times Do While and Do Until Loops Tutorial 5

  7. Do While condition [loop instructions] Loop Do [loop instructions] Loop Until condition Syntax of the Do While and Do Until Loops • condition must evaluate to true or false • condition can contain variables, constants, properties, functions, mathematical operators, relational operators, and logical operators Tutorial 5

  8. In the Do While loop, the instructions are processed only when the condition evaluates to true; the loop stops when the condition evaluates to false Called a pretest loop In the Do Until loop, the instructions are processed only when the condition evaluates to false; the loop stops when the condition evaluates to true Called a posttest loop Do While and Do Until Loops Tutorial 5

  9. intCount = 1 Repeat while intCount <= 3 Display intCount Add 1 to intCount End Repeat while intCount <= 3 Do While Loop intCount = 1 intCount <= 3 F T Display intCount intCount = intCount + 1 Tutorial 5

  10. intCount = 1 Repeat Display intCount Add 1 to intCount End Repeat until intCount > 3 Do Until Loop intCount = 1 Display intCount intCount = intCount + 1 intCount > 3 T F Tutorial 5

  11. Used within a repetition structure to calculate subtotals, totals, and averages Initialized (usually to 0 or 1) outside the loop and updated within the loop A counter is a numeric variable used for counting something and is typically updated by 1 An accumulator is a numeric variable used for accumulating (adding together) and is updated by an amount that varies Counters and Accumulators Tutorial 5

  12. Calculating an Average • Average = accumulator / counter • Be sure to verify that counter is greater than 0 before calculating the average • Use the If…Then…Else selection structure Tutorial 5

  13. Control Arrays • Group of controls of the same type that have the same name and share the same set of event procedures • Each control in the array is identified by the array’s name and a unique index • The first control in the array has an index of 0 Tutorial 5

  14. Creating a Control Array • To make existing controls into an array, simply assign the same name to each control. When you are asked if you want to create a control array, click the Yes button • If the controls are not already on the form, place the first control on the form and set its name and other properties • Copy the control to the clipboard, then paste the appropriate number of controls on the form • When you are asked if you want to create a control array, click the Yes button Tutorial 5

  15. Control Array Code Window • Index As Integer appears in the Code windows for a control array • All controls in the array share the same set of Code windows • Index contains an integer that represents the value stored in the Index property of the control receiving the event Tutorial 5

  16. The Index property stores a number that identifies each control in the array Each control in the array has a value in its Index property The Index property is like an address The Index argument found in the Code window is a local variable created by Visual Basic The Index argument contains the address of the control receiving the event Index Property vs Index Argument Tutorial 5

  17. Parallel Arrays • Arrays whose contents are related by their position in the arrays--in other words, by their index chkDone(0) txtScore(0) chkDone(1) txtScore(1) Tutorial 5

  18. Enabled Property • An object’s Enabled property determines whether the object can respond to user-generated events, such as clicking or entering information • Can be set to True (default) or False • It is customary in Windows applications to disable objects that don’t apply to the current state of the application Tutorial 5

  19. GotFocus Event • Occurs when a control receives the focus • It is customary in Windows applications to highlight a text box’s existing text when the text box receives the focus. You do so by setting the text box’s SelStart and SelLength properties Tutorial 5

  20. Change Event • Occurs when the contents of an object are changed • You can use this event to clear the results of calculations from label controls when the contents of a text box has changed Tutorial 5

  21. Alignment Property • You can use the Alignment property to align the contents of a label control • You also can use the Alignment property to align the contents of a text box, but you must be sure to set the text box’s MultiLine property to True and also code its KeyPress event to prevent the text box from recognizing the Enter key Tutorial 5

  22. MsgBox Function • Displays one of Visual Basic’s predefined dialog boxes, which contains a message, one or more command buttons, and an icon • After displaying the dialog box, the MsgBox function waits for the user to choose a button, then returns a value that indicates which button the user selected Tutorial 5

  23. MsgBox Function • Syntax: MsgBox(prompt[, buttons][, title][,helpfile, context]) • prompt is the message you want displayed • buttons is a numeric expression that specifies the number and type of buttons, the icon, the default button, and the modality • title is a string expression displayed in the title bar Tutorial 5

  24. Modality • Application modal • Default modality; user must respond to the dialog box’s message before he or she can continue working in the current application; the user still can access other applications • System modal • All applications are suspended until the user responds to the dialog box’s message Tutorial 5

  25. buttons Argument ConstantValueDescription vbOKOnly 0 Display OK button only. vbOKCancel 1 Display OK and Cancel buttons. vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons. vbYesNoCancel 3 Display Yes, No, and Cancel buttons. vbYesNo 4 Display Yes and No buttons. vbRetryCancel 5 Display Retry and Cancel buttons. vbCritical 16 Display Critical Message icon. vbQuestion 32 Display Warning Query icon. vbExclamation 48 Display Warning Message icon. vbInformation 64 Display Information Message icon. vbDefaultButton1 0 First button is default. vbDefaultButton2 256 Second button is default. vbDefaultButton3 512 Third button is default. vbDefaultButton4 768 Fourth button is default. vbApplicationModal 0 Application modal; the user must respond to the message box before continuing work in the current application. vbSystemModal 4096 System modal; all applications are suspended until the user responds to the message box. Tutorial 5

  26. Return Values ConstantValueDescription vbOK 1 OK vbCancel 2 Cancel vbAbort 3 Abort vbRetry 4 Retry vbIgnore 5 Ignore vbYes 6 Yes vbNo 7 No Tutorial 5

  27. Unload Event • Occurs when a form is about to be removed from the screen and memory • Triggered when you use the Unload statement in code or when you click the Close button to close the form • Use the Unload event to verify that the user wants to exit an application and also for code that saves and closes files Tutorial 5

  28. Unload Statement vs Unload Event • The Unload statement tells Visual Basic to unload the form • The Unload event is the procedure that occurs before the form is unloaded Tutorial 5

  29. Unload Event Code Window • The Cancel argument is a local variable created by Visual Basic and it contains the value 0 • To prevent the form from being unloaded, set Cancel to a value other than 0 Tutorial 5

  30. Debugging Technique • You can use the KeyPress event to control what the user can enter into a text box Tutorial 5

More Related