Download presentation
Presentation is loading. Please wait.
Published byEdmund Adams Modified over 9 years ago
1
`
3
Data Types Integer - This data type is used to represent integer. Float - This data type is used to represent floating point number. Double - This data type is used to represent double precision floating point number. Char - This data type is used to represent a single character. Boolean - This data type is used to represent boolean value. It can take one of two values: True or False. String is not a basic data type but may be implemented as an array of characters.
4
Variables A variable is the content of a memory location that stores a certain value. A variable is identified or denoted by a variable name. The syntax for declaring variable name: –Dim variable_name As data_type –Dim firstNumber As Integer type identifier Dimension
5
Rules for defining Variable Name must begin with an alphabetic character or underscore E.g.: _num may contain only letters, digits and underscore (no special characters) E.g.: myNum1 case sensitive E.g.: NUM1 differs from num1 can not use keywords as identifiers E.g.: Dim, If, While, use meaningful variable names
6
Declaring Variable Dim a As Integer This declares a variable name a of type integer. If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma. For instance: –Dim x, y, z As Integer –This declares 3 variables x, y and z all of data type integer.
7
Key Terms Reserved Words Words that has specific roles in the context in which it occurs, which cannot be used for other purposes. Example: IF – THEN – ELSE DO – WHILE - LOOP Example: Dim, “”, := Keyword A symbol in programming language that has a special meaning for the compiler or interpreter.
9
Constants Like variables, constants are data storage locations. Unlike variables, and as the name implies, constants don't change. You must initialize a constant when you create it, and you cannot assign a new value later. Two types of constants: literal and symbolic.
10
Literal Constants A literal constant is a value typed directly into your program wherever it is needed. For example –Dim myAge As Integer = 39 –myAge is a variable of type integer –39 is a literal constant. You can't assign a value to 39, and its value can't be changed.
11
Literal Constants If your program has one integer variable named students and another named classes, you could compute how many students you have, given a known number of classes, if you knew there were 15 students per class: students = classes * 15; 15 is a literal constant
12
Symbolic Constants A symbolic constant is a constant that is represented by a name, just as a variable is represented. Your code would be easier to read, and easier to maintain, if you substituted a symbolic constant for this value: students = classes * studentsPerClass If you later decided to change the number of students in each class, you could do so where you define the constant studentsPerClass without having to make a change every place you used that value.
14
String A sequence of characters is often referred to as a character “string”. Example: 'Declaring variables Dim firstName, lastName, fullName As String 'Assign the value for each variables firstName = "James" lastName = "Bond" 'combine firstName with lastName fullName = firstName & lastName 'display the output MsgBox(fullName)
15
String A string value must inside the double-quote (“ “). Dim firstName As String = “James” To combine two Strings, we have to use ampersand symbol (&). fullName = firstName & lastName
17
Mathematical Operators There are five mathematical operators: –addition (+) –subtraction (-) –multiplication (*) –division (/) –modulus (%) –integer division (\)
18
Relational Operators The relational operators are used to determine whether two numbers are equal, or if one is greater or less than the other. Every relational statement evaluates to either 1 (TRUE) or 0 (FALSE). NameOperatorSampeEvaluates Equals==100 == 50;False 50 == 50;True Not Equals!=100 != 50;True 50 != 50;False Greater Than>100 > 50;True 50>50;False Greater Than or Equals >=100 >= 50;True 50 >= 50;True Less Than<100 < 50;False 50 < 50;False Less Than or Equals <=100 <=50;False 50 <= 50;True
20
Comments When you are writing a program: –always clear –self-evident what you are trying to do. Comments are simply text that is ignored by the compiler, but that may inform the reader of what you are doing at any particular point in your program. VB comments come in: –the single-quote (‘) comment, which tells the compiler to ignore everything that follows this comment, until the end of the line.
21
Comments (cont.) As a general rule, the overall program should have comments at the beginning, telling you what the program does. Each function should also have comments explaining what the function does and what values it returns. Finally, any statement in your program that is obscure or less than obvious should be commented as well.
22
Comments (cont.) 'Declaring variables Dim firstNumber, secondNumber, total As Integer 'Assign the value for each variables firstNumber = 10 secondNumber = 13 'calculate the total total = firstNumber * secondNumber 'display the output MsgBox(total)
23
Comments (cont.) DO add comments to your code. DO keep comments up-to-date. DO use comments to tell what a section of code does. DON'T use comments for self-explanatory code.
24
EXPRESSION
25
Anything that evaluates to a value is an expression An expression is said to return a value. Thus, 3+2; returns the value 5 and so is an expression. All expressions are statements. x = a + b; Not only adds a and b and assigns the result to x, but returns the value of that assignment (the value of x) as well. Thus, this statement is also an expression. Because it is an expression, it can be on the right side of an assignment operator:
26
x = a + b This line is evaluated in the following order: –Add a to b. –Assign the result of the expression a + b to x. –If a, b, and x are all integers, and if a has the value 2 and b has the value 5, x will be assigned the value 7.
27
CONTROL STRUCTURES
28
A program is usually not limited to a linear sequence of instructions. During its process it may sequence, repeat code or take decisions. For that purpose, VB provides control structures that serve to specify what has to be done by our program, when and under which circumstances. If and Else Case Statements Do While loop Repeat Until loop For loop
29
Normally, your program flows along line by line in the order in which it appears in your source code. The if statement enables you to test for a condition (such as whether two variables are equal) and branch to different parts of your code, depending on the result. If (expression) statement The expression in the parentheses can be any expression at all, but it usually contains one of the relational expressions. If the expressionhas the value 0, it is considered false, and the statement is skipped. If it has any nonzero value, it is considered true, and the statement is executed
30
Dim firstNumber, secondNumber As Integer firstNumber = 10 secondNumber = 7 If (firstNumber > secondNumber) Then MsgBox("First is greater than Second") End If
31
Often your program will want to take one branch if your condition is true, another if it is false. The keyword else can make for far more readable code: If (expression) statement Else statement
32
Dim firstNumber, secondNumber As Integer firstNumber = 10 secondNumber = 7 If (firstNumber > secondNumber) Then MsgBox("First is greater than Second") Else MsgBox("Second is greater than First") End If
33
If (expression) Then statement Else statement End If Else If (expression) Then statement Else statement End If
34
To ask more than one relational question at a time OperatorExample ANDExpression1 And expression 2 ORExpression1 Or expression2
35
CASE STATEMENT
36
Case Statement Its objective is to check several possible constant values for an expression. Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions.
37
Case Statement The syntax for the case statement is as follows: Select Case (expression) Case constant1 group of statements 1 Case constant2 group of statements 2... Case Else group of statements End Select
38
Case Statement (Example) Dim num As Integer Dim msg As String num = Val(txtDisplay.Text) Select Case num Case 1 To 50 msg = "Pass“ Case 71, 76, 79 To 100 msg = "High" Case Is > 101 msg = "Higher“ Case Else msg = "Invalid“ End Select MsgBox(msg)
39
Case Statement (Example) Both of the following code fragments have the same behavior: Case exampleif-else equivalent Select Case name Case "Adam" MsgBox(“Position is MANAGER") Case "Bob” MsgBox(“Position is SUPERVISOR") Case "Charlie” MsgBox(“Position is ACCOUNTANT") Case "Eve" MsgBox(“Position is SECRETARY") Case Else MsgBox("No data found") End Select If name = "Adam" Then MsgBox(“Position is MANAGER") ElseIf name = "Bob" Then MsgBox(“Position is SUPERVISOR") ElseIf name = "Charlie" Then MsgBox(“Position is ACCOUNTANT") ElseIf name = "Eve" Then MsgBox(“Position is SECRETARY") Else MsgBox("No data found") End If
40
Repetition Statement A repetition statement can repeat actions, depending on the value of a condition (which can be either truer or false) Types of repetition statements: Do While…Loop Do…Loop While Do Until…Loop Do…Loop Until
41
DO WHILE…LOOP & DO LOOP…WHILE
42
Do While..Loop & Do..Loop While Do While..Loop & Do..Loop While – a control statement that executes a set of body statements while its loop-continuation condition is True Loop-continuation condition – the condition used in a repetition statement that enables repetition to continue while the condition is True and that causes repetition to terminate when the condition becomes False Syntax: Do While loop-continuation condition block of statements Loop Do While loop-continuation condition block of statements Loop Do block of statements Loop While loop-continuation condition
43
Do While..Loop & Do..Loop While Example: Do While product <= 50 product += 3 Loop Do While product <= 50 product += 3 Loop Do product +=3 Loop While product <= 50 In the Do While..Loop statement, the loop-continuation condition is tested at the beginning of the loop, before the body of the loop is performed. In Do..Loop While statement performs the loop-continuation condition after the loop body if performed. Therefore, in a Do..Loop While statement, the loop body always executes at least once. A Do While..Loop executes only if its loop-continuation condition evaluates to true.
44
Do While..Loop & Do..Loop While Example: Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do MsgBox("The number is " & number) number = number + 1 Loop While number < 4 Dim number As Integer Do MsgBox("The number is " & number) number = number + 1 Loop While number < 4
45
Do While..Loop & Do..Loop While Example: Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do While number < 4 number = number + 1 Loop MsgBox("The number is " & number) Dim number As Integer Do While number < 4 number = number + 1 Loop MsgBox("The number is " & number) The output of the code above will be different with the code below
46
DO UNTIL…LOOP & DO LOOP…UNTIL
47
Do Until..Loop & Do..Loop Until Do Until..Loop & Do..Loop Until– a control statement that executes a set of body statements while its loop-termination condition becomes True. Loop-termination condition – the condition used in a repetition statement that enables repetition to continue while the condition is False and that causes repetition to terminate when the condition becomes True Syntax: Do Until loop-termination condition block of statements Loop Do Until loop-termination condition block of statements Loop Do block of statements Loop Until loop-termination condition Do block of statements Loop Until loop-termination condition
48
Do Until..Loop & Do..Loop Until Example: Do Until product >= 50 product += 3 Loop Do Until product >= 50 product += 3 Loop Do product +=3 Loop Until product >= 50 Do product +=3 Loop Until product >= 50 In the Do Until..Loop statement, the loop-termination condition is tested at the beginning of the loop, before the body of the loop is performed. In Do..Loop Until statement performs the loop-termination condition after the loop body if performed. Therefore, in a Do..Loop Until statement, the loop body always executes at least once. A Do Until..Loop executes only if its loop-termination condition evaluates to true.
49
Do While..Loop & Do Until..Loop Do While..Loop Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number) number = number + 1 Loop Do Until…Loop
50
FOR…NEXT
51
Counter-controlled Repetition Four essential elements of counter-controlled repetition: 1.The name of a control variable (or loop counter) that is used to determine whether the loop continues to iterate 2.The initial value of the control variable 3.The increment (or decrement) by which the control variable is modified during each iteration of the loop (that is, each time the loop is performed) 4.The condition that tests for the final value of the control variable (to determine whether looping should continue)
52
For…Next Statement Syntax: For controlVariable = initialValue To finalValue Step increment statements Next Dim counter As Integer For counter = 2 To 10 Step 2 MsgBox("The number is " & counter) Next Dim counter As Integer For counter = 2 To 10 Step 2 MsgBox("The number is " & counter) Next Example Optional (depends on the program) Dim answer As Integer Dim startNumber As Integer answer = 0 For startNumber = 1 To 4 answer += startNumber MsgBox(answer) Next Dim answer As Integer Dim startNumber As Integer answer = 0 For startNumber = 1 To 4 answer += startNumber MsgBox(answer) Next answer = answer + startNumber
53
For…Next vs. Do While..Loop vs. Do Until..Loop Do While..Loop Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do While number < 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number) number = number + 1 Loop Dim number As Integer Do Until number > 4 MsgBox("The number is " & number) number = number + 1 Loop Do Until…Loop Dim number As Integer For number = 1 To 4 MsgBox("The number is " & number) Next For..Next
54
ARRAY
55
An array is a group of memory locations all containing data items of the same name and type. Array names follow the same conventions that apply to other identifiers.
56
Dim myNumber(4) As Integer name of array index type An array starts counting at zero, and the first position in your array will be zero An index must be either zero, a positive integer or an integer expression that yields a positive result
57
Dim myNumbers(4) As Integer myNumbers(0) = 1 myNumbers(1) = 2 myNumbers(2) = 3 myNumbers(3) = 4 myNumbers(4) = 5 value
58
Dim myNumbers(4) As Integer myNumbers(0) = 1 myNumbers(1) = 2 myNumbers(2) = 3 myNumbers(3) = 4 myNumbers(4) = 5 MsgBox("First Number is: " & myNumbers(0)) MsgBox("Second Number is: " & myNumbers(1)) MsgBox("Third Number is: " & myNumbers(2)) MsgBox("Fourth Number is: " & myNumbers(3)) MsgBox("Fifth Number is: " & myNumbers(4))
59
Dim MyNumbers(4) As Integer Dim i As Integer MyNumbers(0) = 10 MyNumbers(1) = 20 MyNumbers(2) = 30 MyNumbers(3) = 40 MyNumbers(4) = 50 For i = 0 To 4 ListBox1.Items.Add(MyNumbers(i)) Next
60
Dim MyText(4) As String Dim i As Integer MyText(0) = "This" MyText(1) = "is" MyText(2) = "a" MyText(3) = "String" MyText(4) = "Array" For i = 0 To 4 ListBox1.Items.Add(MyText(i)) Next
61
Dim numbers() As Integer Dim startAt As Integer Dim endAt As Integer Dim times As Integer Dim storeAnswer As Integer Dim i As Integer times = Val(TextBox1.Text) startAt = Val(TextBox2.Text) endAt = Val(TextBox3.Text) ReDim numbers(endAt) For i = startAt To endAt storeAnswer = i * times numbers(i) = storeAnswer ListBox1.Items.Add(times & " times " & i & " = " & numbers(i)) Next Assigning values from TextBox Assigning values from TextBox Non-Fixed size Array or Dynamic Array Non-Fixed size Array or Dynamic Array ReDim = reset an array then specify the new values ReDim = reset an array then specify the new values
62
RANDOM
63
Dim objRandom As Random = New Random() Dim intRandom As Integer = objRandom.Next() The first statement declares objRandom as a reference of type of Random and assigns it a Random object. A reference is a variable to which you assign an object. Recall that keyword New creates a new structure value or a new instance of an object in memory. The second statement declares Integer variable intRandom. It then assigns to it the value returned by calling Random ’s Next method on objRandom using the dot operator
64
Method CallResulting Range objRandom.Next()0 to one less than Int32.MaxValue objRandom.Next(30)0 to 29 10 + objRandom.Next(10)10 to 19 objRandom.Next(10,20)10 to 19 objRandom.Next(5,100)5 to 99 objRandom.NextDouble()0.0 to less than 1.0 8 * objRandom.NextDouble()0.0 to less than 8.0
65
Dim objRandom As Random = New Random() Dim intRandom As Integer = objRandom.Next(3) TextBox1.Text = intRandom Dim objRandom As Random = New Random() Dim intRandom As Integer = objRandom.Next(3) Dim name As String() = New String() {"Hafiz", "Syazwan", "Irfan"} TextBox1.Text = name(intRandom) Dim objRandom As Random = New Random() Dim intRandom As Integer = objRandom.Next(3) Dim name As String() = New String() {"Hafiz", "Syazwan", "Irfan"} TextBox1.Text = name(intRandom) Dim objRandom As Random = New Random() Dim intRandom As Integer = objRandom.Next(3) Dim number As Integer() = New Integer() {1, 5, 10} TextBox1.Text = number(intRandom) Dim objRandom As Random = New Random() Dim intRandom As Integer = objRandom.Next(3) Dim number As Integer() = New Integer() {1, 5, 10} TextBox1.Text = number(intRandom) Example 1: Example 3: Example 2:
66
QUESTIONS?
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.