I want to start of with saying the these will look very much like loops in Java, so they should not be too difficult to understand.
The first loop we will go over is a "for" loop. It is put together in the following manner:
for (initialization; exit_condition; repeat){}
The main difference is that in C the initialization step, the variable must be initialized before the loop. In other words the variable is not a local variable for that loop alone, it's a global one for the entire class.
If we try and initialize the variable completely in the loop, the program will give us an error when we try to build it.
Another kind of loop is the "while" loop. While the for loop uses the increments to tell when the condition is reached, a "while" loop will just continue until the condition inside of it is reached.
This last example for loops will be doing a nested loop. For this I will use a nested "for" loop to create a variety of results. This is just a loop inside of another loop, and these can be done more than just once, and the more there are the more complex and confusing they become. These can be used for more complex mathematical functions, or running through multidimensional arrays.
No comments:
Post a Comment