Anda di halaman 1dari 5

DATATYPE DATATYPE in a programming language describes that what type of data a variable can hold .

When we declare a variable, we have to tell the compiler about what type of the data the variable can hold or which data type the variable belongs to. Type Boolea n Byte Char Date Decimal Storage size 2 bytes 1 byte 2 bytes 8 bytes 16 bytes Value range True or False 0 to 255 (unsigned) 0 to 65535 (unsigned) January 1, 0001 to December 31, 9999 +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; 7.92281625142643 37593543950335 with 28 places to the right of the decimal; smallest non-zero number is 0.00000 00000000000000000000001 -1.79769313486231E+308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486231E+308 for positive values -2,147,483,648 to 2,147,483,647 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Any type can be stored in a variable of type Object -32,768 to 32,767 -3.402823E to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E for positive values 0 to approximately 2 billion Unicode characters

Double

8 bytes

Integer Long Object Short Single String

4 bytes 8 bytes 4 bytes 2 bytes 4 bytes Depends on implementing platform

Decision Structures

Visual Basic allows you to test conditions and perform different operations depending on the results of that test. You can test for a condition being true or false, for various values of an expression, or for various exceptions generated when you execute a series of statements. The decision statements supported by Visual Basic include: If...Then If...Then...Else Select...Case Try...Catch...Finally

If...Then...Else Statements You can use If...Then...Else statements to execute a specific statement or block of statements depending on the Boolean value of a condition. The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (True or False). This

includes values of other data types, such as numeric types, that have been converted to Boolean. Executing Statements if a Condition Is True To execute only one statement when a condition is True, you can use the single-line syntax ofIf...Then...Else, omitting the Else and End If statements, as in the following example: Sub FixDate() Dim MyDate As Date = #2/13/1973# If MyDate < Now Then MyDate = Now End Sub To execute more than one line of code when the condition is True, you use multiple-line syntax, which includes the End If statement. If you have no code to run when the condition is False, you omit theElse statement, as the following example shows: Dim AlertLabel As New System.Windows.Forms.Label ' ... Sub AlertUser(ByVal Value As Long) If Value = 0 Then AlertLabel.ForeColor = Color.Red AlertLabel.Font = New Font(AlertLabel.Font, _ FontStyle.Bold Or FontStyle.Italic) End If End Sub Executing Some Statements if a Condition Is True and Others if It Is False You can use If...Then...Else with the Else statement to define two blocks of executable statements. One block is executed if the condition is True, the other if it is False. Dim AlertLabel As New System.Windows.Forms.Label ' ... Sub AlertUser(ByVal Value As Long) If Value = 0 Then AlertLabel.ForeColor = Color.Red AlertLabel.Font = New Font(AlertLabel.Font, _ FontStyle.Bold Or FontStyle.Italic) Else AlertLabel.Forecolor = Color.Black AlertLabel.Font = New Font(AlertLabel.Font, _ FontStyle.Regular) End If End Sub

Testing Additional Conditions if the First Condition Is False


You can add one or more ElseIf statements to If...Then...Else to test additional conditions if the first condition is False. For example, the following Function procedure computes a payroll bonus based on performance rating. The statement following the Else statement runs only if the conditions in all of theIf and ElseIf statements are False. Function Bonus(ByVal Performance As Integer, ByVal Salary As Decimal) _ As Decimal If Performance = 1 Then Return Salary * 0.1 ElseIf Performance = 2 Then Return Salary * 0.09 ElseIf Performance = 3 Then Return Salary * 0.07 Else Return 0 End If End Function

Visual Basic tests the conditions in the order they appear in the If...Then...Else statements tests until it

encounters a True condition or an Else statement, at which point it executes the corresponding statement block. Execution then branches to the end of the If...Then...Else block. You can have any number of ElseIf statements, or none at all. You can include or omit an Elsestatement regardless of whether you have ElseIf statements. If...Then...Else statements can be nested to as many levels as you need. However, for readability, you might want to use Select...Case statements rather than multiple levels of nested If...Then...Elsestatements or many ElseIf statements.

Select...Case Statements
When you are comparing the same expression to several different values, you can use theSelect...Case statements as an alternative to the If...Then...Else statements. Whereas the If andElseIf statements can evaluate a different expression in each statement, the Select statement evaluates a single expression only once and uses it for every comparison. Visual Basic compares the value of the expression to the values in the Case statements in the order they appear in the Select...Case block. If it finds a match or a Case Else statement, it executes the corresponding statement block. In any case, it then executes the code following the End Selectstatement. You can have any number of Case statements, and you can include or omit a Case Else statement. In the following example, Select...Case is used to evaluate the performance rating that is passed to theFunction procedure. Note that each Case statement can contain more than one value, a range of values, or a combination of values and comparison operators. When a Case statement contains more than one value, the Case block is executed if any of the values match the value of the Selectstatement expression. Function Bonus(ByVal Performance As Integer, ByVal Salary As Decimal) _ As Decimal Select Performance Case 1 ' Performance is 1. Return Salary * 0.1 Case 2, 3 ' Performance is 2 or 3. Return Salary * 0.09 Case 5 To 7 ' Performance is 5, 6, or 7. Return Salary * 0.07 Case 4, 8 To 10 ' Performance is 4, 8, 9, or 10. Return Salary * 0.05 Case Is < 15 ' Performance is 11, 12, 13, or 14. Return 100 Case Else ' Performance is < 1 or > 14. Return 0 End Select End Function

Looping
Loop structures allow you to execute one or more lines of code repetitively. You can repeat the statements until a condition is true, until a condition is false, a specified number of times, or once for each object in a collection. The loop structures supported by Visual Basic include: While Do...Loop For...Next For Each...Next

While

You can use the While statement to execute a block of statements an indefinite number of times depending on the Boolean value of a condition. The statements are repeated as long as the condition isTrue. The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (True or False). This includes values of other data types, such as numeric types, that have been converted to Boolean.

The While statement always checks the condition before it begins the loop. Looping continues while the condition remains True. Ex. Dim Counter As Integer = 0 Dim Number As Integer = 10 While Number > 6 Number = Number - 1 Counter = Counter + 1 End While MsgBox("The loop ran " & Counter & " times.")

' Runs 4 times.

You can transfer out of a While statement by using the Exit While statement. which is a loop that could run an extremely large or even infinite number of times. If the condition is True, use Exit While to escape. If the condition isFalse, the loop continues running. Ex. Dim Counter As Integer = 0 Dim Number As Integer = 8 While Number <> 10 If Number < 0 Then Exit While Number = Number - 1 Counter = Counter + 1 End While MsgBox("The loop ran " & Counter & " times.")

' Runs 9 times.

Do...Loop Statements You can use Do...Loop statements to execute a block of statements an indefinite number of times depending on the Boolean value of a condition. The statements can be repeated either while the condition is True or until it becomes True. The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (True or False). This includes values of other data types, such as numeric types, that have been converted to Boolean. Repeating Statements While a Condition Is True There are two ways to use the While keyword to check a condition in a Do loop. You can check the condition before you enter the loop, or you can check it after the loop has run at least once. Looping continues while the condition remains True. Sub CheckWhileFirst() Dim Counter As Integer = 0 Dim Number As Integer = 10 Do While Number > 6 Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") End Sub ' ... Sub CheckWhileLast() Dim Counter As Integer = 0 Dim Number As Integer = 5 Do Number = Number - 1 Counter = Counter + 1 Loop While Number > 6 MsgBox("The loop ran " & Counter & " times.") End Sub

' Runs 4 times.

' Runs 1 time.

In the following example, the CheckWhileFirst procedure tests the condition before it enters the loop. IfNumber had been initialized to 6 instead of 10, the statements inside the loop would never run. In theCheckWhileLast procedure, the statements inside the loop run once before testing the condition, which isFalse on the first test.

Repeating Statements Until a Condition Becomes True There are two ways to use the Until keyword to check a condition in a Do loop. You can check the condition before you enter the loop, or you can check it after the loop has run at least once. Looping continues while the condition remains False. Sub CheckUntilFirst() Dim Counter As Integer = 0 Dim Number As Integer = 20 Do Until Number = 15 Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") End Sub ' ... Sub CheckUntilLast() Dim Counter As Integer = 0 Dim Number As Integer = 20 Do Number = Number - 1 Counter = Counter + 1 Loop Until Number = 15 MsgBox("The loop ran " & Counter & " times.") End Sub

' Runs 5 times.

' Runs 5 times.

In the following example, the CheckUntilFirst procedure tests the condition before it enters the loop. IfNumber had been initialized to 15 instead of 20, the statements inside the loop would never run. In theCheckUntilLast procedure, the statements inside the loop run once before testing the condition, which isFalse on the first test. Exiting a Do Loop from Inside the Loop You can transfer out of a Do loop by using the Exit Do statement. One use of this is to test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. If the condition is True, use Exit Do to escape. If the condition is False, the loop continues running. Dim Counter As Integer = 0 Dim Number As Integer = 8 Do Until Number = 10 If Number <= 0 Then Exit Do Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") ' Runs 8 times. In the following example, Number is assigned a value that could cause the loop to execute more than 2 ^ 31 times. The If statement checks for this and exits if it exists, preventing endless looping. Replacing the Continue Statement Visual Basic .NET does not support the Continue statement of previous versions of Visual Basic. However, you can achieve the same functionality by putting a statement label on the Loop statement and branching to it from the middle of the loop: Dim LoopCounter As Integer = 0 Do While LoopCounter < 100 LoopCounter += 1 Dim SkipToNextIteration As Boolean ' Local to this loop. ' Processing, which might change value of SkipToNextIteration. If SkipToNextIteration = True Then GoTo EndOfLoop ' More processing if SkipToNextIteration was still False. EndOfLoop: Loop ' Acts like Continue.

Anda mungkin juga menyukai