how to stop from looping

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
pythip
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sat Apr 24, 2010 11:25 pm

how to stop from looping

Post by pythip »

I call a function which looks like this, and it keeps looping by resetting itself, this code is also in a function, which has a for loop that sets flip, and once the for loop is done calls checkFlip(), and then does another for loop and checkFlip(). The problem with this code it it sets flip to false and then comes back around somehow and sees that its true

Code: Select all

void checkFlip()
{
	if (flip == false)
	{
		for (int i = 1; i<=7; i+=1)
		{
			flip();
		}
	}
	else if (flip == true)
	{
	flip = false;
	}
}
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: how to stop from looping

Post by Bakkon »

We need to see more code. What scope is 'flip' in?
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: how to stop from looping

Post by X Abstract X »

You didn't post enough code for us to determine the problem. Anyway, I felt the need to simplify the code you did post in an attempt to help. Don't take this the wrong way but your code was a bit overly complicated.

Code: Select all

void checkFlip() {
    if (flip) {
        flip = false;
    } else {
        for (int i = 0; i < 7; i++)
            flip();
    }
}
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: how to stop from looping

Post by avansc »

X Abstract X wrote:You didn't post enough code for us to determine the problem. Anyway, I felt the need to simplify the code you did post in an attempt to help. Don't take this the wrong way but your code was a bit overly complicated.

Code: Select all

void checkFlip() {
    if (flip) {
        flip = false;
    } else {
        for (int i = 0; i < 7; i++)
            flip();
    }
}
you didnt change the code really you just used a else instead of a else if, its no more less complicated, if anything his is easier to read.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: how to stop from looping

Post by X Abstract X »

avansc wrote:
X Abstract X wrote:You didn't post enough code for us to determine the problem. Anyway, I felt the need to simplify the code you did post in an attempt to help. Don't take this the wrong way but your code was a bit overly complicated.

Code: Select all

void checkFlip() {
    if (flip) {
        flip = false;
    } else {
        for (int i = 0; i < 7; i++)
            flip();
    }
}
you didnt change the code really you just used a else instead of a else if, its no more less complicated, if anything his is easier to read.

I'd say his loop is damn hard to read. You could argue that the rest is personal preference but it's bloated in my opinion. Forums are a place for everyone to pitch in opinions and people can take from it what they want, I'm not claiming my preference is gold.
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: how to stop from looping

Post by pritam »

Just from a quick look at your code.

Code: Select all

void checkFlip()
{
   if (flip == false)
   {
      for (int i = 1; i<=7; i+=1)
      {
         flip();
      }
   }
   else if (flip == true)
   {
   flip = false;
   }
}
I think you could go about it like this as well.

Code: Select all

void checkFlip() {
   if (flip == false) {
      for (int i = 1; i<=7; i+=1) {
         flip();
      }
   }
   flip = false;
}
I may be wrong, correct me if I am.
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: how to stop from looping

Post by Amarant »

Is 'flip' supposed to be a function or a variable?

Code: Select all

flip = false; // flip is a variable ?
flip(); // or is it a function...
177
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: how to stop from looping

Post by RyanPridgeon »

Amarant wrote:Is 'flip' supposed to be a function or a variable?

Code: Select all

flip = false; // flip is a variable ?
flip(); // or is it a function...
That's what I thought, I'm amazed that even compiled!
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: how to stop from looping

Post by Ginto8 »

Amarant wrote:Is 'flip' supposed to be a function or a variable?

Code: Select all

flip = false; // flip is a variable ?
flip(); // or is it a function...
The only reason it works is because C++ defines functions not just by their name but also their parameters. func(void) (a function, also written as func())is different from just plain func (the variable).

Anyway, looking at the original problem, you're using mutual recursion without a condition to stop it. We'll need to see the code for flip() as well as checkFlip().
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: how to stop from looping

Post by xiphirx »

You need to provide more code. We need to see where "flip" is modified to true, if it ever is.


Taking a look at the code, flip is quite useless. When you call "checkFlip()" you first see if flip is false, lets just say it is, so the code will start the loop, which will halt your game execution until it is done. I am assuming flip() sets flip to false at the end? So, the sequences goes like this
check flip if false
flip is false
repeat flip() seven times
flip is still false
if flip is true (which it seems it cant be)
set flip to false

Right now, it looks like a function that just starts another function... flip is probably only set to true in your flip() function.

I think what you want, is a counting sequence that will execute along with your overall loop.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
MacJordan
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Fri Oct 22, 2010 4:13 am

Re: how to stop from looping

Post by MacJordan »

pythip wrote:I call a function which looks like this, and it keeps looping by resetting itself, this code is also in a function, which has a for loop that sets flip, and once the for loop is done calls checkFlip(), and then does another for loop and checkFlip(). The problem with this code it it sets flip to false and then comes back around somehow and sees that its true

Code: Select all

void checkFlip()
{
	if (flip == false)
	{
		for (int i = 1; i<=7; i+=1)
		{
			flip();
		}
	}
	else if (flip == true)
	{
	flip = false;
	}
}

if you want to stop the looping you can use the break; statements;
When break statement is call cursor position transfer outside the for loop.
try this statement to stop your for loop.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: how to stop from looping

Post by dandymcgee »

MacJordan wrote: if you want to stop the looping you can use the break; statements;
When break statement is call cursor position transfer outside the for loop.
try this statement to stop your for loop.
Appreciate you trying to help, but please read the last post date before posting. This thread is over 5 months old.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply