1 / 85

Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control

7. Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control. Outline. 7.1 Test-Driving the Wage Calculator Application 7.2 Algorithms 7.3 Pseudocode 7.4 Control Structures 7.5 If...Then Selection Statement

Mercy
Télécharger la présentation

Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control

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. 7 • Wage Calculator Application • Introducing Algorithms,Pseudocode andProgram Control

  2. Outline • 7.1 Test-Driving the WageCalculator Application • 7.2 Algorithms • 7.3 Pseudocode • 7.4 Control Structures • 7.5If...Then Selection Statement • 7.6If...Then...Else Selection Statement and Conditional If Expressions • 7.7 Constructing the WageCalculator Application • 7.8 Assignment Operators • 7.9 Formatting Text • 7.10 Using the Debugger: The Watch Window

  3. In this tutorial you will learn: Understand basic problem-solving techniques. Understand control structures. Understand and create pseudocode. Use the If...Then and If...Then...Else selection statements to choose among alternative actions. Use the assignment operators. Use the debugger’s Watch window. Objectives

  4. Introduction • Structured programming • A technique for organizing program control that helps you develop applications that are clear and easier to debug and modify. • Applicable to most high-level languages, including Visual Basic.

  5. 7.1 Test-Driving the Wage Calculator Application Application Requirements A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number of hours they worked and their hourly wages. The application assumes a standard work week of 40 hours. The wages for 40 or fewer hours are calculated by multiplying the employee’s hourly wage by the number of hours worked. Any time worked over 40 hours in a week is considered “overtime” and earns time and a half. The total overtime earned is added to the user’s gross earnings for the regular 40 hours of work to calculate the total earnings for that week. • This application calculates earnings from hourly wage and hours worked per week.

  6. 7.1 Test-Driving the Wage CalculatorApplication (Cont.) • To organize the GUI, we vertically aligned the TextBoxes on their right sides and made the TextBoxes the same size (Fig 7.1). • We also left aligned the TextBoxes’ descriptive Labels. Figure 7.1|Wage Calculator application.

  7. GUI Design Tip • When using multiple TextBoxes vertically, align the TextBoxes on their right sides, and where possible make the TextBoxes the same size. Left align the descriptive Labels for such TextBoxes.

  8. 7.1 Test-Driving the Wage CalculatorApplication (Cont.) • The employee’s earnings are the sum of the wages for the standard 40-hour work week (40*10) and the overtime pay (5*10*1.5)(Fig 7.2). Figure 7.2| Calculating wages by clicking the CalculateButton.

  9. 7.2 Algorithms • Computing problems can be solved by executing a series of actions in a specific order. • A procedure for solving a problem, called an algorithm, deals with: • the actions to be executed and • the order in which these actions are to be executed

  10. 7.2 Algorithms (Cont.) • The following example demonstrates the importance of correctly specifying the order in which the actions are to be executed. Consider the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work: • (1) get out of bed, • (2) take off pajamas, • (3) take a shower, • (4) get dressed, • (5) eat breakfast and • (6) carpool to work. • This routine prepares the executive for a productive dayat the office.

  11. 7.2 Algorithms (Cont.) • Suppose that the same steps are performed in a slightly different order: • (1) get out of bed, • (2) take off pajamas, • (3) get dressed, • (4) take a shower, • (5) eat breakfast, • (6) carpool to work.

  12. 7.2 Algorithms (Cont.) • In this case, our junior executive shows up for work soaking wet. • Indicating the appropriate sequence in which to execute actions is equally crucial in computer programs. • Programcontrol refers to the task of orderingan application’s statements correctly.

  13. 7.3 Pseudocode • Pseudocode is an informal language that helps you formulate algo­rithms. • It is convenient and user friendly • Not an actual computer-programming language. • Pseudocode statements are not executed on computers. • Helps you think out an application before attempting to write it in a programming language.

  14. Software Design Tip • Pseudocode helps you conceptualize an application during the application-design process. Pseudocode statements can be converted to Visual Basic at a later point.

  15. 7.3 Pseudocode (Cont.) • Example of a pseudocode statement: Assign 0 to the counter • The pseudocode statement above can be converted to the following Visual Basic statement: counter = 0

  16. 7.3 Pseudocode (Cont.) • Pseudocode normally describes only executablestatements—the actions performed when the corresponding Visual Basic application is run. • An example of a programming statement that is not executed is a declaration.

  17. 7.4 Control Structures • Sequentialexecution • Normally, statements in an application are executed one after another in the order in which they are written. • transferof control • Occurs when an executed statement does not directly follow the previously executed statement in the written application. • All programs can be written in terms of only three control structures: • the sequence structure, • the selection structure and • the repetition structure.

  18. 7.4 Control Structures (Cont.) • The sequencestructure is built into Visual Basic—unless directed to act otherwise, the computer executes Visual Basic statements sequentially—that is, one after the other in the order in which they appear in the application.

  19. 7.4 Control Structures (Cont.) • The activitydiagram in Fig. 7.3 illustrates a typical sequence structure, in which two calculations are performed in order. • Figure 7.3| Sequence structure activity diagram.

  20. 7.4 Control Structures (Cont.) • Activity diagrams are part of the UnifiedModelingLanguage(UML)—an industry standard for modeling software systems. • An activity diagram models the activity (also called the workflow) of a portion of a software system.

  21. 7.4 Control Structures (Cont.) • Activity diagrams are composed of special-purpose symbols, such as • the action-statesymbol (a rectangle with its left and right sides replaced with arcs curving outward) • the diamondsymbol and the smallcircle symbol • transitionarrows, which represent the flow of the activity. • Like pseudocode, activity diagrams help you develop and represent algorithms. Activity diagrams clearly show how control structures operate.

  22. 7.4 Control Structures (Cont.) • The activity diagram in Fig. 7.3 contains two actionstates that represent actions to perform. • Each action state contains an actionexpression. • The arrows in the activity diagram are called transition arrows. • Represent transitions that indicate the order in which the actions represented by the action states occur. • The solidcircle located at the top of the activity diagram represents the activity’s initial state • The beginning of the workflow before the application performs the modeled activities.

  23. 7.4 Control Structures (Cont.) • The solid circle surrounded by a hollow circle that appears at the bottom of the activity diagram represents the finalstate • The end of the workflow after the application performs its activities. • In Fig. 7.3, the rectangles with the upper-right corners folded over are called notes in the UML • Like comments in Visual Basic applications • Explanatory remarks that describe the purpose of symbols in the diagram. • A dottedline connects each note with the element that the note describes.

  24. 7.4 Control Structures (Cont.) Selection Structures • A condition is an expression with a true or false value that is used to make a decision. • Conditions are evaluated to determine whether their value is true or false. • These values are of data type Boolean • Specified in Visual Basic code by using the keywordsTrue and False. • Refer to a condition as a Boolean expression.

  25. 7.4 Control Structures (Cont.) • Visual Basic provides three types of selectionstructures. • The If...Then selection structure performs (selects) an action (or sequence of actions) based ona condition. • If the condition evaluates to True, the actions specified by the If...Then structure are executed. • If the condition evaluates to False, the actions specified by the If...Then structure are skipped.

  26. 7.4 Control Structures (Cont.) • The If...Then...Else selection structure • …performs an action (or sequence of actions) if a condition is true • …performs a different action (or sequence of actions) if the condition is false. • The SelectCase structure performs one of many actions (or sequences of actions), depending on the value of an expression.

  27. 7.4 Control Structures (Cont.) • The If...Then structure is called a single-selectionstructure because it selects or ignores a single action (or a sequence of actions). • The If...Then...Else structure is called a double-selectionstructure because it selects between two different actions (or sequences of actions). • The SelectCase structure is called a multiple-selectionstructure because it selects among many different actions or sequences of actions.-

  28. 7.4 Control Structures (Cont.) Repetition Structures • Visual Basic provides seven types of repetitionstructures for performing a statement or groupof statements repeatedly: • While...EndWhile • DoWhile...Loop • DoUntil...Loop • Do...LoopWhile • Do...LoopUntil • For...Next • ForEach...Next

  29. 7.4 Control Structures (Cont.) Keywords • The words If, Then, Else, End, Select, Case, While, Do, Until, Loop, For, Next and Each are all Visual Basic keywords—Appendix E includes a complete list of Visual Basic keywords.

  30. 7.4 Control Structures (Cont.) Control Structure Notes • Visual Basic has 11 control structures • the sequence structure, • three types of selection structures and • seven types of repetition structures. • All Visual Basic applications are formed by combining as many of each type of control structure as is necessary. • All Visual Basic control structures aresingle-entry/single-exitcontrolstructures • Each has exactly one entry point and one exit point.

  31. 7.4 Control Structures (Cont.) • Control structures are attached to one another by connecting the exit point of one control structure to the entry point of the next. • Similar to stacking building blocks, so we call itcontrol-structurestacking. • The only other way to connect control structures is through control-structurenesting, whereby one control structure can be placed inside another.

  32. 7.5 If...Then Selection Statement • A selection statement chooses among alternative courses of action in an application. If student’s grade is greater than or equal to 60 Display “Passed” • The preceding pseudocode If statement may be written in Visual Basic as IfstudentGrade>=60ThendisplayLabel.Text="Passed"EndIf

  33. 7.5 If...Then Selection Statement (Cont.) • The condition between keywords If and Then determines whether the statement(s) within theIf...Then statement will execute. • If the condition is true, the body of theIf...Then statement executes. • If the condition is false, the body is not executed.

  34. Common Programming Error • Omitting the Then keyword in an If...Then statement is a syntax error. The IDE helps prevent this error by inserting the Then keyword after you write the condition.

  35. Good Programming Practice • Visual Basic indents the statements inside If...Then statements to improve readability.

  36. 7.5 If...Then Selection Statement (Cont.) • Conditions in If...Then statements can be formed by using the equalityoperators and relationaloperators(also called comparisonoperators), which are summarized in Fig. 7.4. Figure 7.4| Equality and relational operators.

  37. Common Programming Error • Adding spaces between the symbols in the operators <>, >= and <= (as in <>, >=, <=) is a syntax error that Visual Basic fixes automatically.

  38. Common Programming Error • Reversal of the operators <>, >= and<= (as in ><, =>, =<) is a syntax error thatVisual Basic fixes automatically.

  39. 7.5 If...Then Selection Statement (Cont.) • Figure 7.5 shows the syntax of the If...Then statement. • A statement’s syntax specifies how the statement must be formed to compile. Figure 7.5|If...Thenstatement syntax.

  40. 7.5 If...Then Selection Statement (Cont.) • Figure 7.6 illustrates the single-selection If...Then statement. • This activity diagram contains the diamond, or decisionsymbol, which indicates that a decision is to be made. • The two sets of square brackets above or next to the arrows leading from the decision symbol are called guardconditions. Figure 7.6|If...Then single-selection statement activity diagram.

  41. 7.5 If...Then Selection Statement (Cont.) • A decision symbol indicates that the workflow continues along a path determined by the guard conditions, which can be true or false. • Each transition arrow emerging from a decision symbol has a guard condition. • If a particular guard condition is true, the workflow enters the action state to which that transition arrow points. • Only one guard condition associated with a particular decision symbol can be true at once.

  42. 7.6 If...Then...Else Selection Statementand Conditional If Expressions • The If...Then selection statement performs an indicated action only when the condition evaluates to True; otherwise, the action is skipped. • The If...Then...Else selection statement allows you to specify that a different action is to be performed when the condition is true than when the condition is false. If student’s grade is greater than or equal to 60 Display “Passed” Else Display “Failed” The preceding pseudocode may be written in Visual Basic as If studentGrade >= 60Then displayLabel.Text = "Passed"Else displayLabel.Text = "Failed"End If

  43. Good Programming Practice • Indent both body statements of anIf...Then...Else statement to improve readability. [Note: The Visual Basic IDE does this automatically.]

  44. 7.6 If...Then...Else Selection Statementand Conditional If Expressions (Cont.) • A standard indentation convention should be applied consistently throughout your applications. • The body of the Else clause is indented so that it lines up with the indented body of the If clause. • The If...Then...Else selection statement follows the same general syntax as the If...Then statement (Fig. 7.7). Figure 7.7|If...Then...Else statement syntax.

  45. 7.6 If...Then...Else Selection Statementand Conditional If Expressions (Cont.) • The preceding If...Then...Else statement can also be written using a conditional If expression, as in displayLabel.Text = If(studentGrade >= 60, "Passed", "Failed") • A conditional If expression starts with the keyword If and is followed by three expressions in parentheses—a condition, the value of the conditional expression if the condition is true and the value if the condition is false.

  46. 7.6 If...Then...Else Selection Statementand Conditional If Expressions (Cont.) • Figure 7.8 illustrates the flow of control in theIf...Then...Else double-selection statement. Figure 7.8|If...Then...Else double-selection statement activity diagram.

  47. 7.6 If...Then...Else Selection Statementand Conditional If Expressions (Cont.) • NestedIf...Then...Elsestatements testfor multiple conditions by placing If...Then...Else statements inside other If...Then...Else statements. If student’s grade is greater than or equal to 90 Display “A” Else If student’s grade is greater than or equal to 80 Display “B” Else If student’s grade is greater than or equal to 70 Display “C” Else If student’s grade is greater than or equal to 60 Display “D” Else Display “F”

  48. Outline • The preceding pseudocode may be written in Visual Basic as shown in Fig. 7.9.

  49. Good Programming Practice • If there are several levels of indentation, each level should be indented farther to the right by the same amount of space.

  50. Outline • Most Visual Basic programmers prefer to use the ElseIfkeyword to write the preceding If...Then...Else statement, as shown in Fig. 7.10.

More Related