Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.

Similar presentations


Presentation on theme: "Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals."— Presentation transcript:

1 Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

2 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)

3 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

4 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

5 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.

6 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, a@test, 3589a, dim, decimal

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

8 Understanding Data Types Visual Basic.NET Data Type Values Stored Example BooleanTrue/FalseTrue Char Single Character “A” Date Dates and Times 12/21/1970 02:00 PM Decimal Decimal or Whole 19.95D, 7.281 Double Decimal Numbers 1.23E-10 Integer Whole Numbers 6753 Long 73428976324 Single Decimal Numbers.0000123 Short Whole Numbers 32123 StringCharacters“HELLO”

9 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: -3.402823E38 to -1.401298E-45 +ve no:1.401298E-45 to 3.402823E38 Double8 -ve no: -1.79769313486232E308 to -4.94065645841247E-324 +ve no: 4.94065645841247E-324 to 1.79769313486232E308 Decimal16 Up to 28 digits to the left or right of.

10 Using Math Operations OperationOperator Addition+ Subtraction- Multiplication* Division/ Integer division \ ModulusMod Exponentiation^

11 Addition and Subtraction Using the Addition Operator Using the Addition Operator result = number1 + number2[+ number3] result = number1 + number2[+ number3] result = 1 + 2 + 3 result = 1 + 2 + 3 Using the Subtraction Operator Using the Subtraction Operator result = number1 - number2[- number3] result = number1 - number2[- number3] result = 15 - 6 - 3 result = 15 - 6 - 3

12 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 1.333333 result = number1 \ number2 [\ number3] result = 4\3 ‘will return 1

13 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

14 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)

15 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.

16 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

17 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

18 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)

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

20 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

21 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

22 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

23 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

24 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.

25 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.

26 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

27 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

28 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

29 Working with Loops Using Do While Statements Using Do While Statements Dim Counter As Integer = 0 Do While Counter < 10 Counter += 1 Counter += 1Loop

30 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

31 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.


Download ppt "Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals."

Similar presentations


Ads by Google