1 / 32

Pseudocode

Pseudocode. Skill Area 305.2. Materials Prepared by Dhimas Ruswanto , BMm. Lecture Overview. Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples. Pseudocode.

valora
Download Presentation

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. Pseudocode Skill Area 305.2 Materials Prepared by DhimasRuswanto, BMm

  2. Lecture Overview • Pseudocode • Pseudocode (Sequence) • Pseudocode (Selection) • Pseudocode (Iteration) • Examples

  3. Pseudocode • Pseudocode consists of short, English phrases used to explain specific tasks within a program’s algorithm.

  4. Pseudocode • Used to provide an outline description of the specification for the software module • Contain a natural language of expressions embedded in syntactic structures taken from programming language • IF…THEN…ELSE • REPEAT..UNTIL • Not intended to be executed by a computer, must be interpreted by people.

  5. PseudocodeSequence

  6. Pseudocode (Sequence) • When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end. • This is a basic assumption of all algorithm design. • We call this SEQUENCE.

  7. Pseudocode (Sequence) Statement1; Statement2; Statement3; Statement4; Statement5; Statement6; Statement7; Statement8; • In pseudo-code it looks like this:

  8. Pseudocode (Sequence) Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk and/or sugar; Serve; • For example, for making a cup of tea:

  9. Pseudocode (Sequence) BEGIN PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk and/or sugar; Serve; END. • Or as a program:

  10. PseudocodeSeLECTION

  11. Pseudocode (Selection) • What if we want to make a choice • example, do we want to add sugar or not to the tea? • We call this SELECTION.

  12. Pseudocode (Selection) IF (sugar is required) THEN add sugar; ELSE don’t add sugar; ENDIF; • So, we could state this as:

  13. Pseudocode (Selection) IF (<CONDITION>) THEN <Statements>; ELSE <Statements>; ENDIF; • Or, in general:

  14. Pseudocode (Selection) IF (A > B) THEN Print A + “is bigger”; ELSE Print B + “is bigger”; ENDIF; • Or to check which number is biggest:

  15. Pseudocode (Selection) • BEGIN • PROGRAM MakeACupOfTea: • Organise everything together; • Plug in kettle; • Put teabag in cup; • Put water into kettle; • Wait for kettle to boil; • Add water to cup; • Remove teabag with spoon/fork; • Add milk; • IF (sugar is required) • THEN add sugar; • ELSE do nothing; • ENDIF; • Serve; • END. • Adding a selection statement in the program:

  16. PseudocodeITERATION

  17. Pseudocode (Iteration) • What if we need to tell the computer to keep doing something until some condition occurs? • Let’s say we wish to indicate that the you need to keep filling the kettle with water until it is full. • We need a LOOP, or ITERATION.

  18. WHILE (a true condition) STATEMENT or STATEMENT BLOCK concluded with ENDWHILE Pseudocode (Iteration) WHILE Loop REPEAT statement statement UNTIL (condition is true) REPEAT UNTIL Loop FOR (starting state, final state, increment) Statement Statement ENDFOR FOR Loop

  19. Pseudocode (Iteration) WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE; • So, we could state this as: • Or to print out the numbers 1 to 5: A = 1; WHILE(A > 5) DO Print A; A = A + 1; ENDWHILE;

  20. Get first entry If this is the required entry Then write down phone number Else get next entry If this is the correct entry then write done entry else get next entry if this is the correct entry Consider the problem of searching for an entry in a phone book with only condition: What is the benefit of using loop? Get first entry; Call this entry N; WHILE N is NOT the required entry DO Get next entry; Call this entry N; ENDWHILE; We may rewrite this as follows:

  21. Pseudocode (Iteration) BEGIN PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve; END • Or as a program:

  22. PseudocodeeXAMPLES

  23. Examples • So let’s say we want to express the following algorithm: • Read in a number and print it out. BEGIN PROGRAMPrintNumber: Use variables: A OF TYPE Integer Read A; Print A; END

  24. Examples • So let’s say we want to express the following algorithm: • Read in a number and print it out double the number BEGIN PROGRAMPrintDoubleNumber: Use variables: A,B OF TYPE Integer Read A; B = A*2; Print B; END

  25. Examples • So let’s say we want to express the following algorithm to print out the bigger of two numbers: • Read in two numbers, call them A and B. Is A is bigger than B, print out A, otherwise print out B. BEGIN PROGRAMPrintBiggerOfTwo: Use variables: A,B OF TYPE Integer Read A; Read B; IF (A>B) THEN Print A; ELSE Print B; ENDIF; END

  26. Examples • So let’s say we want to express the following algorithm to print out the bigger of three numbers: • Read in three numbers, call them A, B and C. • If A is bigger than B, then if A is bigger than C, print out A, otherwise print out C. • If B is bigger than A, then if B is bigger than C, print out B, otherwise print out C.

  27. BEGIN PROGRAM BiggerOfThree: Use variables: A,B,C OF TYPE Integer Read A; Read B; Read C; IF (A>B) THEN IF (A>C) THEN Print A; ELSE Print C; END IF; ELSE IF (B>C) THEN Print B; ELSE Print C; END IF; END IF; END Examples

  28. Examples • So let’s say we want to express the following algorithm: • Print out the numbers from 1 to 5 BEGIN PROGRAM Print1to5: Use variables: A OF TYPE Integer A = 1; WHILE (A != 6) DO Print A; A = A + 1; ENDWHILE; END

  29. Examples BEGIN PROGRAMComputeSum: Use variables: number1, number2, sum OF TYPE Integer DISPLAY “Enter the numbers” ACCEPT number1, number2 Sum:=number1 + number2 DISPLAY “SUM” END

  30. Examples BEGIN PROGRAMCategoryOfInsurance: Use variables: category OF TYPE character insurance OF TYPE string ACCEPT category DO CASE of category CASE category = U DISPLAY Insurance := “not available” CASE category = A DISPLAY Insurance := “double” CASE category = B DISPLAY Insurance := “normal” CASE category = M DISPLAY Insurance := “medically dependent” OTHERWISE DISPLAY “entry is invalid” ENDCASE END

  31. Examples BEGIN PROGRAM NumberRange0-100: Use variables: number OF TYPE Integer REPEAT DISPLAY “Enter a number between 0 and 100” ACCEPT number UNTIL number <0 or number >100 END

  32. Examples Create a flowchart for inputting your employee’s name. If the first letter starts from A to J then display “ First Category” , if K to T then display “ Second Category” otherwise display Third Category If student's grade is greater than or equal to 60 Print "passed“ else Print "failed“ Set the counter to 0. If the counter is more than 10 then customer receives free value meal otherwise no free value meal.

More Related