Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Pseudo-code and loops


1. Pseudo-code and loops

2. Primitive statements
Primitive statements are statements that do something.

There are only three types of primitive statements (in any imperative) programming language (like c). Here are the primary ones for c. All other statements control the flow of in what order these statements are executed(if at all).

3. Loops
Loops allow actions to be done zero or more times.

There are several names that refer to loops.

4. Infinite loops
With the loop comes the possibility that a program may never stop.

In student-oriented systems (like CloudCoder) a time limit is placed on the program.

In other programs, the program may never end and the user may need to force the program to stop. At the command line, one would press Ctrl-C.

5. Flow charts
Flow charts are pictoral diagrams of the control flow of a program.

There are many useful diagrams for software design, but the (control) flow chart is not one of them.

In 1972 they were referred to as "flaw charts". At that time there was an interest in reasoning about and proving programs correct. Flow charts were not useful for understanding control flow, reasoning about control flow, nor in proving programs correct.

6. Pseudocode
One result of the move towards reasoning about and proving programs correct was the use of pseudo-code.

The word "pseudo" means "false". A pseudo-code as a readable code that is not a true code, but something that can be read and understand. A pseudo-code often leaves out a lot of details that are needed in real code.

7. Example flow chart
Contral flow chart exampleHere is an example of a control flow chart. Do not try to think like this! A control flow chart is like a cryptic puzzle. Instead, try to think in terms of pseudo-code as follows.
   Let a and be be non-negative integers    Set count to 0    Set product to 0    While count < b Do       Add a to product       Add 1 to count       End While




8. While loop
Here is the while loop in pseudo-code form.
   While expression Do       statements       End While

The semantics is that while the expression is true the statements are executed. (Expressions are assumed not to have side-effects).

9. Viewpoints
There are (at least) two view of understanding a while loop (or any loop). A good computer science needs to understand both views and be able to mentally switch between them when needed.

10. Operational viewpoint
While loop pseudo-code:
   While expression Do       statements       End While

Operational (engineering) viewpoint (using goto constructs):
   100 If not expression Then GoTo 400    200 statements    300 GoTo 100    400


11. Functional viewpoint
While loop pseudo-code:
   While expression Do       statements       End While

Functional (mathematics) viewpoint (using a recursive definition):
   if expression Then       statements       While expression Do          statements          End While       End If


12. Refined pseudo-code
Some refined pseudo-code is now presented - in more detail than would usually be done - for example purposes.

13. While loop pseudo-code
Here is a pseudo-code for counting from 1 to 4 using a while loop.
set i1 to 0 set n1 to 4 while i1 != 4 do    add 1 to i1    print i1    end while

Note that pseudo-code is not c code. But it is a simplified form that is close to c (or other languages).

14. Loop in c
Here is a loop that counts from 1 to 4 using a while loop.

Here is the C code.

Note: I usually start a loop with a loop that does just the counting and output. It works well to have some output of the loop body variables. When not needed, just comment out the printf. When needed, remove the comments for that statement.

15. Output
Here is the output of the C code.


16. Loop variation
Here is a minor variation of the above loop code. What are the differences? Check the output too.

Here is the C code.


17. Output
Here is the output of the C code.


18. End of page

19. Multiple choice questions for this page