Loops n Strings n Things - Part 3

Released 19/9-2002

Written by VerticalE

 Required reading: Hello DNA!, Variables Explained - Part 1 & 2, Loops n String n Things - Part 1 & 2


Now that you are already familiar with the for-loop, lets take a look at the while-loop. It's really simple, and if you understood the for-loop examples, then you will not have any problem understanding this one. Below you will find a code sample that will output the text "Polarbears are cooler than grizzlys!" indefinately.
 

/* w00t to polarbears */

#include <stdio.h>

int main()
{
bool loop = true;

while(loop == true)
{
printf("Polarbears are cooler than grizzlys!\n");
}

return 0;
}

Since you already know everything besides the "while(loop == true)" sentence, we are gonna jump straight to that and explain it in detail. Read the sentence out loud, and I think you'll get the point; "while loop is true". We can add a few words here and there, and it will make the sentence more readable; "loop the code in the codeblock while the loop variable has the value of true". As long as the statement in the parantheses next to while is true, the codeblock will run.

When creating while loops, remember that the code will only run if the statement is true. That does not mean that the value of the variable in the statement have to be "true". The following alteration to the code explains what I mean:
 

/* w00t to polarbears */

#include <stdio.h>

int main()
{
int loop = 12345;

while(loop == 12345)
{
printf("Polarbears are cooler than grizzlys!\n");
}

return 0;
}

As you can see, the statement "loop == 12345" is true when the while-loop starts. In fact, it will loop forever, as the value of the loop variable never changes in the source. So how do you stop the loop from looping? That's right, make the while-statement false! Examine the following example:
 

/* w00t to polarbears */

#include <stdio.h>

int main()
{
int loop = 0;

while(loop<10)
{
printf("Polarbears are cooler than grizzlys!\n");
loop++;
}

return 0;
}

You start by saying "loop this codeblock for as long as the value of the variable loop is less than 10. With each time the loop loops, the loop value gets incremented, until it reaches the value of 10. Then, the while-loop breaks, and the application continues to the next pieces of code - in this case by quitting the application and returning the value 0.

There is also another variant of the while-loop, called a do-while loop. The difference is that it does not check if any statement is true before it run, until the codeblock has been run once. Then, it checks if the statement is true, and if it is, the codeblock is run once more. Below is yet an example:
 

/* w00t to polarbears */

#include <stdio.h>

int main()
{
int loop = 12345;

do{
printf("Polarbears are cooler than grizzlys!\n");
}while(loop == 67890)

return 0;
}

The above example will only run once. If the value of the variable loop was 12345, the loop would run indefinately.

That ends the "Loops n Strings n Things" articles. Remember that the best way to learn this is compiling and playing around with the code examples. Good luck!