Presentation is loading. Please wait.

Presentation is loading. Please wait.

Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.

Similar presentations


Presentation on theme: "Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8."— Presentation transcript:

1 Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8

2 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 2 Objectives Understand and use multiple versions of the If…Then statement Understand and use the Select Case statement Understand and use the Do…Loop statement Understand and use the For…Next statement

3 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 3 Objectives (cont.) Understand and use the Exit statement Distinguish between syntax, runtime, and logic errors Explain the advantages of Try…Catch for structured exception handling

4 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 4 Objectives (cont.) Understand and effectively use the CheckBox and RadioButton controls Successfully write Windows applications where the flow of execution changes through conditions, loops, and error- handling structures

5 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 5 8-1 If…Then Statement The If…Then statement is one of the most important structures in any programming language because it gives developers control over the flow of execution and allows the software solution to support multiple possibilities.

6 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 6 8-1 If…Then Statement (cont.) The simple If…Then structure contains a condition followed by a block of statements that execute only when the condition evaluates to true. You can use the single-line format with the following syntax: –If condition Then statement

7 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 7 8-1 If…Then Statement (cont.) The multi-line format requires an End If and can be used to execute blocks of statements. The multi-line form may contain one or more ElseIf statements. The multi-line form may have an ending Else statement containing the code that executes when the previous conditions are false.

8 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 8 8-2 Select Case Statement The Select Case statement is a decision structure that executes statements based on the value of an expression. The three types of test constructs are: –A simple value –A relational value using the keyword IS –A range of values specified by the To keyword

9 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 9 8-3 Do…Loop Statement The Do…Loop statement is the general iterative statement. The four versions of the Do…Loop are: –Do While –Do Until –Do…Loop While –Do…Loop Until

10 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 10 8-4 For…Next Statement The For…Next statement is a loop that repeats a known number of times. The For…Next loop contains a loop variable whose values are determined by the loop’s start, end, and step values. The Step keyword allows you to specify a step value other than 1.

11 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 11 8-5 Exit Statement The Exit statement allows you to immediately exit from a decision, loop, or procedure code block. –Exit control A Do…Loop combined with an Exit statement makes a sentinel controlled loop easy to write and read.

12 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 12 8-6 Error Types When you program you create errors called bugs. The process of identifying and fixing errors is called debugging. Errors fall into one of three categories. –Syntax errors, runtime errors, and logic errors

13 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 13 Syntax Errors Syntax errors (or compiler errors) occur when you write code that does not follow the rules of the language. Syntax errors are the most common type of errors. When Visual Studio recognizes a syntax error, it marks the error with a blue wavy line underneath.

14 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 14 Runtime Errors Runtime errors are the result of syntactically correct code that cannot be executed. A runtime error generates an exception, which is an “exceptional situation” that requires special handling. If the exception is not handled, then the program will halt execution.

15 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 15 Runtime Errors (cont.) You should anticipate and handle code that may result in runtime errors. The Try…Catch statement allows for structured exception handling. Using this allows your solution to recover from runtime errors without crashing.

16 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 16 Logic Errors Logic errors are mistakes in the solution design or implementation that cause incorrect behavior. Logic errors are the most problematic because the program can continue running. The errors may cause other problems in the code and may go undetected.

17 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 17 8-7 CheckBox Control The CheckBox control allows the user to specify values such as Yes/No, True/False, or On/Off. You can use a group of CheckBox controls to display a set of options from which the user can select zero, one, or more options.

18 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 18 CheckBox Properties

19 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 19 8-8 RadioButton Control The RadioButton control allows the user to select a single item from a list of mutually exclusive options. When the user selects one RadioButton within a group, the others clear automatically.

20 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 20 RadioButton Properties

21 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 21 Chapter Summary A condition is an expression that evaluates to true or false or a data type that is implicitly convertible to Boolean. The If…Then statement can be single-line or multi-line. To execute blocks of statements, use multiple-line If…Then statements.

22 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 22 Chapter Summary (cont.) To support multiple branches based on the value of an expression, use the Select Case statement. The Do…Loop statement repeats a block of statements either while a condition is true or until it becomes true. The For…Next statement repeats a block of statements a specific number of times.

23 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 23 Chapter Summary (cont.) The Exit statement allows control to pass out from a condition or loop or procedure. Syntax errors are violations of the rules of the programming language. Runtime errors occur when an application attempts to perform an action that the system cannot execute.

24 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 24 Chapter Summary (cont.) Logic errors occur when the program generates incorrect output or displays incorrect behavior. Runtime errors produce exceptions that can be handled through the Try…Catch structure.

25 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 25 Chapter Summary (cont.) The CheckBox control indicates whether a particular value is on or off. The RadioButton control allows the user to select a single item from a list of mutually exclusive options.

26 Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8


Download ppt "Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8."

Similar presentations


Ads by Google