Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2007 Pearson Education, Inc. All rights reserved. 1 6 6 Control Statements: Part2.

Similar presentations


Presentation on theme: " 2007 Pearson Education, Inc. All rights reserved. 1 6 6 Control Statements: Part2."— Presentation transcript:

1  2007 Pearson Education, Inc. All rights reserved. 1 6 6 Control Statements: Part2

2  2007 Pearson Education, Inc. All rights reserved. 2 Who can control his fate? — William Shakespeare, Othello The used key is always bright. — Benjamin Franklin Not everything that can be counted counts, and not every thing that counts can be counted. — Albert Einstein Every advantage in the past is judged in the light of the final issue. — Demosthenes

3  2007 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  The essentials of counter-controlled repetition.  To use the For...Next, Do...Loop While and  Do...Loop Until repetition statements to execute statements in a program repeatedly.  To perform multiple selection using the Select...Case selection statement.  To use the Exit statement to break out of a repetition statement.  To use the Continue statement to break out of the current iteration of a repetition statement.  To use logical operators to form more complex conditions.

4  2007 Pearson Education, Inc. All rights reserved. 4 6.1Introduction 6.2 Essentials of Counter-Controlled Repetition 6.3 For...Next Repetition Statement 6.4 Examples Using the For...Next Statement 6.5 GradeBook Case Study: Select...Case Multiple- Selection Statement 6.6 Do...Loop While Repetition Statement 6.7 Do...Loop Until Repetition Statement 6.8 Using the Exit Statement in Repetition Statements 6.9 Using the Continue Statement in Repetition Statements 6.10 Logical Operators 6.11 (Optional) Software Engineering Case Study: Identifying Objects’ States and Activities in the ATM System 6.12 Wrap-Up

5  2007 Pearson Education, Inc. All rights reserved. 5 6.1 Introduction Continue structured-programming discussion – Introduce Visual Basic’s remaining control structures For…Next, Select…Case, Do…Loop While, Do…Loop Until – Introduce the Exit and Continue statements – Introduce the logical operators

6  2007 Pearson Education, Inc. All rights reserved. 6 6.2 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: – Control variable (loop counter) – Initial value of the control variable – Increment/decrement of control variable through each loop – Loop-continuation condition that tests for the final value of the control variable

7  2007 Pearson Education, Inc. All rights reserved. 7 Outline WhileCounter.vb Control-variable name is counter Control-variable initial value is 1 Condition tests for counter ’s final value Increment for counter

8  2007 Pearson Education, Inc. All rights reserved. 8 6.3 For…Next Repetition Statement Handles counter-controlled-repetition details Format: For initialization To finalValue Step increment statement Next – If Step is omitted, the default increment is 1 – If the counter is initialize in the For…Next header, then that variable has a scope of that loop. Can usually be rewritten as: initialization While variable <= finalValue statement increment End While

9  2007 Pearson Education, Inc. All rights reserved. 9 Outline ForCounter.vb Control-variable name is counter Control-variable initial value is 2 Condition tests for counter ’s final value Increment for counter

10  2007 Pearson Education, Inc. All rights reserved. 10 Good Programming Practice 6.1 Place a blank line before and after each control statement to make it stand out in the program.

11  2007 Pearson Education, Inc. All rights reserved. 11 Good Programming Practice 6.2 Vertical spacing above and below control statements, as well as indentation of the bodies of control statements, gives programs a two-dimensional appearance that enhances readability.

12  2007 Pearson Education, Inc. All rights reserved. 12 Error-Prevention Tip 6.1 Use a For...Next loop for counter-controlled repetition. Off-by-one errors (which occur when a loop is executed for one more or one less iteration than is necessary) tend to disappear, because the terminating value is clear.

13  2007 Pearson Education, Inc. All rights reserved. 13 Fig. 6.3 | For...Next header components.

14  2007 Pearson Education, Inc. All rights reserved. 14 Common Programming Error 6.1 Counter-controlled loops should not be controlled with floating-point variables. Floating-point values are represented only approximately in the computer’s memory; this can lead to imprecise counter values and inaccurate tests for termination.

15  2007 Pearson Education, Inc. All rights reserved. 15 Error-Prevention Tip 6.2 Although the value of the control variable can be changed in the body of a For...Next loop, avoid doing so, because this practice can lead to subtle errors.

16  2007 Pearson Education, Inc. All rights reserved. 16 Common Programming Error 6.2 In nested For...Next loops, the use of the same control-variable name in more than one loop is a compilation error.

17  2007 Pearson Education, Inc. All rights reserved. 17 Fig. 6.4 | For...Next repetition statement activity diagram.

18  2007 Pearson Education, Inc. All rights reserved. 18 6.4 Examples Using the For…Next Statement Varying control variable in For…Next headers – Vary control variable from 1 to 100 in increments of 1 For i = 1 To 100 Step 1 – Vary control variable from 100 to 1 in increments of –1 For i = 100 To 1 Step -1 – Vary control variable from 7 to 77 in increments of 7 For i = 7 To 77 Step 7 – Vary control variable from 20 to 2 in decrements of 2 For i = 20 To 2 Step -2 – Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 For i = 2 To 20 Step 3 – Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 For i = 99 To 0 Step -11

19  2007 Pearson Education, Inc. All rights reserved. 19 6.4 Examples Using the For…Next Statement (Cont.) Programmers can choose from a listing of MessageBoxButtons and MessageBoxIcon constants for MessageBox es.

20  2007 Pearson Education, Inc. All rights reserved. 20 Outline Sum.vb increment number by 2 each iteration Using the features of MessageBox by passing in MessageBoxButtons and MessageBoxIcon constants

21  2007 Pearson Education, Inc. All rights reserved. 21 Fig. 6.6 | Message dialog icon constants.

22  2007 Pearson Education, Inc. All rights reserved. 22 Fig. 6.7 | Message dialog button constants.

23  2007 Pearson Education, Inc. All rights reserved. 23 6.4 Examples Using the For…Next Statement (Cont.) Formatting output Ex: {0: C } – The C format specifier indicates that its corresponding value (amount) should be displayed in monetary format With a dollar sign to the left of the value and commas in the proper location Decimal should be used for monetary amounts; Single and Double can cause calculational errors.

24  2007 Pearson Education, Inc. All rights reserved. 24 Outline Interest.vb (1 of 2 ) Decimal should be used for monetary value Calculate amount with For…Next statement amount is displayed in currency format

25  2007 Pearson Education, Inc. All rights reserved. 25 Outline Interest.vb (2 of 2 )

26  2007 Pearson Education, Inc. All rights reserved. 26 Error-Prevention Tip 6.3 Do not use variables of type Single or Double to perform precise monetary calculations. The imprecision of floating-point numbers can cause errors that result in incorrect monetary values. Use the type Decimal for monetary calculations.

27  2007 Pearson Education, Inc. All rights reserved. 27 Performance Tip 6.1 Avoid placing inside a loop the calculation of an expression whose value does not change each time through the loop. Such an expression should be evaluated only once and prior to the loop.

28  2007 Pearson Education, Inc. All rights reserved. 28 6.5 Select…Case Multiple-Selection Statement Select…Case statement – Used for multiple selections – Constant integral expression, character constant, and constant variable Multiple values can be tested using the, separator A range of values can be test using the To keyword – Relational condition Use the Is keyword before the operator – Goes to Case Else if none of the Case s matches Optional

29  2007 Pearson Education, Inc. All rights reserved. 29 Outline GradeBook.vb (1 of 5 ) Variables to be incremented determined by the Select…Case statement

30  2007 Pearson Education, Inc. All rights reserved. 30 Outline GradeBook.vb (2 of 5 ) Display prompt

31  2007 Pearson Education, Inc. All rights reserved. 31 Outline GradeBook.vb (3 of 5 ) Loop condition: if there is more data input

32  2007 Pearson Education, Inc. All rights reserved. 32 Outline GradeBook.vb (4 of 5 ) Select statement determines which Case label to execute, depending on controlling variable grade is the controlling variable Case Else for grade less than 60

33  2007 Pearson Education, Inc. All rights reserved. 33 Outline GradeBook.vb (5 of 5 ) Prints out results

34  2007 Pearson Education, Inc. All rights reserved. 34 Common Programming Error 6.3 Duplicate Case statements are logic errors. At run time, the first matching Case is executed.

35  2007 Pearson Education, Inc. All rights reserved. 35 Common Programming Error 6.4 If the value on the left side of the To keyword in a Case statement is larger than the value on the right side, the Case is ignored during program execution; this is probably a logic error.

36  2007 Pearson Education, Inc. All rights reserved. 36 Error-Prevention Tip 6.4 Provide a Case Else in Select...Case statements. Cases not handled in a Select...Case statement are ignored unless a Case Else is provided. The inclusion of a Case Else statement can facilitate the processing of exceptional conditions.

37  2007 Pearson Education, Inc. All rights reserved. 37 Outline GradeBookTest.vb (1 of 2 ) Call GradeBook Public methods to count grades

38  2007 Pearson Education, Inc. All rights reserved. 38 Outline GradeBookTest.vb (2 of 2 )

39  2007 Pearson Education, Inc. All rights reserved. 39 Fig. 6.11 | Select...Case multiple-selection statement UML activity diagram.

40  2007 Pearson Education, Inc. All rights reserved. 40 6.6 Do … Loop While Repetition Statement Do…Loop While structure – Similar to While structure; the body loops while condition is True – Tests loop-continuation after performing body of loop i.e., loop body always executes at least once Format: Do statement Loop While condition

41  2007 Pearson Education, Inc. All rights reserved. 41 Error-Prevention Tip 6.5 Infinite loops occur when the loop-continuation condition in a While, Do While Loop or Do Loop While statement never becomes false.

42  2007 Pearson Education, Inc. All rights reserved. 42 Outline DoLoopWhile.vb Declares and initializes control variable counter Variable counter ’s value is displayed before testing counter ’s final value

43  2007 Pearson Education, Inc. All rights reserved. 43 6.6 Do … Loop Until Repetition Statement Do…Loop Until structure – Similar to Do Until…Loop structure; the body loops while condition is False – Tests loop-continuation after performing body of loop i.e., loop body always executes at least once Format: Do statement Loop Until condition

44  2007 Pearson Education, Inc. All rights reserved. 44 Outline DoLoopUntil.vb Declares and initializes control variable counter Variable counter ’s value is displayed before testing counter ’s final value

45  2007 Pearson Education, Inc. All rights reserved. 45 Common Programming Error 6.5 Including an incorrect relational operator or an incorrect final value for a loop counter in the condition of any repetition statement can cause off-by-one errors.

46  2007 Pearson Education, Inc. All rights reserved. 46 Error-Prevention Tip 6.6 Infinite loops occur when the loop-termination condition in a Do Until...Loop or Do... Loop Until statement never becomes true.

47  2007 Pearson Education, Inc. All rights reserved. 47 Fig. 6.14 | Do...Loop While repetition statement activity diagram.

48  2007 Pearson Education, Inc. All rights reserved. 48 Error-Prevention Tip 6.7 In a counter-controlled loop, ensure that the control variable is incremented (or decremented) appropriately in the body of the loop to avoid an infinite loop.

49  2007 Pearson Education, Inc. All rights reserved. 49 6.8 Using the Exit Statement in Repetition Statements Exit statement – Alter flow of control – Causes immediate exit from control structure Used in Do While…Loop, Do…Loop While, Do Until…Loop, Do…Loop Until, For…Next, While, Select…Case, or Property statements Example: Exit Do

50  2007 Pearson Education, Inc. All rights reserved. 50 Outline ExitTest.vb (1 of 3 ) Break out of For … Next statement when count equals 5

51  2007 Pearson Education, Inc. All rights reserved. 51 Outline ExitTest.vb (2 of 3 ) Break out of Do Until … Loop statement when count equals 5

52  2007 Pearson Education, Inc. All rights reserved. 52 Outline ExitTest.vb (3 of 3 ) Break out of While statement when count equals 5

53  2007 Pearson Education, Inc. All rights reserved. 53 6.9 Using the Continue Statement in Repetition Statements Continue statement – Alter flow of control – Skips remaining statements in loop body – Proceeds to next iteration Used in Do While…Loop, Do…Loop While, Do Until…Loop, Do…Loop Until, For…Next, or While statements Example: Continue For

54  2007 Pearson Education, Inc. All rights reserved. 54 Outline ContinueTest.vb (1 of 3 ) Skip line 13 and proceed to line 8 when count equals 5

55  2007 Pearson Education, Inc. All rights reserved. 55 Outline ContinueTest.vb (2 of 3 ) Skip line 28 and proceed to line 21 when count equals 5

56  2007 Pearson Education, Inc. All rights reserved. 56 Outline ContinueTest.vb (3 of 3 ) Skip line 43 and proceed to line 36 when count equals 5

57  2007 Pearson Education, Inc. All rights reserved. 57 6.10 Logical Operators Logical operators – Allows for forming more complex conditions – Combines simple conditions Visual Basic logical operators – And – Or – AndAlso – OrElse – Xor – Not

58  2007 Pearson Education, Inc. All rights reserved. 58 6.10 Logical Operators (Cont.) Logical And Operator – Consider the following If…Then statement If gender == FEMALE And age >= 65 Then seniorFemales += 1 – Combined condition is True if and only if both simple conditions are True – Combined condition is False if either or both of the simple conditions are False

59  2007 Pearson Education, Inc. All rights reserved. 59 Fig. 6.17 | Truth table for the logical And operator.

60  2007 Pearson Education, Inc. All rights reserved. 60 6.10 Logical Operators (Cont.) Logical Or Operator – Consider the following If…Then statement If ( semesterAverage >= 90 Or finalExam >= 90 ) Then Console.WriteLine( “Student grade is A” ) – Combined condition is True if either or both of the simple condition are True – Combined condition is False if both of the simple conditions are False

61  2007 Pearson Education, Inc. All rights reserved. 61 Fig. 6.18 | Truth table for the logical Or operator.

62  2007 Pearson Education, Inc. All rights reserved. 62 6.10 Logical Operators (Cont.) Logical AndAlso Operator – Works identically to And – Except AndAlso is a short-circuit evaluation Boolean OrElse Operator – Works identically to Or – Except OrElse is a short-circuit evaluation Short-Circuit Evaluation of Complex Conditions – Parts of an expression containing AndAlso or OrElse operators are evaluated only until it is known whether the condition is true or false – E.g., gender = FEMALE AndAlso age >= 65 Stops immediately if gender is not equal to FEMALE

63  2007 Pearson Education, Inc. All rights reserved. 63 Performance Tip 6.2 In expressions using operator AndAlso, if the separate conditions are independent of one another, place the condition most likely to be false as the leftmost condition. In expressions using operator OrElse, make the condition most likely to be true the leftmost condition. Each of these suggestions can reduce a program’s execution time.

64  2007 Pearson Education, Inc. All rights reserved. 64 Error-Prevention Tip 6.8 Avoid expressions with side effects in conditions, because side effects often cause subtle errors.

65  2007 Pearson Education, Inc. All rights reserved. 65 6.10 Logical Operators (Cont.) Logical Xor Operator – Exclusive OR – One of its operands is True and the other is False Evaluates to True – Both operands are True or both are False Evaluates to False Logical Not Operator – Negation – Unary operator – Opposite of the boolean value

66  2007 Pearson Education, Inc. All rights reserved. 66 Fig. 6.19 | Truth table for the logical exclusive OR ( Xor ) operator.

67  2007 Pearson Education, Inc. All rights reserved. 67 Fig. 6.20 | Truth table for the operator Not (logical negation).

68  2007 Pearson Education, Inc. All rights reserved. 68 Outline LogicalOperators.vb (1 of 3 ) Logical And operator Logical AndAlso operator Logical Or operator

69  2007 Pearson Education, Inc. All rights reserved. 69 Outline LogicalOperators.vb (2 of 3 ) Logical OrElse operator logical Xor operator Logical Not operator

70  2007 Pearson Education, Inc. All rights reserved. 70 Outline LogicalOperators.vb (3 of 3 )

71  2007 Pearson Education, Inc. All rights reserved. 71 Fig. 6.22 | Precedence of the operators discussed so far.

72  2007 Pearson Education, Inc. All rights reserved. 72 6.11 (Optional) Software Engineering Case Study: Identifying Objects’ State and Activities in the ATM System State Machine Diagrams – Commonly called state diagram – Model several states of an object – Show under what circumstances the object changes state – Focus on system behavior – UML representation State - Rounded rectangle Initial state - Solid circle Transitions - Arrows with stick arrowheads

73  2007 Pearson Education, Inc. All rights reserved. 73 Fig. 6.23 | State machine diagram for some of the states of the ATM object.

74  2007 Pearson Education, Inc. All rights reserved. 74 Software Engineering Observation 6.1 Software designers do not generally create state machine diagrams showing every possible state and state transition for all attributes—there are simply too many of them. State machine diagrams typically show only the most important or complex states and state transitions.

75  2007 Pearson Education, Inc. All rights reserved. 75 6.11 (Optional) Software Engineering Case Study: Identifying Objects’ State and Activities in the ATM System (Cont.) Activity Diagrams – Focus on system behavior – Model an object’s workflow during program execution – Model the actions the object will perform and in what order – UML representation Action state ( rectangle with its left and right sides replaced by arcs curving outwards) Action order ( arrow with a stick arrowhead) Initial state (solid circle) Final state (solid circle enclosed in an open circle)

76  2007 Pearson Education, Inc. All rights reserved. 76 Fig. 6.24 | Activity diagram for a BalanceInquiry transaction.

77  2007 Pearson Education, Inc. All rights reserved. 77 Fig. 6.25 | Activity diagram for a Withdrawal transaction.

78  2007 Pearson Education, Inc. All rights reserved. 78 Fig. 6.26 | Activity diagram for a Deposit transaction.


Download ppt " 2007 Pearson Education, Inc. All rights reserved. 1 6 6 Control Statements: Part2."

Similar presentations


Ads by Google