Title: Introducing Algorithms, Pseudocode
1Tutorial 7
- Introducing Algorithms, Pseudocode Program
Control
2Algorithm
- Procedure or formula to solve a problem
- The word derives from the name of the
mathematician, Mohammed ibn-Musa al-Khwarizmi
(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
3Example A program is required to read three
numbers, add them together, and print the total.
Input Process Output
Num1 Num2 Num3 Read three numbers Add numbers together Print Total Total
Example Read the max and min temperatures,
calculate the average, and print the average.
Input Process Output
max_temp min_temp Prompt for temperatures Get max and min temperatures Calculate average temperature Display average temperature avg_temp
4Pseudocode
- 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
5Example
- 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
6If 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
7Basic Structure
- If condition Then
- code(s) to be executed
- End If
- If intGrade gt 60 Then
- lblCourseResult.Text Passed
- End If
Continue ?
8If condition Then            instruction(s) End If If sngTotalAmount gt 500 Then            sngShippingCharge 0 End If
                                                                                                                                                                                                                                                                                                 Â
9Comparison Operators
Comparison Operator Meaning Example
Equal to 1 1 is true, 12 is false
ltgt Not equal to 1 ltgt 1 is false, 1 ltgt 2 is true
gt Greater than 1 gt 1 is false, 1 gt 2 is false, 2 gt 1 is true
lt Less than 1 lt 1 is false, 1lt 2 is true, 2 lt 1 is false
gt Greater than or equal to 1 gt 1 is true, 1 gt 2 is false, 2 gt 1 is true
lt Less than or equal to 1 lt 1 is true, 1 lt 2 is true, 2 lt 1 is false
10Logical Operators
Logical Operator Meaning Example
And(AndAlso) Only if both conditions are true, then the result is true. If condition1 And condition 2
Or (OrElse) If at least one condition is true, then the result is true If condition1 Or condition 2
Not 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
Xor 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)
11If ThenElse 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 ?
12If condition Then            instruction(s) 1 Else            instruction(s) 2 End If If sglTotalAmount gt 500 Then      sglShippingCharge 0 Else      sglShippingCharge 10 End If
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Â
13Nested 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
14Nested If Statement
If sglTotalAmount gt 500 Then lblShipping.Text
0 Else If sglTotalAmount gt 300 Then
lblShipping.Text 10 Else lblShipping.Text
15 End If End If
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
15ElseIf 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 gt 500 Then lblShipping.Text
0 ElseIf sglTotalAmount gt 300 Then
lblShipping.Text 10 Else lblShipping.Text
15 End If
16ElseIf 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 gt 500 Then sglShippingCharge 0 ElseIf sglTotalAmount gt 300 Then sglShippingCharge 10 ElseIf sglTotalAmount gt 0 Then sglShippingCharge 15 End If
Continue ?
17(No Transcript)