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.
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.
Input is done with scanf.
Output is done with printf.
Moving things around and changing things is done with the assignment statement - a destructive update.
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.
iteration
repetition
loops
while loops
repeat or do while loops
for loops (many variations)
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
Here 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).
Since something in expression must change, the expression must contain at least one variable.
That variables needs to be initialized.
That variable needs to be changed as a result of the execution of the statements.
9. Viewpoints
There are (at least) two view of understanding a while loop (or any loop).
Operational (engineering) view (how it works)
Functional (mathematical) view (what it does)
A good computer science needs to understand both views and be able to mentally switch between them when needed.
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.