Chapter 3 Program Design and Coding Part 1
Chapter 3 Program Design and Coding Part 1
Chapter 3 Program Design and Coding Part 1
VISUAL PROGRAMMING
CHAPTER 3 PROGRAM DESIGN AND CODING
Course Learning Outcome
CLO 1: Explain the different elements of a visual
programming language, architecture, IDE, object-oriented
programming techniques and the concepts as building blocks
to develop correct, coherent programs. (C4, PLO1)
CLO 2: Implement a visual (graphical) user interface and
syntax rules in Visual Basic programs using an integrated
development environment (IDE). (C3, P4, PLO1, PLO2)
CLO 3: Develop a windows application in a team that
separate a program into logical sections: the user interface;
the “business logic” through the use of programming
frameworks, code resources and secure coding techniques.
(P4, A3, PLO2, PLO4, PLO9)
Lesson Learning Outcome
Operators
Control Structure:
a. Selection Structure
b. Looping Structure
Control Arrays.
dblTaxRate = 0.07875
Default Values for Data Types
Data type Default (Initial) value
Syntax:
Private Const strTitle As String = “My Application”
Looks like a normal declaration except:
Const used instead of Dim
An initialization value is required
Operator Use
^ Exponentiation
- Negation
* Multiplication
/ Division
\ Integer Division
+ Addition
- Subtraction
Common Arithmetic Operators
Examples of use:
decTotal = decPrice + decTax
decNetPrice = decPrice - decDiscount
dblArea = dblLength * dblWidth
sngAverage = sngTotal / intItems
dblCube = dblSide ^ 3
Special Integer Division Operator
Modulus (Mod)
Operator Use
+ String Concatenation
& String Concatenation
Example:
If intCnt <= intMax Then
intTotal += intCnt ' Same as intTotal = intTotal + intCnt
intCnt += 1 ' Same as intCnt = intCnt + 1
Else
intTotal += intCnt
intCnt -= 1 ' Same as intCnt = intCnt - 1
End If
Logical Operators
Operator Use
Not Negation
And Conjunction
Or Disjunction
NOT Operators
NOT
The Not operator returns True when the condition is False. Otherwise, it
returns False.
Example:
If Not (1 = 2) Then
MessageBox.Show("(1 = 2) is False. So Not False is
True")
End If
Truth Table
If Condition is: Not Condition will be:
True False
False True
AND Operators
AND
The And operator returns True when the conditions of the left and right are True.
Otherwise, it returns False. Both conditions are evaluated before the result is returned.
Example:
If (1 = 1) And (2 = 2) Then
MessageBox.Show("(1 = 1) is True. (2 = 2) is True.
So True And True is True")
End If
Truth Table :
Condition1 Condition2 Condition1 And Condition2
True True True
True False False
False True False
False False False
OR Operators
OR
The Or operator returns True when the condition on either side is True. Otherwise, it
returns False. Both conditions are evaluated before the result is returned.
Example:
If (1 = 1) Or (2 = 2) Then
MessageBox.Show("(1 = 1) is True. (2 = 2) is True.
So True And True is True")
End If
Truth Table :
Condition1 Condition2 Condition1 And Condition2
True True True
True False True
False True True
False False False
Combined Assignment Operators
Often need to change the value in a variable and
assign the result back to that variable
For example: var = var - 5
Subtracts 5 from the value stored in var
End If
If...Then...Else constructions
statements in a loop
structure until a condition
is satisfied with a
specified number of times.
Repetition / Looping Control Structures
Part Description
counter Required in the For statement. Numeric variable. The control variable for the
loop.
datatype Required if counter is not already declared. Data type of counter.
start Required. Numeric expression. The initial value of counter.
end Required. Numeric expression. The final value of counter.
step Optional. Numeric expression. The amount by which counter is
incremented/decremented each time through the loop. It can be negative or
positive value.
statements Optional. One or more statements between For and Next that run the specified
number of times.
Continue For Optional. Transfers control to the next loop iteration.
Exit For Optional. Transfers control out of the For loop immediately.
Next Required. Terminates the definition of the For loop.
For…Next Loop Statement
Data type
counter
Output:
Dim x As Integer = 0
1
Start value
End value 2
For x = 1 To 4
3
lbNumbers.Items.Add(
4
x)
Next x statement
Nested For…Next Statement
Output: 10
9
Dim x as Integer 8
7
For x = 10 To 1 Step -1 6
5
ListBox1.Items.Add (x) 4
3
2
Next x ‘x will be decrease by 1 1
Do While…Loop Statement
Term Definition
Do Required. Starts the definition of the Do loop.
While Required unless Until is used. Repeat the loop
until condition is False.
Until Required unless While is used. Repeat the loop
until condition is True.
condition Optional. Boolean expression. If condition is Nothing, Visual
Basic treats it as False.
statements Optional. One or more statements that are repeated while, or
until, condition is True.
Exit Do Optional. Transfers control out of the Do loop.
Loop Required. Terminates the definition of the Do loop.
Do While…Loop Statement
Ali (0)
Mamat (1)
Minah (2)
Lim (3)
Muthu (4)
Arrays Declaration
General Form
Private Arrayname(UpperSubscript) As Datatype
Example:
Private category(5) As String
Dim index() As Integer = { 1, 5, 12, 20, 24}
Dim index As Integer() = { 1, 5, 12, 20, 24}
One-Dimensional Arrays
A one-dimensional array is an array that only has one
dimension.
It looks like a row of people at a super market.
The following figure illustrates an example of a typical
one-dimensional array:
Arr
Syntax:
Dim arrayName() as dataType
arrayName = New dataType(){values}
OR
Dim arrayName() as dataType = {values}
OR
Dim arrayName(n) As dataType
arrayName(index) = value
Example :
Dim Test() as Integer 'declaring a array Test Test=New
Integer(){1,3,5,7,9} 'initialize values to array Test
OR
Dim Test() as Integer = {1,3,5} 'declare and initialize values
Examples : Arrays Declaration
Index / subscript
Syntax
Private Sub ProcedureName( parameters )
//statement(s)
End Sub
Example
Public Sub cmdCalculate( )
Dim intNum1, intNum2, intSum As Integer
intNum1 = 2
intNum2 = 3
intSum = intNum1 + intNum2
lblSum.Text = Str(intSum)
End Sub
Function Procedures
Example
Public Function AddNums(numA As Single, numB As
Single) As Single
Dim theTotalValue As Single
theTotalValue = numA + numB
Return theTotalValue
End Function
Passing Arguments by Value and by
Reference
Passing Arguments by Value and by
Reference