440 likes | 2.05k Views
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, …
E N D
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, … 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
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
5. A computer can compare two pieces of information and select one of two alternative actions if (condition) action1 else action 2
6. A computer can repeat a group of actions while (condition) action for (number of times) action
The Structure Theorem It is possible to write any computer program by using only three basic control structures: • sequence • selection • repetition
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
Selection • An instruction that decides which of two possible sequences is executed • The decision is based on a single true/false condition • Examples: • Reciprocals
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.
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
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" }
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
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
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 }
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
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
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
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( )
Homework 2 • Solve the following from the book 1.2: 9, 12