Introducing Algorithms, Pseudocode - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Introducing Algorithms, Pseudocode

Description:

Tutorial 7 Introducing Algorithms, Pseudocode & Program Control Algorithm Procedure or formula to solve a problem The word derives from the name of the mathematician ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 18
Provided by: Yun46
Category:

less

Transcript and Presenter's Notes

Title: Introducing Algorithms, Pseudocode


1
Tutorial 7
  • Introducing Algorithms, Pseudocode Program
    Control

2
Algorithm
  • 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
3
Example 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
4
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

5
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

6
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

7
Basic Structure
  • If condition Then
  • code(s) to be executed
  • End If
  • If intGrade gt 60 Then
  • lblCourseResult.Text Passed
  • End If

Continue ?
8
If condition Then             instruction(s) End If If sngTotalAmount gt 500 Then             sngShippingCharge 0 End If
                                                                                                                                                                                                                                                                                                   
9
Comparison 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
10
Logical 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)
11
If 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 ?
12
If condition Then             instruction(s) 1 Else             instruction(s) 2 End If If sglTotalAmount gt 500 Then       sglShippingCharge 0 Else       sglShippingCharge 10 End If
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
13
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

14
Nested 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
15
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 gt 500 Then lblShipping.Text
0 ElseIf sglTotalAmount gt 300 Then
lblShipping.Text 10 Else lblShipping.Text
15 End If
16
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 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)
Write a Comment
User Comments (0)
About PowerShow.com