Tutorial 1 - Seattle Central College

advertisement
Tutorial 7
Introducing Algorithms,
Pseudocode & Program
Control
Algorithm



Procedure or formula to solve a problem
The word derives from the name of the
mathematician, Mohammed ibn-Musa alKhwarizmi (780 – 850 A.D.) from Baghdad. His
work is the likely source for the word algebra
as well
A problem can be divided into three
components: Input, Process, & Output
Source: http://searchvb.techtarget.com/sDefinition/0,,sid8_gci211545,00.html
Example: A program is required to read three numbers, add them together,
and print the total.
Input
Num1
Num2
Num3
Process
Output
Read three numbers
Add numbers together
Print Total
Total
Example: Read the max and min temperatures, calculate the average, and
print the average.
Input
max_temp
min_temp
Process
Output
Prompt for temperatures
Get max and min temperatures
Calculate average temperature
Display average temperature
avg_temp
Pseudocode
English-like & user-friendly informal code
 Tool to detect any logic error prior to
actual coding
 Simple to translate it into real
programming code
 Used for developing algorithm

Example:
Pseudocode:
Set the balance to be 0
Get the deposit amount from the input box
Add the deposit amount to the balance
 VB .Net:
decBalance = 0
decDeposit = Val (txtDeposit.Text)
decBalance = decBalance + decDeposit

If … Then Selection Control
Executes its instruction(s) when the
condition is met
 Depends on the result of a condition being
true or false.
 If the condition is false, then the entire If
statement is skipped.
 Three key words: If, Then, and End If

Basic Structure
If [condition] Then
code(s) to be executed
End If
If intGrade >= 60 Then
lblCourseResult.Text = “Passed”
End If
Continue 
If [condition] Then
[instruction(s)]
End If
If sngTotalAmount > 500 Then
sngShippingCharge = 0
End If
Comparison Operators
Comparison
Operator
=
<>
>
<
>=
<=
Meaning
Example
Equal to
1 = 1 is true, 1=2 is false
Not equal to
1 <> 1 is false, 1 <> 2 is true
Greater than
1 > 1 is false, 1 > 2 is false, 2 > 1 is true
Less than
1 < 1 is false, 1< 2 is true, 2 < 1 is false
Greater than or equal to
1 >= 1 is true, 1 >= 2 is false, 2 >= 1 is true
Less than or equal to
1 <= 1 is true, 1 <= 2 is true, 2 <= 1 is false
Logical Operators
Logical
Operator
And
(AndAlso)
Or
(OrElse)
Not
Xor
Meaning
Example
Only if both conditions are true, then the
result is true.
If [condition1] And [condition 2]
If at least one condition is true, then the
result is true
If [condition1] Or [condition 2]
Negation of the condition. If the
condition is true, the result is false. If the
condition is false, then the result is true.
If Not [condition]
If one and only one of the conditions is
true, then the result is true. If all
conditions are either true or false, then the
result is false. So, if all conditions are
false, then the result is false. If all
conditions are true, then the result is also
false.
If blnStake Xor blnBurger Then
strCustomer = “Satisfied”
End If
If a customer is served with either a
stake or a burger, she is happy. But
if the customer is not served
anything or both, then she is not
happy. (too hungry or too full)
If … Then…Else Selection
A choice is made between two alternative
paths
 Depends on the result of a condition being
true or false
 Only the instruction(s) for the condition
that is evaluated to be true is executed
 Otherwise, skipped

Continue 
If [condition] Then
[instruction(s) 1]
Else
[instruction(s) 2]
End If
If sglTotalAmount > 500 Then
sglShippingCharge = 0
Else
sglShippingCharge = 10
End If
Nested If Statement
 A choice
is made among many
alternative paths
 Only the instruction(s) for the
condition that is first evaluated to be
true is executed  the rest skipped.
 Once the selected instruction(s) are
executed, the entire If statement
terminates
Nested If Statement
If [condition] Then
code(s) to be executed
Else
If [condition] Then
code(s) to be executed
Else
code(s) to be executed
End If
End If
If sglTotalAmount > 500 Then
lblShipping.Text = “0”
Else
If sglTotalAmount > 300 Then
lblShipping.Text = “10”
Else
lblShipping.Text = “15”
End If
End If
ElseIf Statement
If [condition] Then
code(s) to be executed
ElseIf [condition] Then
code(s) to be executed
Else
code(s) to be executed
End If
If sglTotalAmount > 500 Then
lblShipping.Text = “0”
ElseIf sglTotalAmount > 300 Then
lblShipping.Text = “10”
Else
lblShipping.Text = “15”
End If
ElseIf Statement Example
If [condition 1] Then
[instruction(s) 1]
ElseIf [condition 2] Then
[instruction(s) 2]
ElseIf [condition 3] Then
[instruction(s) 3]
End If
If sglTotalAmount > 500 Then
sglShippingCharge = 0
ElseIf sglTotalAmount > 300 Then
sglShippingCharge = 10
ElseIf sglTotalAmount > 0 Then
sglShippingCharge = 15
End If
Continue 
Download