Looping in C++

How does it work and why is it important?

Page 1 of 1

6 Replies - 1231 Views - Last Post: 03 March 2010 - 01:03 PM Rate Topic: -----

#1 Soap360  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-March 10

Looping in C++

Posted 03 March 2010 - 12:15 PM

Im a begginer in C++ and I get everything up until looping. Maybe it seems so easy that Im only challenging myself (very unlikely hence i only started C++ a month ago) or Im just not getting something. So here are my following questions:

1. Why is Looping Important?
2. Is it essential in video game design?(think world of warcraft)
3. How does it work?
Is This A Good Question/Topic? 0
  • +

Replies To: Looping in C++

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Looping in C++

Posted 03 March 2010 - 12:20 PM

In my opinion, loops along with arrays, are the most important things in C/C++. If you want to remain at the stage of computing the sum between two numbers, then you can forget about loops. If you, however, want to move further, then you have to understand how this thing works. This is a good tutorial to get you started: http://www.dreaminco...showtopic=13919 , although there are more tutorials & code examples on Google (or your search engine), if you want.

2. Is it essential in video game design?(think world of warcraft)
You have got to be kidding me. I think that you already know the answer to this question.

This post has been edited by sarmanu: 03 March 2010 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Looping in C++

Posted 03 March 2010 - 12:33 PM

Well what are your thoughts on this?

#1. Why do you think people use loops?
#2. How do you suppose a game figures out what a user is doing if it does not use a loop?


well there are a number of different structures used to create loops:

  • line-label + goto: not used very much any more (generally people prefer structured programming)
  • for-loop -- classic loop used for counting or "iterating"
  • while loop -- another classic loop, used to repleat a block of code until some condition is met.
  • do-while loop -- like a while-loop but the body of the loop always happens at least once.
  • recursion -- a bit more complicated but basically it is a function that calls itself.

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Looping in C++

Posted 03 March 2010 - 12:36 PM

let's take a very basic example.
suppose you want to print the same messege "Hello World" 100 times.
you could write 100 cout statements to print that messege. Not very neat and efficient...
However, if you use a loop to iterate 100 times, and each iteration print this messege, it would look like: (using a for loop)
for(int times = 0; times < 100; times++){
   cout<<"hello World\n";
}

thats it. 100 "hello World" were printed :)
now, loops are used in much more advanced programs and code, and of course in games.
suppose you have one player and 200 enemies.
how will you check which enemy is dead? attaks? moves? check for each monster individually? but every time a new monster is created or dies. how would you know the number of statements to write?
that is just one small example regarding an RPG game..

using loops is extremely useful.
follow sarmanu's link and search for further information.
looping is the basics.
you should and must master loops in order to be a good programmer.
Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Looping in C++

Posted 03 March 2010 - 12:40 PM

Hi Soap360, welcome to Dreamincode! You will very quickly learn as sarmanu says, loops are very important to us. Imagine a world where loops did not exist. Let's take a simple example. Imagine you have a bowl containing marbles and an empty bag that is closed. You are asked to open the bag, take a marble out of the bowl one at a time and place it into the bag. When all the marbles have been taken out of the bowl, close the bag and go find your friends.

Now lets say you have 10 marbles in that bowl.

So, here are your instructions:

Open the bag
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Take one marble out of the bowl and place into the bag.
Close the bag and go find your friends.

That was easy to do wasn't it.

Now try with 10,000,000 marbles and that would be a waste of Dreamincode server space and I would probably find my marbles well and truly severed from my personage.

To overcome this problem we introduce the loop.

What you would say is:-

Open the bag
If there are marbles in the bowl then
Take one marble out of the bowl and place into the bag
Repeat the previous step until the bowl is empty
Close the bag and go find your friends.

There, we have a simple loop that can work for no marbles up to and including an infinite number of marbles.

In computer programming, we do not obviously have marbles, but we process data in the form of strings and numbers. They are massaged with loops to produce the output that is desired.

Hope that helps you understand the loop and it's purpose.

This post has been edited by Martyn.Rae: 03 March 2010 - 12:44 PM

Was This Post Helpful? 1
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Looping in C++

Posted 03 March 2010 - 12:58 PM

As games must be updated (Graphics, sound, input etc...), they use a main update loop. From this main loop, the entire program is constantly updated with new values rather than just entering, drawing one frame, then quiting. Can you imagine the amount of code you would have to write if you couldnt use loops? You would have to write an entire program for every one single frame of the game.
Was This Post Helpful? 0
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Looping in C++

Posted 03 March 2010 - 01:03 PM

Read up on the various types of loops, and some more C++ skill will let you do a whole lot of stuff, power over the machine. Yes, do thy bidding!

look into arrays too. I'm sure a google search will give some examples for making a file copy program (a good start, in my opinion).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1