Loops in C

Loops, we all need to use them, especially on robots. Without a loop, our robot would execute a program once, then stop. Often, that cycle is way too fast for anyone to notice. In this tutorial, I will explain the basics of three kinds of loops; the FOR Loop, the WHILE Loop, and the DO WHILE Loop.

The FOR Loop

The first loop we shall look at is the FOR loop. This loop is –in my opinion– the most useful type of loop. Why? Because I find it relatively easy to change, and it is one of the more versatile forms of loops. Here is the basic structure:

for (initiate variable; declare condition; update)

{

do stuff;

}

To initiate a variable, either declare one on the spot (int i;) or,  just insert the name of one that you have already declared. The condition determines until when it will loop. So if the condition is “i > 50” then it will loop until i is less than 50, compris?

To update the variable, you need to indicate a change in the value of the variable (ie. i++ to add 1 each time, i = i+5 to add 5 each time, i — to subtract 1 each time, and you get the idea). This is what will ensure your variable WILL END…or not…depending on how you update it…

So now, say we want to blink an LED 100 times. What do we do?

for (int i; i < 100; i++) //setup to loop 100 times

{

LEDon;            // turn on LED for 500ms

delay_ms(500);

LEDoff;           //then turn it off for 500ms

delay_ms(500);

//repeat loop until i >= 100

The WHILE Loop

Next is the WHILE loop. These loops also have many uses as well, and are commonly used for infinite loops…that is, loops that never end. The basic format for the WHILE loop is as follows

while(declare condition)

{

DO STUFF!;

}

It is really easy looking in comparison to the FOR loop. The code equivalency to the FOR loop flashing LED would be the following:

int i=0;
while(i < 100) //loop when i<100

{

LEDon;            // turn on LED for 500ms

delay_ms(500);

LEDoff;           //then turn it off for 500ms

delay_ms(500);

i++;  //add 1 to i each time

//repeat loop until i >= 100

So obviously, the code won’t look as tidy as the FOR loop. But that’s fine, if you prefer WHILE loops…

To do an infinite loop, type the following code:

while(1) //this does not have to be 1, but it can be 1 = 1, 2 = 2, ect..ect…

{

insert custom code here;

}

The only problem with this loop is that it is possible for it to never happen if you use a variable in the condition.

For instance:

int i = 49;

while(i<40)

{

do stuff!; //but it will never happen!

}

Whoa! What happened here?

i never equals a value less than 40. Therefore, this loop never happens! Congratulations, you’ve just written a program that can do nothing.

So do you understand WHILE loops? ok, good, time for DO WHILE loops…

The DO WHILE LOOP

Remember how WHILE loops have a chance of never being looped? Well, here comes the DO WHILE loop. Unlike the previous, DO WHILE actually has a chance to show itself before the condition is ever looked at. Here’s the general structure of the DO WHILE loop:

do {

DO STUFF!;

} while(declare condition)

See what I mean? The program doesn’t check if the condition is true until it cycles the code at least once! If you want the equivalency to the flashing LED code, look below:

do {

LEDon;            // turn on LED for 500ms

delay_ms(500);

LEDoff;           //then turn it off for 500ms

delay_ms(500);

i++;  //add 1 to i each time

} while(i < 100) //repeat if i is less than 100

I suppose you will also want to know the infinite loop as well. Here it is:

do {

DO STUFF!;

} while(1) //repeat forever

There, that’s all. This is just the basic stuff, but I think it covers what you may need for starters. If you want to know more info, just Google!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: