-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bd787b
commit f0c887f
Showing
4 changed files
with
193 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<link rel="stylesheet" href="../../main.css"> | ||
<link rel="stylesheet" href="../main.css"> | ||
|
||
<title>Computer Science Revision</title> | ||
|
||
<link rel="icon" type="image/png" href="../icons/logo.svg"/> | ||
<meta property="og:image" content="../icons/logo.svg" /> | ||
<meta property="og:image:width" content="256" /> | ||
<meta property="og:image:height" content="256" /> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta name="description" content="1.02 - Pseudo-code | Computer Science Revision"> | ||
</head> | ||
|
||
<body> | ||
<h1>1.02 – Pseudo-code</h1> | ||
|
||
<a class="prev-link" href="../1-01-Computational-Thinking/">Previous: 1.01 - Computational Thinking</a> | ||
<br /> <br /> | ||
|
||
<p class="definition"> | ||
<img src="../icons/book.svg" class="icon" /> | ||
<strong>Pseudo-code</strong> is a way of writing algorithms that reads like code written in actual programming languages. | ||
</p> | ||
|
||
<p> | ||
<b>Pseudo-code is not a real programming language.</b> | ||
The idea is that pseudo-code clearly shows an algorithm's steps without worrying about the syntax (finer details) of any particular programming language. | ||
</p> | ||
|
||
<p><br />There are three basic programming constructs which we use when writing pseudo-code:</p> | ||
|
||
<p class="definition"> | ||
<img src="../icons/book.svg" class="icon" /> | ||
<strong>Sequence</strong> is just a mattter of writing of writing down the steps in the correct order. | ||
</p> | ||
|
||
<p class="definition"> | ||
<img src="../icons/book.svg" class="icon" /> | ||
<strong>Selection</strong> allows you to choose between multiple options. | ||
</p> | ||
|
||
<p>Example 1 shows how an <code>IF</code> statement can be used in pseudo-code.</p> | ||
|
||
<p class="code"> | ||
<img src="../icons/code.svg" class="icon" /> | ||
Example 1 <br /> | ||
<code> | ||
age ← <el class="function">USERINPUT</el> <br /> | ||
<el class="keyword">IF</el> age ≥ <el class="number">18</el> <el class="keyword">THEN</el> <br /> | ||
  <el class="function">OUTPUT</el> <el class="string">"OK"</el> <br /> | ||
<el class="keyword">ELSE</el> <br /> | ||
  <el class="function">OUTPUT</el> <el class="string">"Too young"</el> <br /> | ||
<el class="keyword">ENDIF</el> | ||
</code> | ||
</p> | ||
|
||
<p class="definition"> | ||
<img src="../icons/book.svg" class="icon" /> | ||
<strong>Iteration</strong> means looping. There are three different types of loops in pseudo-code. | ||
</p> | ||
|
||
<p>Example 2 shows an algorithm containing a <code>FOR ... ENDFOR</code> loop. The algorithm asks the user for five numbers and outputs their sum.</p> | ||
|
||
<p class="code"> | ||
<img src="../icons/code.svg" class="icon" /> | ||
Example 2 <br /> | ||
<code> | ||
total ← <el class="number">0</el> <br /> | ||
<el class="keyword">FOR</el> count ← <el class="number">1</el> <el class="keyword">TO</el> <el class="number">5</el> <br /> | ||
  num ← <el class="function">USERINPUT</el> <br /> | ||
  total ← total + num <br /> | ||
<el class="keyword">ENDFOR</el> <br /> | ||
<el class="function">OUTPUT</el> total | ||
</code> | ||
</p> | ||
|
||
<p>Example 3 shows an algorithm containing a <code>REPEAT ... UNTIL</code> loop. The algorithm counts up until the user stops it.</p> | ||
|
||
<p class="code"> | ||
<img src="../icons/code.svg" class="icon" /> | ||
Example 3 <br /> | ||
<code> | ||
count ← <el class="number">0</el> <br /> | ||
<el class="keyword">REPEAT</el> <br /> | ||
  count ← count + <el class="number">1</el> <br /> | ||
  <el class="function">OUTPUT</el> count <br /> | ||
  end ← <el class="function">USERINPUT</el> <br /> | ||
<el class="keyword">UNTIL</el> end = <el class="string">"yes"</el> | ||
</code> | ||
</p> | ||
|
||
<p>Example 4 shows an algorithm containing a <code>WHILE ... ENDWHILE</code> loop. The algorithm keeps asking the user for a password until they get it correct.</p> | ||
|
||
<p class="code"> | ||
<img src="../icons/code.svg" class="icon" /> | ||
Example 4 <br /> | ||
<code> | ||
password ← <el class="function">USERINPUT</el> <br /> | ||
<el class="keyword">WHILE</el> password ≠ <el class="string">"Password123"</el> <br /> | ||
  <el class="function">OUTPUT</el> <el class="string">"Try again"</el> <br /> | ||
  password ← <el class="function">USERINPUT</el> <br /> | ||
<el class="keyword">ENDWHILE</el> <br /> | ||
<el class="function">OUTPUT</el> <el class="string">"Correct password"</el> | ||
</code> | ||
</p> | ||
|
||
<p> | ||
<code>REPEAT ... UNTIL</code> loops and <code>WHILE ... ENDWHILE</code> loops are very similar, but there are some key differences. | ||
</p> | ||
|
||
<p> | ||
Firstly, <code>REPEAT ... UNTIL</code> loops keep looping <em>until</em> the condition is true, | ||
whereas <code>WHILE ... ENDWHILE</code> loops keep looping <em>while</em> the condition is true. | ||
</p> | ||
|
||
<p> | ||
Secondly, <code>REPEAT ... UNTIL</code> loops check the condition at the end, so will always run at least once, | ||
but <code>WHILE ... ENDWHILE</code> loops check their condition at the start, so won't run if the condition starts off as false. | ||
</p> | ||
|
||
<p><br />Example 5 shows an algorithm written in pseudo-code.</p> | ||
|
||
<p class="code"> | ||
<img src="../icons/code.svg" class="icon" /> | ||
Example 5 <br /> | ||
<code> | ||
total ← <el class="number">1</el> <br /> | ||
<el class="keyword">FOR</el> num ← <el class="number">1</el> <el class="keyword">TO</el> <el class="number">10</el> <br /> | ||
  <el class="keyword">IF</el> num MOD <el class="number">2</el> = <el class="number">0</el> <el class="keyword">THEN</el> <br /> | ||
    total ← total * num <br /> | ||
  <el class="keyword">ENDIF</el> <br /> | ||
<el class="keyword">ENDFOR</el> <br /> | ||
<el class="function">OUTPUT</el> total | ||
</code> | ||
</p> | ||
|
||
<p class="question"> | ||
<img src="../icons/question.svg" class="icon" /> | ||
What does the code in Example 5 do? | ||
</p> | ||
|
||
<p><i>Hint: <code>num MOD 2</code> means the remainder from dividing <code>num</code> by <code>2</code>.</i><br /> </p> | ||
|
||
<p class="spoiler"> | ||
<el class="default">Hover/tap to reveal</el> | ||
<el class="answer">Multiplies all the even numbers from 1 to 10 and outputs the result.</el> | ||
</p> | ||
|
||
<br /> | ||
<br /> | ||
|
||
<div id="sub">© Rujul Nayak <el id="year">2024-</el></div> | ||
<script src="../../year.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters