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.
Here is the pseudo-code for an if-then-else statement.
If Then
Else
End If
Here is a shorter form used in examples.
If b Then
s1
Else
s2
End If
4. If-Then statement
Here is the pseudo-code for the if-then statement where no else part is needed.
If Then
End If
Here is a shorter form used in examples.
If b Then
s
End If
5. Iteration using conditionals.
Iteration can be performed (a limited number of times) using conditional statements.
6. At most one iteration
If b Then
s
End If
7. At most two iterations
If b Then
s
If b Then
s
End If
End If
8. At most three iterations
If b Then
s
If b Then
s
If b Then
s
End If
End If
End If
Do you see the pattern? These are not separate if statements. They are a collection of nested if statements where b and s are exactly the same every place that they occur.
9. Program examples
Below are some C examples for iteration using conditionals. In this case, the powers of 2 from 1 to the limit (up to 4) are output. Then, the final power is output.
The iteration body comments are to show how the code is organized. They would not be written in actual code.
Here is the C code.
10. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
11. Notes
In this program, we can check all possible input cases. In general, it is not possible to check all possible input cases.
12. Short form for assignment
Note the use of "+=" and "*=" in the above code.
The following forms are equivalent (as assignment statements, not in loops which have not yet been covered).
13. Addition
Adding 1 (one) to a variable is called an increment. In general, avoid "++" except in common patterns.
14. Subtraction
Subtracting 1 (one) to a variable is called an decrement. In general, avoid "--" except in common patterns.
15. Multiplication
If it makes sense for the problem being solved, the same short forms can be used with other operators such as multiplication, division, etc.
16. C: Iteration
The term iteration means doing something more than once.
Iteration is the same as repetition.
Iteration is not the same thing as a loop. A loop is one way to do iteration, but one can do iteration by doing the same (or similar) thing more than one time.
17. Iteration
Here is a C program that reads in a non-negative integer representing how many integers follow.
The comments for begin and end of iteration body are to show how the code is organized. One would not write the comments in the actual code.
18. Program 1: best way
Here is the best way to do iteration via loop unrolling.
Here is the C code.
19. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
20. Redundancy
Notice the redundancy in the above program and the limitations of the above program.
Computer science is the search for finite representations of (potentially) infinite objects.
In order to remove the above redundancies and remove the limitations of the above program, the concept of a loop will be soon introduced.
21. Another way
Here is another way to do the above. This way should not be used as it requires a check for every possibility along the way.
Do not use this example for any work! It is for comparison only.
Here is the C code.
22. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
Here is an example input.
For the above example input, here is the expected output.
23. Notes
Make no unnecessary checks to see of the limit has been reached. (Follow the example code below).
Note: At this time, we do not know enough to separate the repeated input, processing, and output.
This program does not use a loop. This method is known as loop unrolling and is used in special cases to speed up certain code.