Adventures in Game Development -- Day 15

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.

19 comments:

Pool Fool said...

interesting but again I got confused in all of that

Miyamoto Karyuu said...

i respect u alot for understanding all this o_o

nowbuddy said...

so basically a countdown?

baka1236 said...

looking good m8

happyhacker said...

You can declare your "begin" variable in the for loop, the compiler will optimize this kind of thing anyway.

It's not uncommon to see:

for(int i = 1; i <= 100; i++) ;
for(int i = 0; i < 100; i++) ;
for(int j = 0; j < 50; j++) ;

Jesse Crows said...

right on!!

Xenototh said...

My god... I actually understood this one! :D

Triper said...

I may try it later.

Dave Benzene said...

Great post, only just understood it tho

Jay Reid said...

What happyhacker said :)

Zoupic Music said...

how do you understand any of this :|

Alexis said...

That's pretty cool, I wish I could write things like this. So complicated though!

Desktoper said...

Saving your posts so I can maybe understand someday haha :)

Lt Nite said...

Kinda understand it, but I'm not as smart as you with this, haha!

oinyo said...

Looking good thank you!

baka1236 said...

i can't wait till i start with coding this holiday

FateIT said...

What platform is this wep cracking game going to be on?

Tomas.G said...

Good stuff.

yourminigames.com said...

Interesting stuff

Post a Comment