Hi, Im currently implimenting a nested for loop, but there seems to have some known bugs and i dont know whats causing it , how to fix it, etc.
Heres part of my code:

//using some of vector<structs> below:

void Profile()
	int wire = 1;
	int face = 1;
	int i = 0;
	int j = 0;
	int index;
	bool startcheck = true;
	gp_Pnt end; //a sort of struct like, have x, y and z member.
	gp_Pnt2d start; //similar above, but 2 members

	end.SetCoord(0,0,0);//initialize the pnt. ie. set all member values to 0
	//for(int i = 0; i < urange.size();i++)//urange is just a vector struct
	while(i < urange.size())
		if (startcheck == true)//check if is first time entering 
			start.SetCoord( Contour[urange[end.Z()].EdgeStartIndex].u, //save 
			startcheck = false;

		for(int j= 0; j < urange[end.Z()].NbPoints; j++)//the number of time 
		{//this loop loops is varying depending on what is in variable end
			ProfileStore(j, wire, face);//store some information
			if((start.X()==end.X())&&(start.Y()==end.Y()))//test condition
			{//condition:check if two values match.if match, wanna go back to the while loop but with i incremented.
				startcheck == true;
			else if (j == (urange[end.Z()].NbPoints - 1))//the last value
			{//the ProfileFindNextStart(a,b) function will return values that determines how many times the for loop will be looped
				index = urange[end.Z()].EdgeStartIndex + j;
				end = ProfileFindNextStart(Contour[index].u, Contour[index].v);

Im pretty sure i have check the variables are passed correctly,
the problem now is that everything is good when the first time the for loop is entered. but when second time the for loop is entered. THE COUNT OF J IS NOT STARTING FROM ZERO!!! it seems like the j didnt got set.
i.e. for loop looped 4 times when i = 0; gud. nothing wrong with that.
but looped 11-4=7 times when i =1; which is supposed to loop 11 times, but looped 7 times instead and it shows that the count starts from 4 when i was trying to print out to screen.

Anyone know whats causing it? and how to fix it?

Votes + Comments
Code tags on first post! Thank you!
7 Years
Discussion Span
Last Post by VernonDozier

btw, what i intended to do is that when any of the two IF conditions were met, it'll just back to the while loop, but with i incremented by 1.


You have a j variable declared in line 6 and a j variable declared in line 24. These have different scopes. Be very careful with this. You are also passing j to functions outside of what is posted. Check to make sure j isn't passed by reference and that the function isn't changing j. You should either put some debugging statements into your program that displays the value of j (and again, make sure you know WHICH j is you are displaying), or use a debugger and stick some breakpoints. Not having the entire picture, it's hard to speculate what may be causing this. Line 24 will create a NEW j variable and initialize it to 0.

To get a really good idea of what is going on, you must step through the program and display/find out what j is.


Check your line 30, you are comparing or do you want startcheck to become true?

This will check if startcheck equals true:


This will make startcheck become true:


Edited by Reverend Jim: Fixed formatting


poncho, good catch on the = vs. == .

Regarding the code tags, be careful of \ vs. /. Code tags are these:



as opposed to these:



Also note inline code tags:



You can also highlight text, then press either the green icon on the right for inline code or the # icon (third from right) for regular code tags.

Again, good catch on the = vs. == .


thanks for replying. well, its my bad, i was trying to fiddle around with different loops before. i was using two for loops then swapped to two while loops then finally one while and one for....Its just different bugs.
what i intended to do is that when either of the two if conditions were met, they should exit the inner for loop and the second if condition will control how many times to loop when the inner for loop is entered next time.
any suggestions how this should be done? thank you


Thank you for spotting that, didnt' notice, it should be a = only, just readjusting the flag. but that doesnt really fix the problem



Impossible to say beyond what I commented last post. What is definitely NOT going on is this:

THE COUNT OF J IS NOT STARTING FROM ZERO!!! it seems like the j didnt got set.

If the inner for loop is being entered on line 24, j is set to 0. Note again that there are two j variables, one defined on line 6, one on line 24. I don't know if you tried my ideas from my earlier post or not, but you haven't commented on them since.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.