0% found this document useful (0 votes)
5 views12 pages

Wk03 PROG8240 LecNotes

Uploaded by

Bhagyesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Wk03 PROG8240 LecNotes

Uploaded by

Bhagyesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PROG8240 Lecture notes – week #03

Structured Programming

Today’s Highlights
• Data Hierarchy
• Variables and Constants
• Parentheses in Operator Precedence
• Relational and Logical Operators
• Program Development and Programming Tools
• Control Structures for Decision Making
• If…Then…Else blocks
• Input Validation

Structured Programming 7

Data Hierarchy
• Data items processed by
computers form a data
hierarchy that becomes
larger and more complex in
structure as we progress
from the simplest data items
(called “bits”) to richer data
items, such as characters,
fields, and so on.

Structured Programming 8

1
PROG8240 Lecture notes – week #03
Structured Programming

Data Hierarchy (cont’d)


• Bits
• The smallest data item in a computer can assume
the value 0 or the value 1.
• Such a data item is called a bit (short for “binary
digit” – a digit that can assume either of two
values).
• Bits are widely used, such as checking (or
examining) a bit’s value.
• True, or 1, or ON
• False, or 0, or OFF
• 8 bits form one byte.
• 2 bytes form one word.
• 2 words form one double-word.
• Zero-based numbering: items are usually counted
beginning with 0 instead of 1.
Structured Programming 9

Data Hierarchy (cont’d)


• Characters
• We prefer to work with decimal digits (0 – 9),
uppercase letters (A – Z), lowercase letters (a – z),
and special symbols, (e.g., $, %, &, +, –, *, /, ", :, ?).
• Digits, letters, and special symbols are known as
characters.
• The Unicode character set contains characters for
many of the world’s language.
• ASCII (American Standard Code for Information
Interchange) character set is the popular subset of
Unicode that represents uppercase and lowercase letters
in the English alphabet, digits, and some common special
characters.
• One ASCII character takes 1-2 bytes.

Structured Programming 10

2
PROG8240 Lecture notes – week #03
Structured Programming

Data Hierarchy (cont’d)


• ASCII Character Set

• A – Z: 65 – 90; a – z: 97 - 122
Structured Programming 11

Data Hierarchy (cont’d)


• Big Data
• The amount of data being produced worldwide is
enormous and growing explosively.

Structured Programming 12

3
PROG8240 Lecture notes – week #03
Structured Programming

Variables and Constants


• Numeric Variable:
• A numeric variable is a name to which a number
can be assigned.
• A variable must be declared before it can be used.
• How to declare a variable
Dim varName As DataType
• Variable names
• Up to 40 characters (usually less than 10)
• No spaces allowed, i.e., only be one word.
• First character must be a letter.
• Should be descriptive for better readability.

Structured Programming 13

Variables and Constants (cont’d)


• String Variable:
• A string variable is a name to which a string can be
assigned.
• How to declare a string variable
Dim varName As String

• Initialization
• To specify an initial string value

Dim myString As String = “Programming Course”

Structured Programming 14

4
PROG8240 Lecture notes – week #03
Structured Programming

Variables and Constants (cont’d)


• Commonly-used Data Types
Data Type Memory Allocation Value Range
Boolean 1 bit 1 or 0, True or False
Byte 1 byte (8 bits) 0 – 255
Char 2 bytes 0 – 65535
String 1 or more characters 0 to approx. 2 billion
Unicode characters
Integer 4 bytes whole numbers

Long 8 bytes large whole numbers

Single 4 bytes decimal number


Double 8 bytes large decimal numbers
Structured Programming 15

Variables and Constants (cont’d)


• Scope of Variables
• The scope of a variable is the portion of the program that
can refer to it.
• Variables declared inside an event procedure are said to be
local to the procedure and to have procedure scope. They
are only available to the event procedure in which they are
declared.
• Variables declared outside an event procedure are said to
be global and have class scope, and are available to every
event procedure. Public Class Form1
Dim varGlobal As Integer
Private Sub Button1_Click …
Dim varLocal As Integer
End Sub
Structured Programming End Class 16

5
PROG8240 Lecture notes – week #03
Structured Programming

Variables and Constants (cont’d)


• Numeric/String Constants
• Constants cannot be changed by other statement, once
declared.
• How to declare a constant
Const CONSTANT_NAME As DataType = value
• Examples:
Const COURSE_NUMBER As Integer = 1120

Const PI As Decimal = 3.1415926

Const COURSE_NAME As String = “Visual Basic”

Structured Programming 17

Parentheses in Operator Precedence


• Operators in expressions contained within a pair of
parentheses are evaluated before those that are
outside the parentheses.
• Parentheses can be used to group expressions and
change the order of evaluation to occur in any
sequence you desire.
• With nested parentheses, the operators contained in
the innermost pair of parentheses are applied first.
• Redundant Parentheses
• It’s acceptable to place unnecessary parentheses in an
expression to make the expression clearer.
𝐝𝐢𝐚𝐦𝐞𝐭𝐞𝐫 𝐝𝐢𝐚𝐦𝐞𝐭𝐞𝐫
𝐯𝐨𝐥𝐮𝐦𝐞 𝐏𝐈 𝐥𝐞𝐧𝐠𝐭𝐡 𝐯𝐨𝐥𝐮𝐦𝐞 𝐏𝐈 𝐥𝐞𝐧𝐠𝐭𝐡
2 2

Structured Programming 18

6
PROG8240 Lecture notes – week #03
Structured Programming

Equality and Relational Operators


• Equality and Relational operators are binary – they
require an operand on both sides of the operator.
• Value of a relational expression will always be True or
False.

Structured Programming 19

Logical Operators
• An expression that evaluates to either True or False is
said to have Boolean data type.
• Used with Boolean-valued expressions:
• And – will yield a True if and only if both expressions are
True.
• Or – will yield a True if one of both expressions is True.
• Not – makes a False expression True and vice versa.
• Truth Tables of And / Or / Not:
AND OR NOT
A B X A B X A X
0 0 0 0 0
0 1 0 1 1
1 0 1 0
1 1 1 1
Structured Programming 20

7
PROG8240 Lecture notes – week #03
Structured Programming

Program Development & Programming Tools


• Algorithm
• It is to find a logical sequence of steps that solve the
problem.

• Tools are used to convert algorithms into computer


programs:
• Flowchart - Graphically depicts the logical steps to carry out
a task and shows how the steps relate to each other.

• Pseudocode - Uses English-like phrases with some Visual


Basic terms to outline the program.

Structured Programming 21

Control Structures
• Three most important structures
that control the program flow:
• Sequential Structure: the
statements are executed
sequentially.

Structured Programming 22

8
PROG8240 Lecture notes – week #03
Structured Programming

Control Structures (cont’d)


• Three most important structures (cont’d):
• Selection Structure: the statements are selected or ignored
based on certain condition(s).
• Single selection: If…Then
• Double selection: If…Then…Else
• Multiple selection: Select…Case

Structured Programming 23

Control Structures (cont’d)


• Three most important structures (cont’d):
• Repetition Structure: the statements are executed
repeatedly based on the value of a condition.
• Looping structures

Structured Programming 24

9
PROG8240 Lecture notes – week #03
Structured Programming

Control Structure: If…Then…Else Block


• Syntax of
If…Then…Else Block
• The If…Then
statement allows a
program to make a
decision based on the
truth or falsity of
expressions.
• The expression in an If…Then statement is called a
condition.
• Conditions in If…Then statements can be formed by
using the equality operators and relational operators
(also called comparison operators)
• The relational and equality operators all have the same level
of precedence and associate from left to right.
Structured Programming 25

If…Then…Else Block (Cont’d)


• If the condition is met (that is, the condition is true),
the statement in the If…Then statement’s body
executes.
• If the condition is not met (that is, the condition is
false), the body statement does not execute.

If condition Then
action 1
End If Regardless of whether

Statement 2 the condition in the

Statement 3 If statement is true or


false, these statements
will be executed
Structured Programming 26

10
PROG8240 Lecture notes – week #03
Structured Programming

If…Then…Else Block (Cont’d)


• Pseudocode and Flowchart for an If Block

If condition Then
action 1
Else
action 2
End If

Structured Programming 27

If…Then…Else Block (Cont’d)


• Nested If Blocks
• When one If block is contained inside another If block,
the structure is referred to as nested If blocks.
If condition 1 Then
action 1
Else
If condition 2 Then
action 2
Else
action 3
End If
End If
Structured Programming 28

11
PROG8240 Lecture notes – week #03
Structured Programming

If…Then…Else Block (Cont’d)


• ElseIf Clause

If condition 1 Then
action 1
ElseIf condition 2 Then
action 2
ElseIf condition 3 Then
action 3
Else
action 4
End If

Structured Programming 29

Input Validation
• The statement
If (IsNumeric(txtBox.Text) = True) Then
is commonly used to validate that input is
numeric. It can be condensed to
If IsNumeric(txtBox.Text) Then

Structured Programming 30

12

You might also like