Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals
Comment Comment is a line in a program which is ignored by the compiler (i.e. it is not translated by the compiler) Comment is a line in a program which is ignored by the compiler (i.e. it is not translated by the compiler) In VB, a comment starts with ‘ (Single Quotation) In VB, a comment starts with ‘ (Single Quotation)
Keyword Every language has some reserved words that conveys some special meaning to the compiler, such words are called keywords Every language has some reserved words that conveys some special meaning to the compiler, such words are called keywords E.g. dim, if, end, … are keywords in VB E.g. dim, if, end, … are keywords in VB
Storing information in variables The first line of code is a Dim, or Dimension, statement, used for dimensioning (or declaring) variables. The first line of code is a Dim, or Dimension, statement, used for dimensioning (or declaring) variables. Example: Dim Num As Integer Dim Name As String Dim Price As Single
Naming Convention for variables The name must start with a letter, not a number or other character. The name must start with a letter, not a number or other character. The remainder of the name can contain letters, numbers, and/or underscore characters. No spaces, periods, or other punctuation characters are allowed. The remainder of the name can contain letters, numbers, and/or underscore characters. No spaces, periods, or other punctuation characters are allowed. The name must be unique within the variable's scope. (Scope refers to the context in which the variable is defined) The name must be unique within the variable's scope. (Scope refers to the context in which the variable is defined) The name cannot be one of Visual Basic's reserved words. The name cannot be one of Visual Basic's reserved words.
Examples Valid variable names: Valid variable names: a, test, a_test, a3798test89, absd_s3_ew Invalid variable names: Invalid variable names: a.test, a test, 3589a, dim, decimal
Changes to the Dim Statement Multiple declarations of the same type are now easier, as in the following example: Multiple declarations of the same type are now easier, as in the following example: Dim x, y As Integer Dim x, y As Integer The ability to initialize a variable within the Dim statement. The ability to initialize a variable within the Dim statement. Dim No_of_Cars As Integer = 10 Dim No_of_Cars As Integer = 10 ** Initialization only works for one-variable Dim statements.
Understanding Data Types Visual Basic.NET Data Type Values Stored Example BooleanTrue/FalseTrue Char Single Character “A” Date Dates and Times 12/21/ :00 PM Decimal Decimal or Whole 19.95D, Double Decimal Numbers 1.23E-10 Integer Whole Numbers 6753 Long Single Decimal Numbers Short Whole Numbers StringCharacters“HELLO”
Understanding Data Types Data Type Size (B) Range Boolean2 True or False Date8 January 1,100 to December 31,9999 short2 -32,768 to 32,767 Integer4 -2,147,483,648 to 2,147,483,647 Long8 19 digits Single4 -ve no: E38 to E-45 +ve no: E-45 to E38 Double8 -ve no: E308 to E-324 +ve no: E-324 to E308 Decimal16 Up to 28 digits to the left or right of.
Using Math Operations OperationOperator Addition+ Subtraction- Multiplication* Division/ Integer division \ ModulusMod Exponentiation^
Addition and Subtraction Using the Addition Operator Using the Addition Operator result = number1 + number2[+ number3] result = number1 + number2[+ number3] result = result = Using the Subtraction Operator Using the Subtraction Operator result = number1 - number2[- number3] result = number1 - number2[- number3] result = result =
Multiplication and Division Using the Multiplication Operator Using the Multiplication Operator result = number1* number2 [* number3] Using the Division Operators Using the Division Operators result = number1 / number2 [/ number3] result = 4/3 ‘will return result = number1 \ number2 [\ number3] result = 4\3 ‘will return 1
Modulus and Exponentiation Using the Modulus Operator Using the Modulus Operator result = number1 mod number2 result = 20 mod 3 ‘will return 2 Using the Exponentiation Operator Using the Exponentiation Operator answer = number1 ^ exponent answer = 3 ^ 2 ‘will return 9
Incrementing the Value of a Variable One frequent use of variables is a counter, where the same variable can be updated with the result of an equation. One frequent use of variables is a counter, where the same variable can be updated with the result of an equation. intNumber = intNumber + 1 (Shortcut: intNumber += 1) intNumber = intNumber – 1 (Shortcut: intNumber -= 1)
Operator Precedence Exponentiation (^) Exponentiation (^) Negation (-) Negation (-) Multiplication and division (*, /) Multiplication and division (*, /) Integer division (\) Integer division (\) Modulus arithmetic (Mod) Modulus arithmetic (Mod) Addition and subtraction (+, -) Addition and subtraction (+, -) Important Note: You can override normal operator precedence by using parentheses to group sub-expressions that you want to be evaluated first.
Controlling the Flow of Your Program Control statements: Control statements: Without control statements, your program would start at the first line of code and proceed line by line until the last line was reached, at which point the program would stop. Without control statements, your program would start at the first line of code and proceed line by line until the last line was reached, at which point the program would stop. Two types: Two types: Decision statement Decision statement Loop Loop
Control statements Decision statement: Decision statement: This statement is used to control the execution of parts of your program, based on conditions that exist at the time the statement is encountered. This statement is used to control the execution of parts of your program, based on conditions that exist at the time the statement is encountered. Two basic types of decision statements are :- If statement and Select Case statement
Control statements Loops: Loops: Loops are used to perform repetitive tasks in a program. Loops are used to perform repetitive tasks in a program. Three main types of loops are :- Counter Loops (For loop) Counter Loops (For loop) Conditional Loops (Do loop) Conditional Loops (Do loop) Enumerator loops (For Each loop) Enumerator loops (For Each loop)
Loops Counter, or For, loops perform a task a set number of times. Counter, or For, loops perform a task a set number of times. Conditional, or Do, loops perform a task while a specified condition exists or until a specified condition exists. Conditional, or Do, loops perform a task while a specified condition exists or until a specified condition exists. Enumerator loops are used to perform an action on each item in a group of objects. Enumerator loops are used to perform an action on each item in a group of objects.
Understanding If Statements Syntax: If condition Then command Example: 'Single-Line IF If x > 5 then x = 0 'Multiple-Line IF If x > 5 Then x = 0 End If
Understanding If Statements Using Multiple Commands within an If Block Using Multiple Commands within an If Block Example: If DepositAmt > 0 Then TotalPaid = TotalPaid + DepositAmt TotalPaid = TotalPaid + DepositAmt DepositAmt = 0 DepositAmt = 0 End If End If
Understanding If Statements Working with the False Condition Working with the False ConditionSyntax: If condition Then statements to process when condition is True statements to process when condition is TrueElse statements to process when condition is False statements to process when condition is False End If
Understanding If Statements Working with Multiple If Statements Working with Multiple If Statements The ElseIf statement enables you to specify another condition to evaluate when the first condition is False. The ElseIf statement enables you to specify another condition to evaluate when the first condition is False.Example: If TestValue < 0 Then lblResult.Text = "Negative" lblResult.Text = "Negative" ElseIf TestValue = 0 Then lblResult.Text = "Zero" lblResult.Text = "Zero"Else lblResult.Text = "Positive" lblResult.Text = "Positive" End If
Using Boolean Logic in If Conditions The logical operators used with True/False conditions are as follows: The logical operators used with True/False conditions are as follows: And — Expressions on both sides of the And must evaluate True for the result to be True. If one or more expressions in the And comparison is False, the result is False. And — Expressions on both sides of the And must evaluate True for the result to be True. If one or more expressions in the And comparison is False, the result is False. Or — If either side of an Or comparison is True, or both sides are True, then the result is True. The result of an Or is False only if both expressions are False. Or — If either side of an Or comparison is True, or both sides are True, then the result is True. The result of an Or is False only if both expressions are False.
Using Boolean Logic in If Conditions Xor — This stands for exclusive or. This operator is similar to Or but False if both sides are True. Not — Negates the result of an expression, Not True is False and Not False is True.
Boolean Operators xyx And y FFF FTF TFF TTT xyx Or y FFF FTT TFT TTT xyx Xor y FFF FTT TFT TTF xNot x FT TF
Using Boolean Logic in If Conditions IF username.text=“ahsan” And password.text = “123” Then lblMsg.text = “Welcome Ahsan” Else lblMsg.text = “Login Failed” End If
Working with Loops For Loops For Loops Dim i As Integer Dim i As Integer Dim Output As Integer = 0 Dim Output As Integer = 0 For i = 1 To 10 For i = 1 To 10 Output = Output + 1 Output = Output + 1 Next i Next i
Working with Loops Using Do While Statements Using Do While Statements Dim Counter As Integer = 0 Do While Counter < 10 Counter += 1 Counter += 1Loop
Working with Loops Using Do While Statements Using Do While Statements Dim Counter As Integer = 0 Do Counter += 1 Counter += 1 Loop While Counter < 10
Working with Loops Note Note Do not put the While condition clause in both the Do and the Loop statements because doing so causes an error when you try to run your program. Do not put the While condition clause in both the Do and the Loop statements because doing so causes an error when you try to run your program.