Post on 20-Sep-2015
description
Information Technology, Engineering Faculty
UNIM
Sequence and Selection Structure
Academic Year: 2014-2015
UNIM
(1th Week)
Control Structures (Part 1)
Programming Control Structure
You can write any program by using a combination of three
control structures:
(1) sequence
(2) selection
(3) repetition (a.k.a. iteration or looping)
These three structures are the building blocks of all program
s; they form the foundation of structured programming.
Sequnces Structure (1)
Definition
The sequence control structure is the simplest of the three str
uctures.
It is a program segment where statements are executed in
sequence, one after the other.
Sequnces Structure (2)
Definition
simply series of statements, one after the other, such as th
e following set of statements to calculate net pay:
GrossPay = HoursWorked * HourlyRate
Ded401K = GrossPay * 0.055
Medicare = GrossPay * 0.0145
SocSec = GrossPay * 0.0765
TotDeductions = Ded401K + Medicare + SocSec
NetPay = GrossPay - TotDeductions
Selection Structure (1)
Definition
The selection control structure allows one set of statemen
ts to be executed if a condition is true and another set of
actions to be executed if a condition is false.
A selection structure, also called an "If-Then-Else" structur
e, is flowcharted as follows:
Selection Structure (2)
Block Diagram (1)
If Then
Else
End If
Selection Structure (3)
Block Diagram (2)
If dblTotalSales > 100000 Then
dblBonusAmount = 500
End If
Selection Structure (4)
Type of Selection Structure
1. If Statement
2. Select Case
If Statements (1)
Format
If Then
ElseIf Then
. . .
ElseIf Then
Else
End If
If Statements (2)
Example
If strShiftCode = "1" Then
sngShiftRate = sngHourlyRate
ElseIf strShiftCode = "2" Then
sngShiftRate = sngHourlyRate * 1.1
ElseIf strShiftCode = "3" Then
sngShiftRate = sngHourlyRate * 1.15
Else
MsgBox("Shift code error")
End If
Select Case (1)
Format
Select Case
Case
Case
Case Else
End Select
Select Case (2)
Example
Select Case strShiftCode
Case "1"
sngShiftRate = sngHourlyRate
Case "2"
sngShiftRate = sngHourlyRate * 1.1
Case "3"
sngShiftRate = sngHourlyRate * 1.5
Case Else
MsgBox("Shift Code Error")
End Select
Finish!