Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.

Similar presentations


Presentation on theme: "Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection."— Presentation transcript:

1 Tutorial 51 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

2 Tutorial 52 Forms of the Repetition Structure For Next Do While Do Until

3 Tutorial 53 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)

4 Tutorial 54 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

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

6 Tutorial 56 Do While and Do Until Loops 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

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

8 Tutorial 58 Do While and Do Until Loops 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

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

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

11 Tutorial 511 Counters and Accumulators 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

12 Tutorial 512 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

13 Tutorial 513 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

14 Tutorial 514 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

15 Tutorial 515 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

16 Tutorial 516 Index Property vs Index Argument 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

17 Tutorial 517 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)

18 Tutorial 518 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

19 Tutorial 519 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

20 Tutorial 520 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

21 Tutorial 521 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

22 Tutorial 522 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

23 Tutorial 523 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

24 Tutorial 524 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

25 Tutorial 525 buttons Argument ConstantValueDescription vbOKOnly0Display OK button only. vbOKCancel1Display OK and Cancel buttons. vbAbortRetryIgnore2Display Abort, Retry, and Ignore buttons. vbYesNoCancel3Display Yes, No, and Cancel buttons. vbYesNo4Display Yes and No buttons. vbRetryCancel5Display Retry and Cancel buttons. vbCritical16Display Critical Message icon. vbQuestion32Display Warning Query icon. vbExclamation48Display Warning Message icon. vbInformation64Display Information Message icon. vbDefaultButton10First button is default. vbDefaultButton2256Second button is default. vbDefaultButton3512Third button is default. vbDefaultButton4768Fourth button is default. vbApplicationModal0Application modal; the user must respond to the message box before continuing work in the current application. vbSystemModal4096System modal; all applications are suspended until the user responds to the message box.

26 Tutorial 526 Return Values ConstantValueDescription vbOK1OK vbCancel2Cancel vbAbort3Abort vbRetry4Retry vbIgnore5Ignore vbYes6Yes vbNo7No

27 Tutorial 527 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

28 Tutorial 528 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

29 Tutorial 529 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

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


Download ppt "Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection."

Similar presentations


Ads by Google