Successfully reported this slideshow.
Upcoming SlideShare
×

# 05 control structures 2

1,416 views

Published on

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### 05 control structures 2

1. 1. Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2   Essentials of Counter-Controlled Repetition 5.3   For / Next Repetition Structure 5.4   Examples Using the For / Next Structure 5.5   Select Case Multiple-Selection Structure 5.6   Do / Loop While Repetition Structure 5.7   Do / Loop Until Repetition Structure 5.8 Using the Exit Keyword in a Repetition Structure 5.9   Logical Operators 5.10   Structured Programming Summary
2. 2. 5.1 Introduction <ul><li>Problem Solving </li></ul><ul><ul><li>Requires understanding of: </li></ul></ul><ul><ul><ul><li>Building blocks </li></ul></ul></ul><ul><ul><ul><li>Program-construction principles </li></ul></ul></ul>
3. 3. 5.2 Essentials of Counter-Controlled Repetition <ul><li>Elements needed </li></ul><ul><ul><li>Control variable </li></ul></ul><ul><ul><ul><li>Used to determine whether loop continues to iterate </li></ul></ul></ul><ul><ul><li>Initial value of control variable </li></ul></ul><ul><ul><li>Increment (or decrement) </li></ul></ul><ul><ul><ul><li>Describes how control variable is modified during each iteration </li></ul></ul></ul><ul><ul><li>Condition </li></ul></ul><ul><ul><ul><li>Tests for final value of control variable </li></ul></ul></ul>
4. 4. WhileCounter.vb 1 ' Fig. 5.1: WhileCounter.vb 2 ' Using the While structure to demonstrate counter-controlled 3 ' repetition. 4 5 Module modWhileCounter 6 7 Sub Main() 8 9 Dim counter As Integer = 2 ' initialization 10 11 While (counter <= 10 ) ' repetition condition 12 Console.Write(counter & &quot; &quot;) 13 counter += 2 ' increment counter 14 End While 15 16 End Sub ' Main 17 18 End Module ' modWhileCounter 2 4 6 8 10 While structure used for repetition Control variable defined and initialized to 2 Control variable incremented by 2 each iteration Condition tests if control variable is less than or equal to final value
5. 5. 5.3 For / Next Repetition Structure <ul><li>For / Next counter-controlled repetition </li></ul><ul><ul><li>Structure header initializes control variable, specifies final value and increment </li></ul></ul><ul><ul><ul><li>For keyword begins structure </li></ul></ul></ul><ul><ul><ul><ul><li>Followed by control variable initialization </li></ul></ul></ul></ul><ul><ul><ul><li>To keyword specifies final value </li></ul></ul></ul><ul><ul><ul><li>Step keyword specifies increment </li></ul></ul></ul><ul><ul><ul><ul><li>Optional </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Increment defaults to 1 if omitted </li></ul></ul></ul></ul><ul><ul><ul><ul><li>May be positive or negative </li></ul></ul></ul></ul><ul><ul><li>Next keyword marks end of structure </li></ul></ul><ul><ul><li>Executes until control variable greater (or less) than final value </li></ul></ul>
6. 6. ForCounter.vb Program Output 1 ' Fig. 5.2: ForCounter.vb 2 ' Using the For/Next structure to demonstrate counter-controlled 3 ' repetition. 4 5 Module modForCounter 6 7 Sub Main() 8 Dim counter As Integer 9 10 ' initialization, repetition condition and 11 ' incrementing are included in For structure 12 For counter = 2 To 10 Step 2 13 Console.Write(counter & &quot; &quot; ) 14 Next 15 16 End Sub ' Main 17 18 End Module ' modForCounter 2 4 6 8 10 Control variable initialized to 2 To specifies final value of 10 Step increments counter by 2 each iteration Next marks end of loop
7. 7. 5.3 For / Next Repetition Structure Fig. 5.3 Components of a typical For/Next header. For counter = 2 To 10 Step 2 For keyword Initial value of control variable Final value of control variable Increment of control variable Control variable name To keyword Step keyword
8. 8. 5.4 Examples Using the For / Next Structure <ul><li>Examples </li></ul><ul><ul><li>Vary the control variable from 1 to 100 in increments of 1 </li></ul></ul><ul><ul><ul><li>For i = 1 To 100 </li></ul></ul></ul><ul><ul><ul><li>For i = 1 To 100 Step 1 </li></ul></ul></ul><ul><ul><li>Vary the control variable from 100 to 1 in increments of –1 </li></ul></ul><ul><ul><ul><li>For i = 100 To 1 Step –1 </li></ul></ul></ul><ul><ul><li>Vary the control variable from 7 to 77 in increments of 7 </li></ul></ul><ul><ul><ul><li>For i = 7 To 77 Step 7 </li></ul></ul></ul><ul><ul><li>Vary the control variable from 20 to 2 in increments of –2 </li></ul></ul><ul><ul><ul><li>For i = 20 To 2 Step -2 </li></ul></ul></ul>
9. 9. 5.4 Examples Using the For / Next Structure Fig. 5.4 Flowcharting a typical For/Next repetition structure. counter = 1 counter < = 10 (implicit) false true Console.WriteLine(counter * 10) counter += 1 (implicit) Establish initial value of control variable Determine if final value of control variable has been reached Body of loop (this can be multiple statements) Increment the control variable
10. 10. Sum.vb Program Output 1 ' Fig. 5.5: Sum.vb 2 ' Using For/Next structure to demonstrate summation. 3 4 Imports System.Windows.Forms 5 6 Module modSum 7 8 Sub Main() 9 10 Dim sum = 0 , number As Integer 11 12 ' add even numbers from 2 to 100 13 For number = 2 To 100 Step 2 14 sum += number 15 Next 16 17 MessageBox.Show( &quot;The sum is &quot; & sum, _ 18 &quot;Sum even integers from 2 to 100&quot; , _ 19 MessageBoxButtons. OK , MessageBoxIcon. Information ) 20 End Sub ' Main 21 22 End Module ' modSum Control variable counts by 2, from 2 to 100 Value of number is added in each iteration to determine sum of even numbers Text displayed in dialog Display a MessageBox Indicate button to be OK button Indicate icon to be Information icon Text displayed in title bar
11. 11. 5.4 Examples Using the For / Next Structure Fig. 5.6 Icons for message dialogs.
12. 12. 5.4 Examples Using the For / Next Structure Fig. 5.7 Button constants for message dialogs.
13. 13. Interest.vb 1 ' Fig. 5.8: Interest.vb 2 ' Calculating compound interest. 3 4 Imports System.Windows.Forms 5 6 Module modInterest 7 8 Sub Main() 9 10 Dim amount, principal As Decimal ' dollar amounts 11 Dim rate As Double ' interest rate 12 Dim year As Integer ' year counter 13 Dim output As String ' amount after each year 14 15 principal = 1000.00 16 rate = 0.05 17 18 output = &quot;Year&quot; & vbTab & &quot;Amount on deposit&quot; & vbCrLf 19 20 ' calculate amount after each year 21 For year = 1 To 10 22 amount = principal * ( 1 + rate) ^ year 23 output &= year & vbTab & _ 24 String.Format( &quot;{0:C}&quot; , amount) & vbCrLf 25 Next 26 27 ' display output 28 MessageBox.Show(output, &quot;Compound Interest&quot; , _ 29 MessageBoxButtons. Ok , MessageBoxIcon. Information ) 30 31 End Sub ' Main 32 33 End Module ' modInterest Perform calculation to determine amount in account Append year followed by the formatted calculation result and newline character to end of String output Specify C (for “currency”) as formatting code Type Decimal used for precise monetary calculations
14. 14. Program Output
15. 15. 5.4 Examples Using the For / Next Structure Fig. 5.9 String formatting codes.
16. 16. 5.5 Select Case Multiple-Selection Structure <ul><li>Multiple-Selection Structure </li></ul><ul><ul><li>Tests expression separately for each value expression may assume </li></ul></ul><ul><ul><li>Select Case keywords begin structure </li></ul></ul><ul><ul><ul><li>Followed by controlling expression </li></ul></ul></ul><ul><ul><ul><ul><li>Compared sequentially with each case </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Code in case executes if match is found </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Program control proceeds to first statement after structure </li></ul></ul></ul></ul><ul><ul><ul><li>Case keyword </li></ul></ul></ul><ul><ul><ul><ul><li>Specifies each value to test for </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Followed by code to execute if test is true </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Case Else </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Optional </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Executes if no match is found </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Must be last case in sequence </li></ul></ul></ul></ul></ul>
20. 20. 5.5 Select Case Multiple Fig. 5.11 Flowcharting the Select Case multiple-selection structure. Case a Case b Case z . . . Case Else action(s) false false false Case a action(s) Case b action(s) Case z action(s) true true true
21. 21. 5.6 Do / Loop While Repetition Structure <ul><li>Do / Loop While Repetition Structure </li></ul><ul><ul><li>Similar to While and Do / While </li></ul></ul><ul><ul><li>Loop-continuation condition tested after body executes </li></ul></ul><ul><ul><ul><li>Loop body always executed at least once </li></ul></ul></ul><ul><ul><li>Begins with keyword Do </li></ul></ul><ul><ul><li>Ends with keywords Loop While followed by condition </li></ul></ul>
22. 22. DoWhile.vb Program Output 1 ' Fig. 5.12: DoWhile.vb 2 ' Demonstrating the Do/Loop While repetition structure. 3 4 Module modDoWhile 5 6 Sub Main() 7 8 Dim counter As Integer = 1 9 10 ' print values 1 to 5 11 Do 12 Console.Write(counter & &quot; &quot; ) 13 counter += 1 14 Loop While (counter <= 5 ) 15 16 End Sub ' Main 17 18 End Module ' modDoWhile 1 2 3 4 5 Do keyword begins structure Loop While ends structure Condition tested after body executes
23. 23. 5.7 Do / Loop Until Repetition Structure Fig. 5.13 Flowcharting the Do/Loop While repetition structure. true false action(s) condition
24. 24. 5.7 Do / Loop Until Repetition Structure <ul><li>Do / Loop Until Repetition Structure </li></ul><ul><ul><li>Similar to Do Until / Loop structure </li></ul></ul><ul><ul><li>Loop-continuation condition tested after body executes </li></ul></ul><ul><ul><ul><li>Loop body always executed at least once </li></ul></ul></ul>
25. 25. LoopUntil.vb Program Output 1 ' Fig. 5.14: LoopUntil.vb 2 ' Using Do/Loop Until repetition structure 3 4 Module modLoopUntil 5 6 Sub Main() 7 8 Dim counter As Integer = 1 9 10 ' print values 1 to 5 11 Do 12 Console.Write(counter & &quot; &quot; ) 13 counter += 1 14 Loop Until counter > 5 15 16 End Sub ' Main 17 18 End Module ' modLoopUntil 1 2 3 4 5 6 7 8 9 Condition tested after body executes
26. 26. 5.7 Do / Loop Until Repetition Structure Fig. 5.15 Flowcharting the Do/Loop Until repetition structure. true false action(s) condition
27. 27. 5.8 Using the Exit Keyword in a Repetition Structure <ul><li>Exit Statements </li></ul><ul><ul><li>Alter the flow of control </li></ul></ul><ul><ul><ul><li>Cause immediate exit from a repetition structure </li></ul></ul></ul><ul><ul><li>Exit Do </li></ul></ul><ul><ul><ul><li>Executed in Do structures </li></ul></ul></ul><ul><ul><li>Exit For </li></ul></ul><ul><ul><ul><li>Executed in For structures </li></ul></ul></ul><ul><ul><li>Exit While </li></ul></ul><ul><ul><ul><li>Executed in While structures </li></ul></ul></ul>
28. 28. ExitTest.vb 1 ' Fig. 5.16: ExitTest.vb 2 ' Using the Exit keyword in repetition structures. 3 4 Imports System.Windows.Forms 5 6 Module modExitTest 7 8 Sub Main() 9 Dim output As String 10 Dim counter As Integer 11 12 For counter = 1 To 10 13 14 ' skip remaining code in loop only if counter = 3 15 If counter = 3 Then 16 Exit For 17 End If 18 19 Next 20 21 output = &quot;counter = &quot; & counter & _ 22 &quot; after exiting For/Next structure&quot; & vbCrLf 23 24 Do Until counter > 10 25 26 ' skip remaining code in loop only if counter = 5 27 If counter = 5 Then 28 Exit Do 29 End If 30 31 counter += 1 32 Loop 33 Loop specified to execute 10 times Exit For statement executes when condition is met, causing loop to exit Program control proceeds to first statement after the structure counter is 3 when loop starts, specified to execute until it is greater than 10 Exit Do executes when counter is 5, causing loop to exit
29. 29. Program Output 34 output &= &quot;counter = &quot; & counter & _ 35 &quot; after exiting Do Until/Loop structure&quot; & vbCrLf 36 37 While counter <= 10 38 39 ' skip remaining code in loop only if counter = 7 40 If counter = 7 Then 41 Exit While 42 End If 43 44 counter += 1 45 End While 46 47 output &= &quot;counter = &quot; & counter & _ 48 &quot; after exiting While structure&quot; 49 50 MessageBox.Show(output, &quot;Exit Test&quot; , _ 51 MessageBoxButtons. OK , MessageBoxIcon. Information ) 52 End Sub ' Main 53 54 End Module ' modExitTest counter is 5 when loop starts, specified to execute while less than or equal to 10 Exit While executes when counter is 7, causing loop to exit
30. 30. 5.9 Logical Operators <ul><li>Used to form complex conditions by combining simple ones </li></ul><ul><ul><li>Short-circuit evaluation </li></ul></ul><ul><ul><ul><li>Execute only until truth or falsity is known </li></ul></ul></ul><ul><ul><li>AndAlso operator </li></ul></ul><ul><ul><ul><li>Returns true if and only if both conditions are true </li></ul></ul></ul><ul><ul><li>OrElse operator </li></ul></ul><ul><ul><ul><li>Returns true if either or both of two conditions are true </li></ul></ul></ul>
31. 31. 5.9 Logical Operators (II) <ul><li>Logical Operators without short-circuit evaluation </li></ul><ul><ul><li>And and Or </li></ul></ul><ul><ul><ul><li>Similar to AndAlso and OrElse respectively </li></ul></ul></ul><ul><ul><ul><li>Always execute both of their operands </li></ul></ul></ul><ul><ul><ul><li>Used when an operand has a side effect </li></ul></ul></ul><ul><ul><ul><ul><li>Condition makes a modification to a variable </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Should be avoided to reduce subtle errors </li></ul></ul></ul></ul><ul><ul><li>Xor </li></ul></ul><ul><ul><ul><li>Returns true if and only if one operand is true and the other false </li></ul></ul></ul>
32. 32. 5.9 Logical Operators (III) <ul><li>Logical Negation </li></ul><ul><ul><li>Not </li></ul></ul><ul><ul><ul><li>Used to reverse the meaning of a condition </li></ul></ul></ul><ul><ul><ul><li>Unary operator </li></ul></ul></ul><ul><ul><ul><ul><li>Requires one operand </li></ul></ul></ul></ul><ul><ul><ul><li>Can usually be avoided by expressing a condition differently </li></ul></ul></ul>
33. 33. 5.9 Logical Operators Fig. 5.17 Truth table for the AndAlso (logical AND) operator.
34. 34. 5.9 Logical Operators Fig. 5.18 Truth table for the OrElse (logical OR) operator.
35. 35. 5.9 Logical Operators Fig. 5.19 Truth table for the boolean logical exclusive OR (Xor) operator. Fig. 5.20 Truth table for operator Not (logical NOT).
36. 36. LogicalOperator.vb 1 ' Fig. 5.21: LogicalOperator.vb 2 ' Using logical operators. 3 4 Public Class FrmLogicalOperator 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio .NET generated code 8 9 Private Sub FrmLogicalOperator_Load( _ 10 ByVal sender As System.Object, _ 11 ByVal e As System.EventArgs) Handles MyBase .Load 12 13 lblAndAlso.Text = &quot;AndAlso&quot; & vbCrLf & vbCrLf & _ 14 &quot;False AndAlso False: &quot; & ( False AndAlso False ) & _ 15 vbCrLf & &quot;False AndAlso True: &quot; & _ 16 ( False AndAlso True ) & vbCrLf & _ 17 &quot;True AndAlso False: &quot; & ( True AndAlso False ) & _ 18 vbCrLf & &quot;True AndAlso True: &quot; & ( True AndAlso True ) 19 20 lblOrElse.Text = &quot;OrElse&quot; & vbCrLf & vbCrLf & _ 21 &quot;False OrElse False: &quot; & ( False OrElse False ) & _ 22 vbCrLf & &quot;False OrElse True: &quot; & ( False OrElse True ) & _ 23 vbCrLf & &quot;True OrElse False: &quot; & ( True OrElse False ) & _ 24 vbCrLf & &quot;True OrElse True: &quot; & ( True OrElse True ) 25 26 lblAnd.Text = &quot;And&quot; & vbCrLf & vbCrLf & _ 27 &quot;False And False: &quot; & ( False And False ) & vbCrLf & _ 28 &quot;False And True: &quot; & ( False And True ) & vbCrLf & _ 29 &quot;True And False: &quot; & ( True And False ) & vbCrLf & _ 30 &quot;True And True: &quot; & ( True And True ) 31 Code generated by Visual Studio represented by this comment Demonstrate AndAlso operator Demonstrate OrElse operator Demonstrate And operator
37. 37. LogicalOperator.vb 32 lblOr.Text = &quot;Or&quot; & vbCrLf & _ 33 vbCrLf & &quot;False Or False: &quot; & ( False Or False ) & _ 34 vbCrLf & &quot;False Or True: &quot; & ( False Or True ) & _ 35 vbCrLf & &quot;True Or False: &quot; & ( True Or False ) & _ 36 vbCrLf & &quot;True Or True: &quot; & ( True Or True ) 37 38 lblXor.Text = &quot;Xor&quot; & vbCrLf & _ 39 vbCrLf & &quot;False Xor False: &quot; & ( False Xor False ) & _ 40 vbCrLf & &quot;False Xor True: &quot; & ( False Xor True ) & _ 41 vbCrLf & &quot;True Xor False: &quot; & ( True Xor False ) & _ 42 vbCrLf & &quot;True Xor True: &quot; & ( True Xor True ) 43 44 lblNot.Text = &quot;Not&quot; & vbCrLf & vbCrLf & _ 45 &quot;Not False: &quot; & ( Not False ) & vbCrLf & &quot;Not True: &quot; & _ 46 ( Not True ) 47 48 End Sub ' FrmLogicalOperator_Load 49 50 End Class ' FrmLogicalOperator Demonstrate Or operator Demonstrate Xor operator Demonstrate Not operator
38. 38. Program Output
39. 39. 5.9 Logical Operators Fig. 5.22 Precedence and associativity of the operators discussed so far.
40. 40. 5.10 Structured Programming Summary <ul><li>Structured Programming </li></ul><ul><ul><li>Promotes simplicity </li></ul></ul><ul><ul><li>Produces programs that are easier to understand, test, debug and modify </li></ul></ul><ul><ul><li>Rules for Forming Structured Programs </li></ul></ul><ul><ul><ul><li>If followed, an unstructured flowchart cannot be created </li></ul></ul></ul><ul><ul><li>Only three forms of control needed </li></ul></ul><ul><ul><ul><li>Sequence </li></ul></ul></ul><ul><ul><ul><li>Selection </li></ul></ul></ul><ul><ul><ul><ul><li>If / Then structure sufficient to provide any form of selection </li></ul></ul></ul></ul><ul><ul><ul><li>Repetition </li></ul></ul></ul><ul><ul><ul><ul><li>While structure sufficient to provide any form of repetition </li></ul></ul></ul></ul>
41. 41. 5.10 Structured Programming Summary Fig. 5.23 Visual Basic’s single-entry/single-exit sequence and selection structures. Sequence . . . Selection If / Then structure (single selection) T F If / Then / Else structure (double selection) T F . . . Select Case structure (multiple selection)
42. 42. 5.10 Structured Programming Summary Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures. While structure T F F T For / Next structure T F Do / Loop Until structure Do / Loop While structure F T Repetition
43. 43. 5.10 Structured Programming Summary Fig. 5.24 Visual Basic’s single-entry/single-exit repetition structures. Do While / Loop structure T F Do Until / Loop structure F T F T For Each / Next structure Repetition
44. 44. 5.10 Structured Programming Summary Fig. 5.25 Rules for forming structured programs.
45. 45. 5.10 Structured Programming Summary Fig. 5.26 Simplest flowchart. . . . Rule 2 Rule 2 Rule 2 Fig. 5.27 Repeatedly applying rule 2 of Fig. 5.25 to the simplest flowchart.
46. 46. 5.10 Structured Programming Summary Fig. 5.28 Applying rule 3 of Fig. 5.25 to the simplest flowchart. Rule 3 Rule 3
47. 47. 5.10 Structured Programming Summary Fig. 5.29 Stacked, nested and overlapped building blocks. Nested building blocks Stacked building blocks Overlapping building blocks (Illegal in structured programs)
48. 48. 5.10 Structured Programming Summary Fig. 5.30 Unstructured flowchart.