This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2023:cprog:projects:cnv0 [2023/02/28 14:09] – wedge | haas:spring2023:cprog:projects:cnv0 [2023/02/28 14:35] (current) – [Process] wedge | ||
---|---|---|---|
Line 107: | Line 107: | ||
yourpi: | yourpi: | ||
</ | </ | ||
+ | |||
+ | =====Loops===== | ||
+ | A loop is basically instructing the computer to repeat a section, or block, or code a given amount of times (it can be based on a fixed value-- repeat this 4 times, or be based on a conditional value-- keep repeating as long as (or while) this value is not 4). | ||
+ | |||
+ | Loops enable us to simplify our code-- allowing us to write a one-size-fits all algorithm (provided the algorithm itself can appropriately scale!), where the computer merely repeats the instructions we gave. We only have to write them once, but the computer can do that task any number of times. | ||
+ | |||
+ | Loops can be initially difficult to comprehend because unlike other programmatic actions, they are not single-state in nature-- loops are multi-state. What this means is that in order to correctly " | ||
+ | |||
+ | With that said, it is important to be able to focus on the process of the individual steps being taken. What is involved in taking a step? What constitutes a basic unit of stairway traversal? If that unit can be easily repeated for the next and the next (and in fact, the rest of the) steps, we've described the core process of the loop, or what will be iterated a given number of times. | ||
+ | |||
+ | In C and C-syntax influenced languages (C++, Java, PHP, among others), we typically have 3 types of loops: | ||
+ | |||
+ | * **for** loop (automatic counter loop, stepping loop; top-driven) - when we know exactly how many times we wish something to run; we know where we want to start, where we want to end, and exactly how to progress from start to end (step value) | ||
+ | * **while** loop (top-driven conditional loop) - when we want to repeat a process, but the exact number of iterations is either not known, not important, not known, or variable in nature. While loops can run 0 or more times. | ||
+ | * **do-while** loop (bottom-driven conditional loop) - similar to the while loop, only we do the check for loop termination at the bottom of the loop, meaning it runs 1 or more times (a do-while loop is guaranteed to run at least once). | ||
+ | |||
+ | ====for() loops==== | ||
+ | A **for()** loop is the most syntactically unique of the loops, so care must be taken to use the proper syntax. | ||
+ | |||
+ | With any loop, we need (at least one) looping variable, which the loop will use to analyze whether or not we've met our looping destination, | ||
+ | |||
+ | A for loop typically also has a defined starting point, a " | ||
+ | |||
+ | Here's a sample for() loop, in C, which will display the squares of each number, starting at 0, and stepping one at a time, for 8 total iterations: | ||
+ | |||
+ | <code c> | ||
+ | int i = 0; | ||
+ | |||
+ | for (i = 0; i < 8; i++) | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The output of this code, with the help of our loop should be: | ||
+ | |||
+ | <cli> | ||
+ | loop #1 ... 0 | ||
+ | loop #2 ... 1 | ||
+ | loop #3 ... 4 | ||
+ | loop #4 ... 9 | ||
+ | loop #5 ... 16 | ||
+ | loop #6 ... 25 | ||
+ | loop #7 ... 36 | ||
+ | loop #8 ... 49 | ||
+ | </ | ||
+ | |||
+ | Note how we can use our looping variable (**i**) within mathematical expressions to drive a process along... loops can be of enormous help in this way. | ||
+ | |||
+ | And again, we shouldn' | ||
+ | |||
+ | The loop exits once **i** reaches a value of 8, because our loop determinant condition states as long as **i** is **less than** **8**, continue to loop. Once **i** becomes **8**, our looping condition has been satisfied, and the loop will no longer iterate. | ||
+ | |||
+ | The stepping (that third) field is a mathematical expression indicating how we wish for **i** to progress from its starting state (of being equal to 0) to satisfying the loop's iterating condition (no longer being less than 8). | ||
+ | |||
+ | **i++** is a shortcut we can use in C; the longhand (and likely more familiar) equivalent is: **i = i + 1** | ||
+ | |||
+ | ====while() loops==== | ||
+ | A **while()** loop isn't as specific about starting and stepping values, really only caring about what condition needs to be met in order to exit the loop (keep looping while this condition is true). | ||
+ | |||
+ | In actuality, anything we use a for loop for can be expressed as a while loop-- we merely have to ensure we provide the necessary loop variables and progressions within the loop. | ||
+ | |||
+ | That same loop above, expressed as a while loop, could look like: | ||
+ | |||
+ | <code c> | ||
+ | int i = 0; | ||
+ | |||
+ | while (i < 8) | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | i = i + 1; // I could have used " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The output of this code should be identical, even though we used a different loop to accomplish the task (try them both out and confirm!) | ||
+ | |||
+ | **while()** loops, like **for()** loops, will run 0 or more times; if the conditions enabling the loop to occur are not initially met, they will not run... if met, they will continue to iterate until their looping conditions are met. | ||
+ | |||
+ | It is possible to introduce a certain kind of **logical error** into your programs using loops-- what is known as an " | ||
+ | |||
+ | Another common **logical error** that loops will allow us to encounter will be the "off by one" error-- where the conditions we pose to the loop are incorrect, and the loop runs one magnitude more or less than we had intended. Again, proper debugging of our code will resolve this situation. | ||
+ | |||
+ | ====do-while loops==== | ||
+ | The third commonly recognized looping structure in C, the do-while loop is identical to the while() (and therefore also the for()) loop, only it differs in where it checks the looping condition: where **for()** and **while()** are " | ||
+ | |||
+ | The placement of this test determines the minimal number of times a loop can run. | ||
+ | |||
+ | In the case of the for()/ | ||
+ | |||
+ | For the do-while loop, because the test occurs at the bottom, the body of the loop (one full iteration) is run before the test is encountered. So even if the conditions for looping are not met, a do-while will run "1 or more times" | ||
+ | |||
+ | That may seem like a minor, and possibly annoying, difference, but in nuanced algorithm design, such distinctions can drastically change the layout of your code, potentially being the difference between beautifully elegant-looking solutions and those which appear slightly more hackish. They can BOTH be used to solve the same problems, it is merely the nature of how we choose express the solution that should make one more preferable over the other in any given moment. | ||
+ | |||
+ | I encourage you to intentionally try your hand at taking your completed programs and implementing other versions that utilize the other types of loops you haven' | ||
+ | |||
+ | So, expressing that same program in the form of a do-while loop (note the changes from the while): | ||
+ | |||
+ | <code c> | ||
+ | int i = 0; | ||
+ | |||
+ | do | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | i = i + 1; // again, we could just as easily use " | ||
+ | } while(i < 8); | ||
+ | </ | ||
+ | |||
+ | In this case, the 0 or more vs. 1 or more minimal iterations wasn't important; the difference is purely syntactical. | ||
+ | |||
+ | With the do-while loop, we start the loop with a **do** statement. | ||
+ | |||
+ | Also, the do-while is the only one of our loops which NEEDS a terminating semi-colon (**;**).. please take note of this. | ||
=====Program===== | =====Program===== | ||
Line 161: | Line 273: | ||
The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate. | The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate. | ||
+ | |||
+ | =====Process===== | ||
+ | In general, you will be looking to do something like the following: | ||
+ | |||
+ | < | ||
+ | DISPLAY PROMPT | ||
+ | READ NUMBER | ||
+ | |||
+ | SHOULD NUMBER BE LOWER THAN TWO: | ||
+ | DISPLAY AN ERROR | ||
+ | EXIT WITH A NON-ZERO STATUS | ||
+ | |||
+ | SO LONG AS FACTOR IS LESS THAN NUMBER: | ||
+ | SHOULD THE FACTOR BE A LEGITIMATE FACTOR OF NUMBER: | ||
+ | INCREMENT COUNT OF FACTOR PAIRS | ||
+ | SHOULD THIS NUMBER HAVE A SQUARE FACTOR: | ||
+ | INCREMENT COUNT OF FACTOR PAIRS | ||
+ | |||
+ | SHOULD THE NUMBER OF FACTOR PAIRS MATCH WHAT WE ARE LOOKING FOR: | ||
+ | DISPLAY THAT NUMBER BEING PROCESSED IS OF THE NEEDED TYPE | ||
+ | OTHERWISE: | ||
+ | DISPLAY THAT THE NUMBER BEING PROCESSED IS NOT THE NEEDED TYPE | ||
+ | </ | ||
=====Reference===== | =====Reference===== |