## Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and."— Presentation transcript:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and Program Control Outline 4.1 Test-Driving the Wage Calculator Application 4.2 Algorithms 4.3 Pseudocode 4.4 Control Statements 4.5 if Selection Statement 4.6 if…else Selection Statement 4.7 Constructing the Wage Calculator Application 4.8 Assignment Operators 4.9 Formatting Numbers 4.10 Using the Debugger: The Watch and Locals Windows 4.11 Wrap-Up

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Use basic problem-solving techniques. –Use control statements. –Use pseudocode as an application development tool. –Use the if and if…else selection statements to choose between alternative actions. –Use the assignment operators. –Define constants to contain values that do not change as an application executes. –Use the debugger’s Watch window to evaluate expressions. –Use the debugger’s Locals window to change variable values during program execution.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.1Test Driving the Wage Calculator Application (Cont.) Inputting floating-point values and returning a result –Enter 12.5 at the Enter hourly wage: prompt. –Enter 50.5 at the Enter hours worked this week: prompt Figure 4.4 Running the Wage Calculator application.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Algorithms –The actions to be executed –The order in which these actions are to be executed Program Control –Task of executing statements in correct order 4.2Algorithms

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Pseudo code –Informal language to develop algorithms –Resembles everyday English Not a programming language –Only describes executable statements Examples Assign 0 to the counter is equal to: counter = 0; If student’s grade is greater than or equal to 60 Display “Passed” 4.3Pseudocode

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.4Control Statements Sequence, Selection, Repetition –Sequential Execution within a Block of Code Within a block of code Instructions are executed one after another in the order in which they are written Blocks of Code have a Single Entry / Exit defined by curly brackets {} A modern program is defined by an orderly Stacking and Nesting of blocks of code. –Conditional Statements (Selection) if, if…else, switch –if : single selection statement –if…else : double selection statement –switch : multiple selection statement –Looping Statements (Repetition) while, do…while, for

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.4Control Statements (Cont.) Unified Modeling Language (UML) –Activity Diagram Models the workflow of a portion of a software system –Symbols in UML Action-state symbols –Rectangles with left and right sides curved out »Represent an action about to occur Diamonds –Represent a decision to be made Small circles –Represent initial and final state of workflow Transition arrows

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.5 Sequence statement activity diagram. UML symbols –Action state symbols, diamonds, small circles, transition arrows UML notes –Similar to comments 4.4Control Statements (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.5 if Selection Statement if statement performs action based on condition –Conditions are boolean expressions (true / false value) Statement executes when boolean expression is true Conditional expression is inside parenthesis. Conditions are formed with equality operators and relational operators Example pseudocode If student’s grade is greater than or equal to 60 Display “Passed” Corresponding C++ code

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Relational operators –Greater than ( >= ), greater ( > ), less than ( <= ), less ( < ) Equality operators –Equal to ( == ), not equal to ( != ) Common Errors –It is a syntax error to add spaces in between the symbols ==, !=, >=, = etc.) –Reversing the symbols in the !=, >= and and =< ) –Using the assignment operator ( = ) when the equality operator ( == ) is intended. Microsoft Visual C++ Studio 2010 does not warn you about this error! 4.5 if Selection Statement (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.7 if single-selection statement UML activity diagram. 4.5 if Selection Statement (Cont.) UML diamond symbol –Decision symbol Action / decision model of programming

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.6 if…else Selection Statement if…else statement –Performs if action if condition statement is true –Performs separate else action if condition statement is false Nested if…else statements –Can check for multiple conditions

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.8 if…else double-selection statement UML activity diagram. 4.6 if…else Selection Statement (Cont.) if ( grade >= 60 ) { cout << "Passed"; } else { cout << "Failed"; }

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.7Constructing the Wage Calculator Application (Cont.) Figure 4.13 main function (containing only a return statement). Initial main function

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.14 Assigning user input to variables. 4.7Constructing the Wage Calculator Application (Cont.) Define variables to store user input Prompt user for hourly rage and input result into the hourlyWage variable Prompt user for hours worked and input result into the hoursWorked variable

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Constants –Capitalize names to make them stand out –Variable that cannot be changed after definition –Defined by preceding data type with const keyword Figure 4.15 Creating a constant. 4.7Constructing the Wage Calculator Application (Cont.) Constant definition

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Primitive type double –Used to represent floating-point values Figure 4.16 Defining a variable of the double type. Define double variable wages 4.7Constructing the Wage Calculator Application (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.17 if…else statement that calculates gross wages. 4.7Constructing the Wage Calculator Application (Cont.) if…else statement to calculate wages Determine whether the hours worked were less than the hour limit

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.7Constructing the Wage Calculator Application (Cont.) Figure 4.18 Displaying gross wages. Displaying output

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.19 Updated application displaying unformatted number. Displaying unformatted number 4.7Constructing the Wage Calculator Application (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.8Assignment Operators (Cont.) Addition assignment operator ( += ) makes it unnecessary to include the wages variable in both the left and right operands Figure 4.21 Using the addition assignment operator in a calculation. Addition assignment operator

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers Stream manipulators –fixed Indicates floating-point values should be displayed in fixed-point notation –setprecision Indicates the number of digits displayed to the right of the decimal point Parameterized stream manipulators –Require header file –setprecision is a parameterized stream manipulator –Default precision 6 digits to the right of the decimal point Nonparameterized stream manipulators –fixed Does not require

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers (Cont.) Using the setprecision manipulator without including the header file is a syntax error Figure 4.22 Including the header file. The header file declares the setprecision stream manipulator

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers (Cont.) Figure 4.23 Using the fixed and setprecision stream manipulators to display the gross wages with two digits of precision to the right of the decimal point. The fixed and setprecision stream manipulators cause cout to display numeric values accurate to two decimal places This iostream manipulator statement is typically placed before the cout statement(s) for which it is intended.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.9Formatting Numbers (Cont.) Figure 4.24 Completed application displaying formatted wages. Displaying formatted number

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (2 of 3) The const keyword specifies that HOUR_LIMIT is a constant Variable to store gross wages Begin if…else statement

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (3 of 3) Begin else part of if…else statement. else part executes when condition in line 33 evaluates to false Assign left operand the value of adding left and right operands End else part of if…else Format result as a dollar amount

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows Figure 4.26 Setting breakpoints at lines 23 and 36. Breakpoint at line 23 Margin indicator bar Breakpoint at line 36

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.27 Entering hourly wage before breakpoint is reached. 4.10Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.28 Program execution suspended when debugger reaches the breakpoint at line 23. 4.10Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) The Locals window allows new values to be assigned to variables Uninitialized variables are assigned random values Figure 4.29 Examining variable hourlyWage. Value of the hourlyWage variable displayed

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.30 Examining the values of expressions. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) Evaluating an arithmetic expression Evaluating a bool expression

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) Variables modified since the last breakpoint are displayed in red Figure 4.31 Displaying the value of the variables in the Wage Calculator application. Values of the hoursWorked and HOUR_LIMIT variables displayed in red

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Modifying values inside the Locals window Figure 4.32 Modifying values. Value modified in the debugger 4.10Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 4.33 Setting a breakpoint at line 54 prevents the application from exiting immediately after displaying its result. Breakpoint at line 54 4.10Using the Debugger: The Watch and Locals Windows (Cont.)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4.10Using the Debugger: The Watch and Locals Windows (Cont.) Output reflects the modified values from the Locals window Figure 4.34 Output displayed after modifying the hoursWorked variable.