How Do I Write Pseudocode?

How Do I Write Pseudocode?

How to Write Pseudocode

In this video, the speaker explains what pseudocode is and how to write it. The purpose of pseudocode is to be a high-level and abstract way of representing an algorithm without tying it to a specific programming language.

What is Pseudocode?

  • Pseudocode is not an actual programming language; it's fake code that looks like code but doesn't belong to any specific language.
  • Actual programming languages have strict syntax rules, but pseudocode doesn't have a syntax, so you can't have syntax errors.
  • Pseudocode is regarded as high-level or abstract because it simplifies the problem by not dealing with all the nitty-gritty details.

How to Write Pseudocode

  • Writing pseudocode shouldn't be stressful because it's not about the actual writing of the code; it's about understanding how the algorithm works.
  • Exam boards often stick to a standardized syntax for their exam papers' pseudocode excerpts, which can cause confusion when comparing answers that look different from each other.

Conclusion

  • Overall, pseudocode is a useful tool for representing algorithms in a high-level and abstract way without tying them down to specific programming languages.

Understanding Pseudocode

In this section, the speaker explains what pseudocode is and how it can be used to express algorithms. They provide real-life examples of pseudocode being used in textbooks and scientific papers.

What is Pseudocode?

  • Pseudocode is a way of expressing algorithms that doesn't require knowledge of a specific programming language.
  • It's not necessary to memorize the syntax of pseudocode since it can be expressed in any style as long as it can be reasonably inferred by a competent programmer.

Real-Life Examples

Textbook Example

  • A university textbook on AI uses pseudocode to express machine learning algorithms.
  • The example given is for decision trees, which has constructs similar to those used in programming languages such as functions, parameters, return values, if-else statements, for loops, and variables.

Scientific Paper Example

  • Scientific papers use structured documents to explain research findings.
  • One paper proposes an algorithm using high-level statements while another uses more formalized pseudocode with constructs similar to those used in programming languages.

Advantages of Using Pseudocode

  • Writing in pseudocode is more useful than writing in a specific programming language because it allows for abstract expression that can be understood regardless of the reader's knowledge or experience with a particular language.
  • It also allows for easier conversion into different programming languages and future-proofing against changes in technology.

Pseudocode vs Flowcharts

The speaker discusses the benefits of using pseudocode over flowcharts for problem-solving in exams.

Pseudocode is preferred

  • Pseudocode is recommended over flowcharts for problems like these.
  • Even if the question doesn't explicitly ask for pseudocode, it's still recommended to use it.

Writing pseudocode

  • Capitalize keywords and boolean values.
  • Use indentation when nesting control structures.
  • Close off blocks with an "end" statement or indentation to improve clarity.

Problem-Solving with Pseudocode

The speaker walks through a specific problem and demonstrates how to write pseudocode to solve it.

Initializing variables

  • Initialize variables at the beginning of the code, such as amount of fuel and number of kilometers traveled.
  • Use descriptive variable names, such as "kilometers" instead of "x".

User input

  • Use built-in functions like "input" for user input questions.

Clarity in writing

  • Write code clearly and concisely to make it easy to read and understand during exams.

Setting Fuel Amount

In this section, the speaker explains how to set the amount of fuel needed for a program.

Setting Fuel Amount

  • To set the amount of fuel, use an if statement with kilometers greater than 0.
  • Set fuel to be 100 times greater than the number of kilometers.
  • If the amount of fuel is less than 1500, set it to be 1500.

Outputting Results

In this section, the speaker discusses how to output results in programming.

Outputting Results

  • Use a built-in function like "print" or "console.log" to output results.
  • Capitalize keywords like "if" and "end if" for clarity.
  • Use indentation and consistent formatting for readability.
  • Include annotations or comments to clarify nesting and remove ambiguity.

Pseudocode Implementation

In this section, the speaker provides examples of pseudocode implementation in C++ and Java.

Pseudocode Implementation

  • The core code is similar between languages but there are differences specific to each language.
  • Braces can be used instead of indentation for closing off blocks.
  • Indentation makes code more readable even though it's not necessary for functionality.
  • It's possible to write actual program code in an exam answer but including boilerplate code doesn't make sense.

Importance of Learning Real Code for Pseudocode

In this section, the speaker emphasizes the importance of learning a real programming language before attempting to write and understand pseudocode.

Learning a Real Programming Language

  • To write and understand pseudocode, it is important to learn a real programming language first.
  • The basics of any programming language can be learned through online tutorials such as Python or Java.
  • Object-oriented concepts such as classes and objects are also important to know for writing pseudocode.

Writing Pseudocode

  • Understanding actual code helps in writing pseudocode that makes sense.
  • Consistency is key when writing pseudocode. Once you choose a syntax, stick with it throughout the code.
  • Practice writing longer mark algorithm questions using pseudocode to become more comfortable with it.

Final Thoughts

  • Simplify your code by making it as general as possible.
  • Don't stress too much about writing perfect pseudocode since it's just a simplification of real code and will never actually be run.
Video description

Lots of students find writing pseudocode difficult so this video explains what it is, shows some real life examples of it, and goes through an example question. How do I Answer Algorithm Questions? https://www.youtube.com/watch?v=kaG4xeLqeH0&ab_channel=ComputerScienceTutor Practice algorithm questions: https://drive.google.com/drive/u/1/folders/1OVWpwIKbE7oKIcKnnJ0MyLn2cPkvV2mn 0:00 Introduction 1:32 What is pseudocode? 4:00 Exam board pseudocode 6:40 Real life examples 10:57 Going through a practise question 23:19 Final tips If this video was useful, please like it and subscribe, it really helps! Also, if you use an ad blocker, whitelisting my channel is very much appreciated! Any questions/ feedback/ enquiries: tutorcomputerscience@gmail.com These videos will always be free but if you'd consider a donation I'd be extremely grateful: https://www.paypal.me/computersciencetutor If you are studying for a specific course, to watch the videos in their intended order and only those applicable to your Computer Science course, please use the playlists on my channel page.