Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving and Control Statements: Part 2

Similar presentations


Presentation on theme: "Problem Solving and Control Statements: Part 2"— Presentation transcript:

1 Problem Solving and Control Statements: Part 2
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.

2 5.1  Introduction Chapter 4 began our introduction to the types of building blocks that are available for problem solving. In this chapter, we demonstrate the For…Next, Select…Case, Do…Loop While and Do…Loop Until control statements. © by Pearson Education, Inc. All Rights Reserved.

3 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4 5.2 For…Next Repetition Statement
The increment of a For…Next statement could be negative, in which case it’s called a decrement, and the loop actually counts downward. If the loop-continuation condition is initially false (for example, if the initial value is greater than the final value and the increment is positive), the For…Next’s body is not performed. Instead, execution proceeds with the first statement after the For…Next. © by Pearson Education, Inc. All Rights Reserved.

5 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

6 5.2.2 General Form of a For…Next Statement
The general form of the For…Next statement is For initialization To finalValue Step increment statement Next where the initialization expression initializes the loop’s control variable, finalValue determines whether the loop should continue executing and increment specifies the amount the control variable should be incremented (or decremented) each time through the loop. © by Pearson Education, Inc. All Rights Reserved.

7 5.2.3 Declaring the Control Variable Before a For…Next Statement
In Fig. 5.1, the counter variable is declared and initialized in the For…Next header. The counter variable may be declared before the For…Next statement. For example, the code in Fig. 5.1 could have been written as Dim counter As Integer For counter = 2 To 10 Step outputLabel.Text &= counter & " " Next Although both forms are correct, declaring the control variable in the For…Next header is clearer and more concise. © by Pearson Education, Inc. All Rights Reserved.

8 5.2.3 Declaring the Control Variable Before a For…Next Statement
The difference between the two forms is that if the initialization expression in the For…Next statement header declares the control variable (as we’ve done in Fig. 5.1), the control variable can be used only in the body of the For…Next statement—it will be unknown outside the For…Next statement. This restricted use of the control-variable name is known as the variable’s scope, which specifies where the variable can be used in a program. If the control variable is declared before the For…Next control statement, it can be used from the point of declaration, inside the control statement’s body and after the control statement as well. © by Pearson Education, Inc. All Rights Reserved.

9 5.2.6 Local Type Inference For example, in the declaration
Dim x = 7 the compiler infers that the variable x should be of type Integer, because the compiler assumes that whole- number values, like 7, are Integer values. In the declaration Dim y = the compiler infers that the variable y should be of type Double, because the compiler assumes that floating- point literals, like , are Double values. © by Pearson Education, Inc. All Rights Reserved.

10 5.2.6 Local Type Inference In the declaration
Dim z = D the compiler infers that the variable z should be of type Decimal, because the value is followed by the literal type character D, which indicates that the value is of type Decimal. Some common literal type characters include C for Char ("T"C), F for Single (123.45F), S for Short (123S) and L for Long (123L). © by Pearson Education, Inc. All Rights Reserved.

11 5.2.6 Local Type Inference You can also use local type inference with control variables in the header of a For…Next statement. For example, the For…Next header For counter As Integer = 1 To 10 can be written as For counter = 1 To 10 In this case, counter is of type Integer because it is initialized with an Integer literal (1). © by Pearson Education, Inc. All Rights Reserved.

12 5.3 Examples Using the For…Next Statement
The following examples demonstrate different ways of varying the control variable in a For…Next statement. In each case, we write the appropriate For…Next header using local type inference. Vary the control variable from 1 to 100 in increments of 1. For i = 1 To 100 or For i = 1 To 100 Step 1 Vary the control variable from 100 to 1 in increments of -1 (decrements of 1). For i = 100 To 1 Step -1 © by Pearson Education, Inc. All Rights Reserved.

13 5.3 Examples Using the For…Next Statement
Vary the control variable from 7 to 77 in increments of 7. For i = 7 To 77 Step 7 Vary the control variable from 20 to 2 in increments of -2 (decrements of 2). For i = 20 To 2 Step -2 © by Pearson Education, Inc. All Rights Reserved.

14 5.4  Val method Val ignores whitespace (for example, "33 5" will be converted to 335). Val recognizes the decimal point as well as plus and minus signs that indicate whether a number is positive or negative (such as -123). Val does not recognize such symbols as commas and dollar signs. If Val receives an argument that cannot be converted to a number (for example, "b35", which begins with a nonnumeric character, or an empty string), it returns 0. © by Pearson Education, Inc. All Rights Reserved.

15 5.4 Format Other commonly used format specifiers include:
F for floating-point numbers—sets the number of decimal places to two (introduced in Section 4.10). N for numbers—separates every three digits with a comma and sets the number of decimal places to two. D for integers © by Pearson Education, Inc. All Rights Reserved.

16 Select case Dim n As Integer = TextBox1.Text Select Case n
Case 0 To 59 lblGrade.Text = "F" Case 60 To 69 lblGrade.Text = "D" Case 70 To 79 lblGrade.Text = "C" Case 80 To 89 lblGrade.Text = "B" Case 90 To 100 lblGrade.Text = "A" Case Else lblGrade.Text = "Error" End Select © by Pearson Education, Inc. All Rights Reserved.

17 Tutorial © by Pearson Education, Inc. All Rights Reserved.

18 5.6 Select…Case Multiple-Selection Statement
Types of Case Statements Case statements also can use relational operators to determine whether the controlling expression satisfies a condition. For example Case Is < 0 uses keyword Is along with the relational operator, <, to test for values less than 0. Multiple values can be tested in a Case statement by separating the values with commas, as in Case 0, 5 To 9 which tests for the value 0 or values in the range 5–9. Also, Cases can be used to test String values. © by Pearson Education, Inc. All Rights Reserved.

19 5.7 Do…Loop While and Do…Loop Until Repetition Statements
The Do…Loop While repetition statement is similar to the While…End While statement and Do While…Loop statement. In the While…End While and Do While…Loop statements, the loop-continuation condition is tested at the beginning of the loop, before the body of the loop is performed, so these are referred to as pre-test loops. © by Pearson Education, Inc. All Rights Reserved.

20 5.7 Do…Loop While and Do…Loop Until Repetition Statements
The Do…Loop While statement tests the loop- continuation condition after the loop body is performed, so it’s referred to as a post-test loop. In a Do…Loop While statement, the loop body is always executed at least once. © by Pearson Education, Inc. All Rights Reserved.

21 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

22 5.7 Do…Loop While and Do…Loop Until Repetition Statements
The following code uses a Do…Loop Until statement to reimplement the Do…Loop While statement of Fig. 5.11 Do ' display counter value outputLabel.Text &= counter & " " counter += 2 ' increment counter Loop Until counter > 10 The Do…Loop Until statement activity diagram is the same as the one in Fig. 5.12. The loop-termination condition (counter > 10) is not evaluated until after the loop body executes at least once. If this condition is true, the statement exits. If the condition is false (that is, the condition counter <= 10 is true), the loop continues executing. © by Pearson Education, Inc. All Rights Reserved.

23 5.8 Using Exit to Terminate Repetition Statements
There are many forms of the Exit statement, designed to terminate different types of repetition statements. When the Exit Do statement executes in a Do While…Loop, Do…Loop While, Do Until…Loop or Do…Loop Until statement, the program terminates that repetition statement and continues execution with the first statement after the repetition statement. Similarly, the Exit For statement and the Exit While statement cause immediate exit from For…Next and While…End While loops, respectively. The Exit Select statement causes immediate exit from a Select…Case statement. © by Pearson Education, Inc. All Rights Reserved.

24 5.9 Using Continue in Repetition Statements
A Continue statement terminates only the current iteration of a repetition statement and continues execution with the next iteration of the loop. The Continue Do statement can be executed in a Do While…Loop, Do…Loop While, Do Until…Loop or Do…Loop Until statement. Similarly, the Continue For statement and Continue While statement can be used in For…Next and While…End While statements, respectively. © by Pearson Education, Inc. All Rights Reserved.

25 Tutorial © by Pearson Education, Inc. All Rights Reserved.

26 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

27 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

28 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

29 © 1992-2011 by Pearson Education, Inc. All Rights Reserved.


Download ppt "Problem Solving and Control Statements: Part 2"

Similar presentations


Ads by Google