Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Similar presentations


Presentation on theme: "Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved."— Presentation transcript:

1 Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

2  Simple If Conditions: if condition then statement End if  If else as follows: If TextBox1.Text >= 60 Then lblGrade.Text = "Pass" Else lblGrade.Text = "Fail" End If © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

3

4

5  Visual Basic provides Logical operators that can combine multiple Boolean expressions into a compound expression OperatorEffect AndCombines two expressions into one. Both expressions must be true for the overall expression to be true. OrCombines two expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which. XorCombines two expressions into one. One expression (not both) must be true for the overall expression to be true. If both expressions are true, or both expressions are false, the overall expression is false. NotReverses the logical value of an expression: makes a true expression false and a false expression true.

6 ◦ Performing a Calculation in a Do While … Loop Repetition Statement  Consider a program segment designed to find the first power of 3 larger than 100.Suppose that the Integer variable product is initialized to 3.  When the following Do While … Loop statement finishes executing, product contains the result: Do While product <= 100 product = product * 3 Loop © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

7 ◦ While … End While Repetition Statement  The While…End While repetition statement behaves identically to the Do While … Loop repetition statement, so their activity diagrams are also identical.  For consistency, you should choose one or the other.In this book, we use the Do While … Loop statement.  The following While … End While statement reimplements the preceding Do While … Loop :  While product <= 100 product = product * 3 End While © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

8 ◦ Do Until … Loop Repetition Statement  Unlike the preceding repetition statements, the Do Until…Loop repetition statement executes its body statement(s) repeatedly as long as the condition (known as the loop-termination condition) evaluates to false.  You can write the preceding Do While … Loop statement as a Do Until … Loop statement as follows:  Do Until product > 100 product = product * 3 Loop © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

9 ◦ Do Until … Loop Repetition Statement  Because we’re using a Do Until … Loop statement, the statement’s action is performed when the statement’s loop- termination condition is false (that is, product is less than or equal to 100 ).  When the loop-termination condition ( product > 100, the leftmost guard condition) is true, the statement exits.If the condition in a Do Until … Loop is initially true, the body statement(s) do not execute. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

10  To illustrate how algorithms are developed, we solve a problem that averages student grades.  Consider the following problem statement: ◦ A class of students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.  The class average is equal to the sum of the grades divided by the number of students. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

11 ◦ GUI for the Class-Average Application  Figure 4.10 shows this application’s GUI.  For this example, we introduce the ListBox control, which we use to display the grades the user enters and to manipulate those grades to perform the class-average calculation.  Each grade that the user enters into the program by pressing the Submit Grade Button is placed in the ListBox.  We calculate the class average when the user presses the Calculate Average Button.  We also provide a Clear Grades Button to remove all the grades from the ListBox and clear the results, so the user can enter a new set of grades. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12

13 ◦ Setting the Form ’s Default Button  When you execute this program, notice that the Submit Grade Button has a blue highlight around it.  This Button is the Form ’s default Button—the one that will be pressed when the user presses the Enter key.  In this program, the user can type a value in the TextBox and press Enter to press the Submit Grade Button rather than clicking it with the mouse.  This convenience feature enables the user to rapidly enter grades.  You can specify a Form ’s default Button in the Properties window by setting the Form ’s AcceptButton property to the appropriate Button.  For this program, we set the AcceptButton property to the submitGradeButton. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

14 ◦ Pseudocode Algorithm with Counter-Controlled Repetition  After the user enters the grades and presses the Calculate Average Button, we use counter-controlled repetition to get the grades from the ListBox and process them one at a time.  This technique uses a variable called a counter (or control variable) to specify the number of times that a set of statements will execute.  In this example, repetition terminates when the counter exceeds the number of grades in the ListBox. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

15

16

17

18

19  The String class’s Format method performs the formatting.  The first argument to the method ( "{0:F}" ) is the format string, which acts as a placeholder for the value being formatted.  The numeric value that appears before the colon (in this case, 0 ) indicates which of Format ’s arguments will be formatted— 0 specifies the first argument after the format string passed to Format, namely average. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

20  The value after the colon (in this case, F ) is known as a format specifier, which indicates how a value is to be formatted.  The format specifier F indicates that a fixed-point number should be rounded to two decimal places by default.  The entire placeholder ( {0:F} ) is replaced with the formatted value of variable average.  You can change the number of decimal places to display by placing an integer value after the format specifier—for example, the string "{0:F3}" rounds the number to three decimal places. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

21  The ToString method can format a Date or DateTime value in a variety of ways  If the date is 8/10/2010 and the time is 3:22 PM  Tutorial 3-8 provides an opportunity to work with number formatting concepts Format String DescriptionToString() Value dShort Date"8/10/2010" DLong Date"Tuesday, August 10, 2010" tShort Time"3:22 PM" TLong Time"3:22:00 PM" FLong Date & Time "Tuesday August 10, 2010 3:22:00 PM"

22 ◦ GUI for the Licensing-Exam Analysis Application  Figure 4.13 shows this application’s GUI.  Each result is placed in the ListBox when the user presses the Submit Result Button.  We process all 10 results when the user presses the Analyze Results Button.  We also provide a Clear Results Button to reset the GUI so the user can enter a new set of results.  As in the previous example, we used the Form ’s AcceptButton property to set the Submit Result Button as the default Button.  We disabled the Analyze Results Button initially by setting its Enabled property to False. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

23

24


Download ppt "Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved."

Similar presentations


Ads by Google