90 likes | 106 Views
Visual Basic 6 (VB6) Data Types, And Operators. Visual Basic uses building blocks such as Variables, Data Types, Procedures, Functions and Control Structures in its programming environment . Data types in Visual Basic 6: 1. Numeric Byte Store integer values in the range of 0 – 255
E N D
Visual Basic 6 (VB6) Data Types, And Operators Visual Basic uses building blocks such as Variables, Data Types, Procedures, Functions and Control Structures in its programming environment. Data types in Visual Basic 6: 1. Numeric Byte Store integer values in the range of 0 – 255 IntegerStoreinteger values in the range of (-32,768) - (+ 32,767) LongStoreinteger values in the range of (- 2,147,483,468) - (+ 2,147,483,468) SingleStorefloating point value in the range of (-3.4x10-38) - (+ 3.4x1038) DoubleStorelarge floating value which exceeding the single data type value Currencystoremonetary values. It supports 4 digits to the right of decimal point and 15 digits to the left
2. String • Use to store alphanumeric values. A variable length string can store approximately 4 billion characters 3. Date • Use to store date and time values. A variable declared as date type can store both date and time values and it can store date values 01/01/0100 up to 12/31/9999 4. Boolean • Boolean data types hold either a true or false value. These are not stored as numeric values and cannot be used as such. Values are internally stored as -1 (True) and 0 (False) and any non-zero value is considered as true. 5. Variant • Stores any type of data and is the default Visual Basic data type. In Visual Basic if we declare a variable without any data type by default the data type is assigned as default.
Operators in Visual Basic Arithmetical Operators • Add • Substract • Divide • Integer Division • Multiply • Exponent • Remainder of division20 Mod 62 • String concatenation"George"&" "&"Bush""George Bush“ Relational Operators • Greater than • Less than • Greater than or equal to • Less than or equal to • Not Equal to5 • Equal to Logical Operators • OR-Operation will be true if either of the operands is true • AND-Operation will be true only if both the operands are true
Variables In Visual Basic 6 • Variables are the memory locations which are used to store values temporarily. A defined naming strategy has to be followed while naming a variable. A variable name must begin with an alphabet letter and should not exceed 255 characters. It must be unique within the same scope. It should not contain any special character like %, &, !, #, @ or $. • EXPLICIT DECLARATION. Syntax • Dim variable [As Type] • For example, • Dim strName As String • Dim intCounter As Integer • Local Variables • A local variable is one that is declared inside a procedure. This variable is only available to the code inside the procedure and can be declared using the • Dim statements as given below. • Dim sum As Integer
Control Structures In Visual Basic 6.0 • Control Statements are used to control the flow of program's execution. Visual Basic supports control structures such as if... Then, if...Then ...Else, Select...Case, and Loop structures such as Do While...Loop, While...Wend, For...Next etc method. If...Then selection structure • The If...Then selection structure performs an indicated action only when the condition is True; otherwise the action is skipped. Syntax of the If...Then selection • If <condition> ThenstatementEnd If • e.g.: If average>75 ThentxtGrade.Text = "A"End If If...Then...Else selection structure • The If...Then...Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False. Syntax of the If...Then...Else selection • If <condition > ThenstatementsElsestatementsEnd If
e.g.: If average>50 ThentxtGrade.Text = "Pass"ElsetxtGrade.Text = "Fail"End If • Nested If...Then...Else selection structure • If < condition 1 > ThenstatementsElseIf < condition 2 > ThenstatementsElseIf < condition 3 > ThenstatementsElseStatementsEnd IfEnd IfEndIf • e.g.: Assume you have to find the grade using nested if and display in a text box • If average > 75 ThentxtGrade.Text = "A"ElseIf average > 65 ThentxtGrade.Text = "B"ElseIf average > 55 ThentxtGrade.text = "C"ElseIf average > 45 ThentxtGrade.Text = "S"ElsetxtGrade.Text = "F"End If
Select...Case selection structure Syntax of the Select...Case selection structure • Select Case IndexCase 0StatementsCase 1StatementsEnd Select e.g.: Assume you have to find the grade using select...case and display in the text box • Dim average as Integeraverage = txtAverage.TextSelect Case averageCase 100 To 75txtGrade.Text ="A"Case 74 To 65 txtGrade.Text ="B"Case 64 To 55txtGrade.Text ="C"Case 54 To 45txtGrade.Text ="S"Case 44 To 0txtGrade.Text ="F"Case ElseMsgBox "Invalid average marks"End Select
Loops (Repetition Structures) In Visual Basic 6 • Do While... Loop Statement TheDo While...Loop is used to execute statements until a certain condition is met. The following Do Loop counts from 1 to 100. Dim number As Integernumber = 1Do While number <= 100number = number + 1Loop • While... Wend Statement A While...Wend statement behaves like the Do While...Loop statement. The following While...Wend counts from 1 to 100 • Dim number As Integernumber = 1While number <=100number = number + 1Wend • Do...Loop While Statement The Do...Loop While statement first executes the statements and then test the condition after each execution. The following program block illustrates the structure: • Dim number As Longnumber = 0Do number = number + 1Loop While number < 201
Do Until...Loop Statement Unlike the Do While...Loop and While...Wend repetition structures, the Do Until... Loop structure tests a condition for falsity. Statements in the body of a Do Until...Loop are executed repeatedly as long as the loop-continuation test evaluates to False. • An example for Do Until...Loop statement. The coding is typed inside the click event of the command button • Dim number As Longnumber=0Do Until number > 1000number = number + 1Print numberLoop • The For...Next Loop • The For...Next Loop is another way to make loops in Visual Basic. For...Next repetition structure handles all the details of counter-controlled repetition. The following loop counts the numbers from 1 to 100: • Dim x As IntegerFor x = 1 To 50Print xNext