Monday, December 13, 2010

csci135c3.cpp

This is the second kind of loops, the while loops. In this tutorial, I am also going to demonstrate a variation of the while loop, the do-while loop.
/*
 *  csci135c3.cpp
 *  This program shows the useage of the while loop.
 *
 */

#include <iostream>
using namespace std;

int main()
{
   int counter = 5; // our integer counter variable
   cout << "While loop #1, while counter > 0" << endl;
   while (counter > 0)
   {
      cout << "Counter is now: " << counter << endl;
      counter--;
   }
   
   counter = 10; // reset counter back to 10
   cout << endl; // just for the display, this skip one line 
   
   cout << "Do While loop, while counter > 0" << endl;
   do
   {
      cout << "It still run one time although it does not satisific the "
      << "condition counter > 10 " << endl;
      counter--;
   } while (counter > 10);
   
   return 0;
}
The format of the loop is like this:
while (condition)
{
   // do something here
   // some kind of counter adjustment
}
The concept of the while loop is just like how it sounds, while condition is true, do the execution in the block. But unlike the for loop, the counter adjusting is do in the loop. See the example, line 17 is counter--. Of course, you can increment as well. At this point, you might be asking: “What happen if we do not know in advance how many times we need to run?” This is not true, because we can use variable in the condition. You can do something like this.
while (counter > whereToStop)
{
   // do something here
}
However, something important need to be clear. Be careful that your loop, whether it is a for loop or while loop, it is not an infinite loop. An infinite loop is a loop that will not terminate, something it is because the condition is always possible to satisfy, or more frequently, remember to include the counter adjustment. Below are some examples that show what possible mistake one can make.
int counter = 0;
while (counter < 10)
{
   // No decrement or increment, it won't be ever greater than or equal
   // to 10. So it will be an infinite loop.
}

int counter = 10;
while (counter > 10)
{
   // the counter is going the opposite direction, and it will
   // never be greater than 10. This is an infinite loop.
   counter--;
}
Lastly, there are times when you do want the loop be infinite. I remember it the canvas GUI program, the screen requires to be refreshed until the user click the close button. You do not need to use the above examples, you can simply do the follow.
while (true)
{
   // do something here
}
This is a very well know way to create an infinite loop, all the programmers understand this is a loop that wanted to be run forever. There are some usage of this, just take know of such a thing.

Finally, let’s look at the do while loop at the last part. This loop is interesting because, even though the condition of the loop does not satisfy, it will still run for the first time. Let me give you a real world example, let say everyone can get a free cookies from the school’s café, and you can get more if you have a ticket, each ticket you have allow you to get one more cookie. So everyone get to the school café, the loop in this case, will get to run once.
int ticket = 5; // the amount of cookie tickets they have
do 
{
   cout << "Give you a piece of cookie. " << endl;
   ticket--;
} while (ticket >= 0);
Note, this case the student will get 6 pieces of cookies. The first one that everybody gets just by showing up, and then 5 more cookies (from the exchange of tickets). There are many situations too, this is just one of the example I come up with. And plus who doesn’t like cookie? :9
Note: This example has some problems, edit or remove before publish. 

Practice Problem

Write a program that will count from 0 to 10. The output should be like the following.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

No comments:

Post a Comment