View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Define loop?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1makronik  Members

Posted 02 August 2012 - 11:38 AM

Hello. I recently started learning c++, though I decided to start python first in order to understand what is programming etc. in order to do that, can someone briefly explain with is a "loop" and give a situation and a example so that I understand it in basic terms. thanks

### #2Inuyashakagome16  Members

Posted 02 August 2012 - 11:44 AM

Congrats I enjoy Python! it's pretty sweet

To me, a loop is obviously doing the same tasks over and over till a certain condition is met.

while count < 5:
Display "hotdogballoon"

Display "This is out of the loop"


You could look at it as a teenager as well.

While age < 18:

Display "Out of the loop"


You are looping till a condition is met no matter what it is.

Edited by Inuyashakagome16, 02 August 2012 - 11:46 AM.

### #3arkane7  Members

Posted 02 August 2012 - 11:46 AM

in basic terms a loop is a conditional setting for code within in the loop to continue to repeat execution.

say you want to count the number of 1's in an array (an array is just a simple "list" of data in successive memory)

int [50] myNum = { 1,2, 1, 3 1, ..... //fill in the rest with whatever you want}'

now lets count the number of 1's

int numOf1 = 0;
int indexOfArray = 0;
while (indexOfArray != 50) // this is a condition to stop the loop. when indexOfArray equals 50, the repeition of the following will stop
//everything in the brackets will be repeated as long as the above condition is true
{
if (myNum[indexOfArray] == 1)
numOf1++; //this counts the number of 1s

indexOfArray++; //this allows you to go to the next element in the array;
}

now it will first start off with both variables at 0; then it will run the code in the middle. indexOfArray will only be at 1 after the first run through, so indexOfArray !=50 will be true.

this will repeat the inner code 50 times, updating the variable everyloop; indexOfArray increments EVERY loop. while numOf1 only increments when it finds a 1 inside the array

if you just did this

while (true) {} this will run FOREVER because it is always true.
while (false) {...} this will NEVER run becuase it is always false

another example
index = 0;
while (myNum [index] != 1)
{ index++;}

now it will only stop when you find a 1 inside the myNum array. if there is no 1 it will run forever

sorry for not knowing python this is in C++

Edited by arkane7, 02 August 2012 - 11:51 AM.

Always improve, never quit.

### #4makronik  Members

Posted 02 August 2012 - 11:53 AM

in basic terms a loop is a conditional setting for code within in the loop to continue to repeat execution.

say you want to count the number of 1's in an array (an array is just a simple "list" of data in successive memory)

int [50] myNum = { 1,2, 1, 3 1, ..... //fill in the rest with whatever you want}'

now lets count the number of 1's

int numOf1 = 0;
int indexOfArray = 0;
while (indexOfArray != 50) // this is a condition to stop the loop. when indexOfArray equals 50, the repeition of the following will stop
//everything in the brackets will be repeated as long as the above condition is true
{
if (myNum[indexOfArray] == 1)
numOf1++; //this counts the number of 1s

indexOfArray++; //this allows you to go to the next element in the array;
}

now it will first start off with both variables at 0; then it will run the code in the middle. indexOfArray will only be at 1 after the first run through, so indexOfArray !=50 will be true.

this will repeat the inner code 50 times, updating the variable everyloop; indexOfArray increments EVERY loop. while numOf1 only increments when it finds a 1 inside the array

if you just did this

while (true) {} this will run FOREVER because it is always true.
while (false) {...} this will NEVER run becuase it is always false

another example
index = 0;
while (myNum [index] != 1)
{ index++;}

now it will only stop when you find a 1 inside the myNum array. if there is no 1 it will run forever

sorry for not knowing python this is in C++

I'll start this once I done couple of tuts etc ;) at the moment I am just printing and use if , else statements . thanks for the info

### #5makronik  Members

Posted 02 August 2012 - 11:54 AM

Congrats I enjoy Python! it's pretty sweet

To me, a loop is obviously doing the same tasks over and over till a certain condition is met.

while count < 5:
Display "hotdogballoon"

Display "This is out of the loop"


You could look at it as a teenager as well.

While age < 18:

Display "Out of the loop"


You are looping till a condition is met no matter what it is.

thanks

### #66677  Members

Posted 02 August 2012 - 01:25 PM

Surprised he showed a while loop rather than a for loop, the for was always a bit easier to understand first time round for me.

Python 2:
[source lang="python"]for count in range(0,5): print countprint "not looping"[/source]

Python 3:
[source lang="python"]for count in range(0,5): print(count)print('not looping')[/source]

Anyway, what happens here is when the program hits the "for count in range(x,y,step)" line it creates a new variable called count. This variable will have the value x (usually 0 or 1 although some programs you will have need for other numbers)
The program will then run the indented code which in this case is print count.
When it hits the end of the indented code block it adds the step value to count, you don't need to worry about step usually, if you leave it out (as I have above) python assumes its 1 instead. So when count was 0, it hits the end of the indent and adds 1 to count so count now = 1.
Python then checks to see if count is equal to y (in our case 5), if count is less than y then we loop again. If count is equal to y or greater than y (funny step values can cause that) then we don't loop anymore.

So the above program that we had with x and y as 0 and 5 respectively when run will output the following:

0
1
2
3
4
not looping

Count starts off with a value of 0, we print that to the screen.
Then the loop automatically adds 1 to count. The loop automatically checks count is less than our y value and because it is we loop again.
Then when count eventually gets assigned a value of 5 the loop stops looping and we go onto the next line of code which outputs the text not looping.

I didn't know if you use python 2 or 3 so I gave both versions. Top is 2, bottom is 3.

Edited by 6677, 02 August 2012 - 01:25 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.