1 / 19

What is pseudocode?

What is pseudocode?. A way of writing program descriptions that is similar to programming languages but may include English descriptions and does not have a precise syntax . How to write pseudocode?. There are six basic computer operations: 1. A computer can receive information Read, Get, …

tara
Download Presentation

What is pseudocode?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What is pseudocode? A way of writing program descriptions that is similar to programming languages but may include English descriptions and does not have a precise syntax

  2. How to write pseudocode? There are six basic computer operations: 1. A computer can receive information • Read, Get, … 2. A computer can put out information • Write, Display, … 3. A computer can perform arithmetic • Use actual mathematical symbols or the words for the symbols • Add number to total • total = total + number • +, -, *, / • Calculate, Compute also used

  3. 4. A computer can assign a value to a piece of data • to give data an initial value • Initialize, Set • To assign a value as a result of some processing • ‘=’ • to keep a piece of information for later use • Save, Store

  4. 5. A computer can compare two pieces of information and select one of two alternative actions if (condition) action1 else action 2

  5. 6. A computer can repeat a group of actions while (condition) action for (number of times) action

  6. The Structure Theorem It is possible to write any computer program by using only three basic control structures: • sequence • selection • repetition

  7. Sequence Execution of one step after another. Statement 1 Statement 2 Statement 3 Example: Read three numbers Add three numbers Display total of three numbers

  8. Selection • An instruction that decides which of two possible sequences is executed • The decision is based on a single true/false condition • Examples: • Reciprocals

  9. Selection Example – Reciprocals (cont) Algorithm: input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" } Q. Give an algorithm for computing the reciprocal of a number.

  10. Selection Example – Reciprocals (cont) Algorithm: input Num if (Numis not equal 0) then { output 1/Num } else { output "infinity" } For a given value of Num, only one of these two sequences can be executed

  11. Selection -- Exercise Will the following algorithms produce the same output? Algorithm 1: Algorithm 2: input Num if (Num is not equal 0) then { output 1/Num } output "infinity" input Num if (Num is not equal 0) then { output 1/Num } else { output "infinity" }

  12. Selection– Several Conditions • What if several conditions need to be satisfied? if ( today is Wednesday and the time is 10.00am ) then { Go to CSE131111 Lecture } else { Go to Library } Solution 1

  13. Selection– Several Conditions (cont) if ( today is Wednesday ) then { if ( the time is 10.00am ) then { Go to CSE131111 Lecture } } else ...etc...etc...etc... Often called a “nested selection” Solution 2

  14. Selection– At Least One of Several Conditions • What if at least one of several conditions needs to be satisfied? if ( I feel hungry or the time is 1.00pm or my mate has his eye on my lunch ) then { Eat my lunch now }

  15. Repetition • Repeat an instruction... • ...while (or maybe until) some true or false condition occurs • Test the condition each time before repeating the instruction • Also known as iteration or loop • Example: • Algorithm for finding the summation from 1 to a given number

  16. Repetition – Example (cont) procedure Sum1_to_n(num) { count = 1 sum = 0while (count <= num){ add count to sum add 1 to count } } Ensure initial values of variables used in the conditions are set correctly Ensure the variables used in the conditions are updated in each iteration

  17. Repetition – Example (cont) procedure Sum1_to_n(num) { count = 1 sum = 0while (count <= num){ add count to sum } } • What if we don’t increment “count”? Infiniteloop

  18. NOTES • Comment • \\ • Considering a class containing data and functions, if C is an object of the class and f is a member function of the class • C. f( )

  19. Homework 2 • Solve the following from the book 1.2: 9, 12

More Related