C Basics

C Loops

Loops are many times the very heart of a program. They allow programmers to execute the same code a given amount of time. Loops, when used the right way can be very powerful programming tools. There a three loops that we will look over, the first is called a while loop, here is how it looks.

While Loop

while (condition)
{
do stuff;
increment variable;
}

(condition) would be something like (x < 5) and the increment variable (in this case x) would look like x++, this would increment x by one every time the loop executes. Sometimes looking at code when it is described more in general (like how it is above) is actually more confusing than seeing an actual program, let us wait no more.

First notice how when I declared x I also initialized it to 0 in the same line this is legal and is perfect for lazy programmers like me! The while condition check to see if x is less than 5, well we just initialized it to 0 so of course it is. This allows the program to run through the loop and print out x (which is 0 right now) and increment x by 1. Now x is 1, the while condition checks again to see if x is still less than 5 and in this case it is. The command once again is to print out x (which is now 1) and then increment x by 1. Now x is 2, do you get the picture? All this program will do is print out 0,1,2,3, and 4, once x is 5 it is no longer less than 5, so the loop breaks and control is passed to the next command which in this case is to end the program. Not the most exciting program but hopefully this gives you a good idea what loops are all about.

Do While Loop

do
{
x=5;
printf("%i",x);
x++;
}
while(x < 5);

I know before you say it, this almost looks exactly the same and you are right but there is one significant difference. This is called a do while loop, the difference between the do while and the while is that the do while will execute at least once no matter what. This is because before checking the condition the code executes then the condition is checked last (notice the condition is the last thing printed). If we were to initialize x to 5 in the while loop program then the loop would have never ran and the program would just end. However, the do while will run once and once it checks the 5 is not less than 5 then the loop will not execute and the program will end. At first it's hard to see why someone would want a loop to execute at least once even if the condition isn't true, but trust me there will be moments when this little tool does just the trick. ALSO! Notice the semicolon after the do while condition, this is necessary however, when it is a while loop you do not put a semi colon, kind of tricky but it's just one of those things. The last kind of loop is called the for loop. This loop is the most commonly used probably because of it's clarity and versatility. I will write the same program as the while loop but using a for loop instead.

For Loop

Notice how the for loop is more clean and precise. The first thing you must do is initialize the variable x=0, then the condition while x<5, last the increment x++, also make sure you separate each with a semicolon. As you can see there is nothing new just the format, it is all in one, this is why it is commonly preferred. Also be sure to close the loop within curly braces as you would an if statement, this is required. Okay now lets check out a little more interesting program using loops. This program will print out a table of trigonometric functions from 0 to 360, for all of you who have taken trig you may appreciate this program a little more than those who have not. Also, if trig was one of your lesser favorite subjects I apologize in advance for boring you to death. NOTE! There are a few new things I have to introduce at once however, I will do my best to explain them.

Whew that was fun! Guys? You still there? Okay so let me break down this code. The first new thing is the header file math.h, this is used because we are using more advanced math like sine,cosine, and tangent. When we are using add,subtract,multiply, and divide, we don't need this header file. For things like square root, absolute value, exponents, and other advance mathematics we have to include the math.h header file. The next new thing you may have noticed is that #define PI 3.141593, this is a useful tool, whenever you are going to be using a certain constant continuously in your program it is helpful to define it with a keyword, #define is how you call upon this tool, the next thing is what you want to name it, in this case PI, the last thing is what PI holds 3.131593. Now whenever I need to use 3.141593 I simply just type PI in its place. The printf statements may look weird to you, the blank spaces are simply just spaces so it looks more like a table rather than a whole bunch of numbers that are right next to each other. The for loop sets x to 0 and won't stop until it equals 360 incrementing by 15 degrees every loop. This way we will get the sin,cos,and tan every 15 degrees all the way up to 360 degrees. The r=x*PI/180; is the formula to convert degrees to radians, the thing is, with C it can only calculate the sin,cos, and tan in radians not degrees, therefore we have to convert. If you remember back to trig you may remember how tangent is undefined at 90 and 270 degrees, therefore once x is equal to 90 or 270 we want it to print out the sin and cos as it usually would but print out the tangent to be undefined. Otherwise (the else statement) print out the sin,cos, and tan. Remember that the %13.4 means 13 spaces before the number the 4 mean to 4 decimal places. You could just put %.4f but then the numbers would be closer together and it wouldn't look so much like a table There are two keywords that you must know when using loops. These keywords are break and continue. What the keyword break does is break you out of a loop in a special circumstance that you would specify. Here is an example.

int x=1;
while(x<=10)
{
printf("Hello! We are in a loop\n");
if(x==7)
break; /*This is how you use a break*/
x++;
}

The while loop is intended to loop exactly ten times however, the if statement specifies that when x is equal to 7 to break out of the loop. Since this break is here the loop will ultimately only execute 7 times. Breaks can be very useful in times when you want the loop to stop in special circumstances. Continue is another keyword used in looping in special circumstances it is convenient to be able to not execute a certain line or lines of code but not break you out of the loop entirely. Unlike break, continue doesn't take you out of the loop entirely, what continue does is skip the rest of the loop and start from the beginning.