Pages

Today was all about the for loop. In my previous code examples I showed how I was simulating a download progress bar in the command prompt, but it wasn't very efficient. This has been solved with the use of the for loop. Here's the new code using the for loop:

cout << "Beginning download" << endl;

int begin = 1;

for(begin; begin <=100; begin++)

{

cout << begin << endl;

Sleep(200);

}

Basically, this loop is given three commands. The first command tells it that the integer "begin" is being used. The second command tells it to display what is inside the loop while it is less than or equal to 100. The final statement tells the program to increment each time the loop is passed. Inside the loop, the integer "begin" is displayed, and the compiler sleeps for two milliseconds in between each display.

Once compiled and run, this loop displays the numbers 1 through 100, with a two millisecond delay in between.