Sign in to follow this  

Define loop?

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Congrats [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] I enjoy Python! it's pretty sweet [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

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

[CODE]
while count < 5:
Display "hotdogballoon"

Display "This is out of the loop"
[/CODE]

You could look at it as a teenager as well.

[CODE]
While age < 18:
Display "You can't buy cigs"

Display "Out of the loop"
[/CODE]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote name='arkane7' timestamp='1343929605' post='4965604']
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++
[/quote] 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 :)

Share this post


Link to post
Share on other sites
[quote name='Inuyashakagome16' timestamp='1343929491' post='4965602']
Congrats [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] I enjoy Python! it's pretty sweet [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

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

[CODE]
while count < 5:
Display "hotdogballoon"

Display "This is out of the loop"
[/CODE]

You could look at it as a teenager as well.

[CODE]
While age < 18:
Display "You can't buy cigs"

Display "Out of the loop"
[/CODE]

You are looping till a condition is met no matter what it is.
[/quote] thanks :)

Share this post


Link to post
Share on other sites
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.

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

[u]Python 3:[/u]
[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:
[quote name='program above']
0
1
2
3
4
not looping
[/quote]
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

Share this post


Link to post
Share on other sites

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this