Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
The first line of the For … Next statement sometimes is called the For…Next header.
© by Pearson Education, Inc. All Rights Reserved.
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. © by Pearson Education, Inc. All Rights Reserved.
The starting value, ending value and increment portions of a For … Next statement can contain arithmetic expressions. For example, assume that x = 2 and y = 10. For j As Integer = x To 4 * x * y Step y \ x is equivalent to the header For j As Integer = 2 To 80 Step 5 © by Pearson Education, Inc. All Rights Reserved.
◦ 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 ◦ Vary the control variable over the sequence of the following values: 2, 5, 8, 11, 14, 17, 20. For i = 2 To 20 Step 3 ◦ Vary the control variable over the sequence of the following values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. For i = 99 To 0 Step -11 © by Pearson Education, Inc. All Rights Reserved.
Consider the following problem statement: ◦ A person invests $ in a savings account. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for up to 10 years. Allow the user to specify the principal amount, the interest rate and the number of years. To determine these amounts, use the following formula: a = p (1 + r) n ◦ where p is the original amount invested (that is, the principal) r is the annual interest rate (for example,.05 stands for 5%) n is the number of years a is the amount on deposit at the end of the nth year. © by Pearson Education, Inc. All Rights Reserved.
NumericUpDown control: We introduce the NumericUpDown control, which limits a user’s choices to a specific range of values—this helps to avoid data entry errors. In this program, we use the control to allow the user to choose a number of years from 1 to 10. The control’s Minimum property specifies the starting value in the range. Its Maximum property specifies the ending value in the range. © by Pearson Education, Inc. All Rights Reserved.
NumericUpDown control: Its Value property specifies the current value displayed in the control. You can also use this property programmatically to obtain the current value displayed. The Increment property specifies by how much the current number in the NumericUpDown control changes when the user clicks the control’s up (for incrementing) or down (for decrementing) arrow. © by Pearson Education, Inc. All Rights Reserved.
NumericUpDown control: When the user changes the value in a NumericUpDown control, a Value-Changed event occurs. If you double click the yearUpDown control in design mode, the IDE generates the event handler yearUpDown_ValueChanged to handle the Value-Changed event. © by Pearson Education, Inc. All Rights Reserved.
Using the Val Function to Convert String s to Numbers: ◦ This application uses TextBox es to read numeric input. Unfortunately, we cannot prevent users from accidentally entering nonnumeric input, such as letters and special characters like $ in the TextBox es. ◦ We use the built-in Val function to prevent inputs like this from terminating the application. ◦ Previously, you’ve used the method Format, which is part of class String. [String.format] © by Pearson Education, Inc. All Rights Reserved.
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.
Formatting Currency Output: ◦ Lines 20–21 display the amount on deposit at the end of each year in the resultsListBox. ◦ The text includes the current yearCounter value, a tab character ( vbTab ) to position to the second column and the result of the method call String.Format("{0:C}", amount). © by Pearson Education, Inc. All Rights Reserved.
Other commonly used format specifiers include: ◦ F for floating-point numbers—sets the number of decimal places to two. ◦ N for numbers—separates every three digits with a comma and sets the number of decimal places to two. ◦ D for Decimals © by Pearson Education, Inc. All Rights Reserved.
A Warning About Function Val: ◦ Although the value returned by Val is always a number, it’s not necessarily the value the user intended. ◦ For example, the user might enter the text $10.23, which Val evaluates to 0 without reporting an error. This causes the application to execute incorrectly. © by Pearson Education, Inc. All Rights Reserved.
Figure 5.9 enhances the Class Average application by using a Select … Case statement to determine whether each grade is the equivalent of an A, B, C, D or F and to increment the appropriate grade counter. © by Pearson Education, Inc. All Rights Reserved.
Instance variables
© by Pearson Education, Inc. All Rights Reserved.
◦ 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, Case s can be used to test String values. © by Pearson Education, Inc. All Rights Reserved.
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.
The Do … Loop Until repetition statement is simil ar to the Do Until … Loop statement, except that the loop-termination condition is tested after the loop body is performed Do ' display counter value outputLabel.Text &= counter & " " counter += 2 ' increment counter Loop Until counter > 10 The loop-termination condition ( counter > 10 ) is not evaluated until after the loop body executes at least once. © by Pearson Education, Inc. All Rights Reserved.
Logical And Operator: ◦ Suppose we wish to ensure that two conditions are both True in a program before a certain path of execution is chosen. ◦ In such a case, we can use the logical And operator as follows: If gender = "F" And age >= 65 Then seniorFemales += 1 End If ◦ This If … Then statement contains two simple conditions. This condition evaluates to True if and only if both simple conditions are True. © by Pearson Education, Inc. All Rights Reserved.
Logical Or Operator: ◦ Suppose we wish to ensure that either or both of two conditions are True before we choose a certain path of execution. ◦ We use the Or operator as in the following program segment: If (semesterAverage >= 90 Or finalExam >= 90 ) Then resultLabel.Text = "Student grade is A" End If ◦ This statement also contains two simple conditions. © by Pearson Education, Inc. All Rights Reserved.
Logical Not Operator: ◦ The Not (logical negation) operator enables you to “reverse” the meaning of a condition. ◦ If Not (value = 0) Then resultLabel.Text = "The value is " & value End If ◦ The logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is False. © by Pearson Education, Inc. All Rights Reserved.
◦ In the Dental Payment Calculator application, we’ll use two new GUI controls— CheckBox es and a message dialog—to assist the user in entering data. ◦ The CheckBox es allow the user to select which dental services were performed. ◦ Programs often use message dialogs to display important messages to the user. © by Pearson Education, Inc. All Rights Reserved.
A CheckBox is known as a state button because it can be in the “on” state or the “off” state. Any number of CheckBox es can be selected at a time, including none at all. If the CheckBox is “on” (checked), its Checked property has the Boolean value True ; otherwise, it’s False ( “ off ” ). © by Pearson Education, Inc. All Rights Reserved.