Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.

Similar presentations


Presentation on theme: "PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear."— Presentation transcript:

1 PHYS 2020 Pseudocode

2 Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear statement of the problem, with the steps you need to solve it, in plain English.  2) Take your statement and turn it into pseudocode. This should be a program, but not in a specific language.  3) When you are satisfied with the logic of the pseudocode, then turn your program into C (or whatever).

3 What is Pseudocode  Pseudocode mimics computer code in that it contains the same structures, but it does not have a specific syntax.  I have seen very structured pseudocode, but as long as you detail your structures and logic properly, the specific words are not that important.  I think that you should adopt your own preferred statements and practice using them.

4 Suggestions for Pseudocode  A set of words you can refer to helps e.g.  PRINT “print this to screen”  WHILE (condition is true) DO (this) ENDWHILE  FOR (a specified number of times) REPEAT (this)  IF (this is true) DO (this) ELSE DO (that) ENDIF  MODULE (values passed) (value returned) {do something in a separate module with the values passed and return the value returned}  COMMENT {remind yourself to put some comments in the program}

5 More Suggestions  LET variable = value {assign a value to a variable}  Variable = INPUT {take input from the key board and assign it to a variable}  OPEN file {open a file for output}  CLOSE file {close a file when finished with it}  WRITE (values) (file) {write values to a file}

6 An Example  Problem: Design a program to make a database which contains peoples names and occupations. The program should accept input from the keyboard and print output to the terminal. It should also create a file with the information so that it is stored permanently.  Solution in Class Wed 14 th March 2007.

7 Plain English Statement  The program needs to  Take input from the keyboard to collect a data set of peoples names and occupations  Store this information in variables  Write this information to a file  Repeat this process until the users says to stop  Take input from the keyboard after each entry to see if the user wants to enter another set of data  Use the input to decide whether to keep asking for data or exit.  Ask the user if they want the data printed to the screen after data collection is finished.

8 What Next?  Now we will write the program in pseudocode.  This is a good planning tool, and also emphasizes the difference between the logical structures needed to write a program (such as DO WHILE (condition is true), and IF (condition is met) DO) which can be expressed in any computing language), and the syntax of a particular language (sort of like writing instructions in e.g. English, French, German, Turkish, Iranian, Hindi, Japanese, Chinese or Vietnamese etc. The language, and the structures of the language, can be quite different, but the meaning conveyed will be the same.  The importance of pseudocode is that it concentrates the mind on the logic flow rather than the syntax of the language.  i.e. Important things like “how do I set up an IF statement” rather than “are all the semicolons in the right place”.

9 Detailed Pseudocode (1)  Program Setup  Comments in code to explain what program does  Define variables and their types  Print introduction to screen for the the user, explaining what the program does and how they should use it.

10 Detailed Pseudocode (2)  Collect input from user and store data:  OPEN storevalues PRINT “Would you like to enter data”  PRINT“enter y or n”  answer = INPUT

11 Detailed Pseudocode (3)  WHILE (answer = y) DO  PRINT “Please enter first name”  First = INPUT  PRINT “Please enter family name”  Family = INPUT  PRINT “Please enter occupation”  Occupation = INPUT  WRITE (First, Family, Occupation) to (storefile)  PRINT “Do you want to enter data for another person? Y or N?”  Answer = INPUT  END WHILE

12 Detailed Pseudocode (4)  Once answer = n and the loop exits, tidy up and ask whether the user want to print the data entered to the screen.  PRINT “Do you want a printout of what you have entered today? (y/n)”  Printout = INPUT  Test to see if user wants output printed  IF (Printout = y) THEN  WHILE (NOT EOF)  READLINE storefile (first, family, occupation)  Print “first, family, occupation”  ENDWHILE  ENDIF  CLOSE storefile  END program

13 How Much Pseudocode Should I/you/one* Use?  When you first start writing programs, using detailed pseudocode is really useful to help you get the logic used in computer programming inside your head, to use a technical description.  As you get more familiar with the logical structures (IF, WHILE, DO etc.) you can write less verbose pseudocode.  To be honest, I now write my pseudocode in my head (really!), and go straight to writing in the language.  BUT, I think the torture I was put through as an undergrad, writing pseudocode and programming in the training language PASCAL, is what has enabled me to work out in my head what logical structures a program will need.  Also, with a long and difficult program I still sketch out a skeleton pseudocode plan.

14 How Much Pseudocode Should I/you/one* Use?  Unfortunately we won’t have time in this course to cover the scripting language PERL, but I love the PERL maxim “there is more than one way to do it”. This applies to programming in C (or any other language).  The aim is to produce a working, maintainable program, that you will still know how to use a year (or more) after you wrote it (and optional but desirable extra, that someone else can understand the logic of and use and modify if necessary). I can give you skills that I think will help you do this in the most efficient way possible, but how I am most efficient will be different to how you are most efficient. When you are coding, reflect on you successes and failures, and find what works best for you

15 How Much Pseudocode Should I/you/one* Use?  * Why the apostrophe in the title?  Language is a tricky and subtle thing, and computer languages share this trait, so it is good to understand this.  Should the title be “How much pseudocode should I use” or “How much pseudocode should you use” or “How much pseudocode should one use” ?  In English all three forms are acceptable, with the “How much pseudocode should one use” form sounding a bit formal compared to the other two.  However, after spending some months in France recently, I realize that the “How much pseudocode should one use” form is a vulgarism, and the polite way to say this would be “How much pseudocode should we use?”  Computer languages restrict themselves to one form of a statement as correct and all others as unrecognized  When your program wont compile or work, check the syntax!


Download ppt "PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear."

Similar presentations


Ads by Google