Trouble Understanding x++ and y++ Coding

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
PrecentralGyrus
Posts: 10
Joined: April 13th, 2013, 6:04 pm

Trouble Understanding x++ and y++ Coding

Post by PrecentralGyrus » April 22nd, 2013, 11:13 pm

Hey guys,

I know I just posted a post asking for help on a different subject, but I've narrowed down what I am confused about. I don't fully understand how the x++ and y++ commands work. Here is example coding:

Code: Select all

int y = boxY;
	while( y < boxY + boxWidth )
	{
		int x = boxX;
		while( x < boxX + boxWidth )
		{
			gfx.PutPixel( x,y,255,255,255 );
			x++;
		}
		y++;
	}
}

This creates a square, and I understand how everything works, except how the x++ and y++ commands make the entire square. With this coding I would expect the computer to draw a line from the PutPixel coordinates right, along the x axis, until it hits boxX + boxWidth, and draw a line down, along the y axis, until it hits boxY + boxWidth, and not fill in the rest of the square.
How is the rest of the square filled in with this simple command?

Thanks!

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Trouble Understanding x++ and y++ Coding

Post by cameron » April 22nd, 2013, 11:50 pm

x++ means x = x + 1; and y++ means y = y + 1;
Computer too slow? Consider running a VM on your toaster.

PrecentralGyrus
Posts: 10
Joined: April 13th, 2013, 6:04 pm

Re: Trouble Understanding x++ and y++ Coding

Post by PrecentralGyrus » April 23rd, 2013, 12:08 am

cameron wrote:x++ means x = x + 1; and y++ means y = y + 1;
Yeah, but I don't see how that fills in an entire square. With that coding, shouldn't it just create two lines from the PutPixel coordinates on the x and y axis's, instead of filling in an entire square?

Freeman
Posts: 11
Joined: July 31st, 2012, 11:36 am

Re: Trouble Understanding x++ and y++ Coding

Post by Freeman » April 23rd, 2013, 7:12 am

what youre having trouble understanding is not the x++ and y++ commands, but rather the fact that the loops are nested. so whats basically happening is your inner loop always creates a single horizontal line ( read: place dots in a row until while criterion is met ), once it has reached the end, its told to move one space down and reset ( i.e. run through the first loop AGAIN, just with a different starting point ), this keeps on going until your 2nd while criterion is met

PrecentralGyrus
Posts: 10
Joined: April 13th, 2013, 6:04 pm

Re: Trouble Understanding x++ and y++ Coding

Post by PrecentralGyrus » April 23rd, 2013, 12:40 pm

Freeman wrote:what youre having trouble understanding is not the x++ and y++ commands, but rather the fact that the loops are nested. so whats basically happening is your inner loop always creates a single horizontal line ( read: place dots in a row until while criterion is met ), once it has reached the end, its told to move one space down and reset ( i.e. run through the first loop AGAIN, just with a different starting point ), this keeps on going until your 2nd while criterion is met
Ah ok, I understand now. Thanks!

User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Re: Trouble Understanding x++ and y++ Coding

Post by DreamBliss » May 16th, 2013, 2:17 am

There is also ++x and ++y to confuse you even more...
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble Understanding x++ and y++ Coding

Post by albinopapa » May 17th, 2013, 1:54 am

DreamBliss wrote:There is also ++x and ++y to confuse you even more...
...and what does that do?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
DreamBliss
Posts: 30
Joined: March 27th, 2013, 11:48 pm
Location: Lost on the Way
Contact:

Re: Trouble Understanding x++ and y++ Coding

Post by DreamBliss » May 17th, 2013, 7:54 pm

It, as far as I understand, pre-incriments x and y. So in the case of x++ what is really being said is x = x + 1. This increments x by 1, after this bit of code is executed.

But in the case of ++x I guess it's like saying x + 1 = x. It increments it first and asks questions later.

I hope that clarifies things.

I only know about this because of the book, "Exploring C++", which I got as my introductory/review text for C++. The author describes himself as a, "professional stunt programmer."

:D
You create reality in your mind,
Before experiencing it as reality,
So if you want to change something,
First change what you think about it.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Trouble Understanding x++ and y++ Coding

Post by albinopapa » May 22nd, 2013, 4:32 am

I had to look up examples but now I get it.

If you have y = x++ and y = ++x you get different values for y.
The former returns what x was before adding 1 and the latter returns x after adding 1.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Trouble Understanding x++ and y++ Coding

Post by LuX » May 22nd, 2013, 4:44 pm

http://www.planetchili.net/forum/viewto ... 361&p=4098

Scroll a few posts down. There's a huge ass post explaining the loop stuff.
ʕ •ᴥ•ʔ

Post Reply