© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application 7.2 Algorithms 7.3 Pseudocode 7.4 Control Structures 7.5 if Selection Statement 7.6 if…else Selection Statement 7.7 Constructing the Wage Calculator Application 7.8 Assignment Operators 7.9 Formatting Text 7.10 Using the Debugger: The Watch Window 7.11 Wrap-Up Tutorial 7 – Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Understand basic problem-solving techniques. –Understand control structures. –Understand and create pseudocode. –Use the if and if…else selection statements to choose among alternative actions. –Use the assignment operators. –Use the debugger’s Watch window.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Wage Calculator Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Wage Calculator Application Load the Wage Calculator application –Debug > Start –Notice the alignment of the TextBox es Figure 7.1 Wage Calculator application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Wage Calculator Application Initializing TextBox fields –Set Hourly wage: TextBox to 10 –Set Weekly hours: TextBox to 45 –Click Calculate Button Figure 7.2 Calculating wages by clicking the Calculate Button.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Algorithms An algorithm –Actions –Order in which the actions occur Program control
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Pseudocode Pseudocode –Informal language Everyday English Describes only executable statements Not a programming language –Helps plan algorithms and programs
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Control Structures Three control structures –Sequence structure –Selection structure –Repetition structure Activity diagram –Models the workflow
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Control Structures Figure 7.3 Sequence structure activity diagram.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if Selection Statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if Selection Statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if Selection Statement Figure 7.6 if single-selection statement activity diagram.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if…else Selection Statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if…else Selection Statement Figure 7.8 if…else double-selection statement activity diagram.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if…else Selection Statement Pseudocode for an application that displays a student’s grades. 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”
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if…else Selection Statement C# code converted from pseudocode if ( intStudentGrade >= 90 ) { lblDisplay.Text = "A"; } else { if ( intStudentGrade >= 80 ) { lblDisplay.Text = "B"; } else { if ( intStudentGrade >= 70 ) { lblDisplay.Text = "C"; } else { if ( intStudentGrade >= 60 ) { lblDisplay.Text = "D"; } else { lblDisplay.Text = "F"; } } } }
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved if…else Selection Statement if…else statement using the else if structure. if ( intStudentGrade >= 90 ) { lblDisplay.Text = "A"; } else if ( intStudentGrade >= 80 ) { lblDisplay.Text = "B"; } else if ( intStudentGrade >= 70 ) { lblDisplay.Text = "C"; } else if ( intStudentGrade >= 60 ) { lblDisplay.Text = "D"; } else { lblDisplay.Text = "F"; }
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Generating the btnCalculate_Click event handler –Button calculates gross earnings
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.10 Calculate Button event handler. Generated event handler
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Declaring variables –Primitive data types double decimal
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.11 Declaring variables of type double and d ecimal. Variable declarations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Declaring a constant variable –Keyword c onst –Value set at declaration and cannot be changed thereafter
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.12 Creating a constant. Constant declaration
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Assigning data to variables –Double.Parse method –Decimal.Parse method
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.13 Assigning data to variables. Variable assignment
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Determining wages –Use if…else statement Regular wage Time and half for overtime
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.14 i f…else statement to determine wages. Added if…else statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Displaying output –Using Label ’s Text property
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.15 Assigning the result to lblEarningsResult. Displaying output
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Wage Calculator Application Figure 7.16 Running the application before formatting the output. Displaying output
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Assignment Operators
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Assignment Operators Using the assignment operator –Makes statement shorter
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Assignment Operators Figure 7.18 Using the addition assignment operator in a calculation. Assignment operator shortens statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Formatting Text Formatting output –Use Format specifier –Use C for currency
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Formatting Text Figure 7.19 Using the Format method to display the result as currency. Formatting output as currency
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Formatting Text
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 WageCalculator.cs (1 of 5)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 WageCalculator.cs (2 of 5)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 WageCalculator.cs (3 of 5)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 WageCalculator.cs (4 of 5) Condition after keyword if Keyword const specifies constant else body executes when condition evaluates to false
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 WageCalculator.cs (5 of 5) Format result as currency Assignment operator assigns left operand result of adding left and right operands
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Using the debugger –Set breakpoints
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Figure 7.22 Breakpoints added to Wage Calculator application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Suspending program execution –Run the application –IDE switch to break mode –Active window changes Figure 7.23 Suspending program execution.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Examine data –Enabling the Watch window The Name column The Value column The Type column Figure 7.24 Watch window. Variable typesVariable values Variable names
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Examining different expressions –Evaluating arithmetic expressions –bool expressions –Invalid expressions Figure 7.25 Examining expressions. Invalid expression bool expressionComplex expression
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Deleting a watch Figure 7.26 Deleting a watch. Delete Watch option
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Changes to the Watch window –Modified values displayed in red Figure 7.27 Modified values in Watch window. Modified value appears in red
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Changing a variable’s value –Test various values to confirm behavior of the application Figure 7.28 Modifying values in a Watch window. Value modified directly
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Noting the changes to the variable’s values –Allow the debugging process to continue Figure 7.29 Dialog to continue running application after changing values in the Watch window.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using the Debugger: The Watch Window Notice the result from the debugging process Figure 7.32 Output displayed after the debugging process. Earnings result based on altered input