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  All programs could be written in terms of only three control structures—the sequence structure, the selection structure and the repetition structure: ◦ If … Then, ◦ If … Then … Else ◦ Select…Case ◦ Do While … Loop ◦ While … End While and ◦ Do Until … Loop © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

3 ◦ The Do While…Loop and While…End While repetition statements execute a set of statements while a condition—known as the loop- continuation condition—remains true. If the condition is initially false, the set of statements does not execute. ◦ The Do Until…Loop repetition statement executes a set of statements until a condition—known as the loop-termination condition—becomes true. If the condition is initially true, the set of statements does not execute. ◦ The Do…Loop While repetition statement executes a set of statements while its loop-continuation condition remains true. The set of statements is guaranteed to execute at least once. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

4  Repetition Statements (contd..) ◦ The Do…Loop Until repetition statement executes a set of statements until its loop-termination condition becomes true. The set of statements is guaranteed to execute at least once. ◦ The For…Next repetition statement executes a set of statements a specified number of times—this is known as counter-controlled (or definite) repetition. ◦ The For Each…Next repetition statement (introduced in Chapter 7) performs a set of statements for every element of a so-called array or collection of values. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

5  A selection statement chooses among alternative courses of action.  Suppose that the passing grade on an examination is 60 (out of 100).  If studentGrade >= 60 Then resultLabel.Text = "Passed" ' display "Passed" End If © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

6  The If … Then … Else selection statement allows you to specify that a different action (or sequence of actions) is to be performed when the condition is true than when the condition is false.  The preceding pseudocode If…Else statement may be written in Visual Basic as  If studentGrade >= 60 Then resultLabel.Text = "Passed" ’ display "Passed" Else resultLabel.Text = "Failed" ’ display "Failed" End If © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

7

8 4.7 Nested If … Then … Else Statements

9  When the following Do While … Loop statement finishes executing, product contains the result:  Do While product <= 100 product = product * 3 ' compute next power of 3 Loop © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

10  The While…End While repetition statement behaves identically to the Do While … Loop repetition statement.  While product <= 100 product = product * 3 ' compute next power of 3 End While © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

11  You can write the preceding Do While … Loop statement as a Do Until … Loop statement as follows:  Do Until product > 100 product = product * 3 'compute next power of 3 Loop  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 ). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

12

13 Handling empty string

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

15

16

17  The statement in lines 43–45 outputs the class average, we used the expression  String.Format("{0:F}", average)  which indicates that average ’s value should be displayed as a number with a specific number of digits to the right of the decimal point—called a fixed-point number. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

18  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.

19  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.

20  Breakpoints are special markers that you can set at any executable line of code.  You cannot place them on comments or whitespace.  When a running program reaches a breakpoint, execution pauses, allowing you to examine the values of variables to help determine whether logic errors exist.  For example, you can examine the value of a variable that stores a calculation’s result to determine whether the calculation was performed correctly.  You can also examine the value of an expression.  Once the debugger pauses program execution, you can use various debugger commands to execute the program one statement at a time—this is called “single stepping” or simply “stepping.” © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

21  To insert a breakpoint, left click in the margin indicator bar (the gray margin at the left of the code window) or right click on the line and select Breakpoint > Insert Breakpoint.  Additionally, you can click a line of code then press F9 to toggle a breakpoint on and off for that line of code.  A solid circle appears in the margin indicator bar where you clicked and the entire code statement is highlighted, indicating that a breakpoint has been set. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

22

23  While in break mode, you can also explore the values of a method’s local variables using the debugger’s Locals window (Fig. 4.21).  To view the Locals window, select Debug > Windows > Locals.  Recall that all variables in Visual Basic get initialized. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

24

25  You’ll now execute the program one statement at a time using the debugger’s Step Over command.  You can access this command by selecting Debug > Step Over, by pressing Shift + F8 or by pressing the Step Over command’s toolbar icon (). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

26

27  To execute the program without the breakpoints, you can: ◦ Disable each breakpoint by right clicking in the line of code with the breakpoint and selecting Breakpoint > Disable Breakpoint, then run the program; ◦ Remove each breakpoint by right clicking the breakpoint and selecting Delete Breakpoint, then run the program; or ◦ Execute the program without debugging by pressing Ctrl + F5. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

28


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

Similar presentations


Ads by Google