Simple While statement

  • Thread starter Thread starter Varangian
  • Start date Start date
V

Varangian

Hi again,

I have this simple peace of code... When xpos or ypos reaches x or y
the loop stops. My question is the "while" doesn't support && or & ? If
thats the case Imo its a big flaw. I thought I was doing it incorrectly
so I simplified it with an if statement and it work.

int x = 5
int x = 6

int xpos = 0;
int ypos = 0;

while (x != xpos && y != ypos)
{
xpos = xpos + 1;
ypos = ypos + 1;
}
 
What are you trying to achieve?

The while loop will continue until the entire expression evaluates to
false, which will happen in this case when xpos = 5.

I cut and pasted the code and after changing int x=6 to int y=6 (x is
defined twice) the code ran as I would have expected.
 
When xpos or ypos reaches x or y the loop stops.

If that's the behavior you want you should use the || operator rather
than &&. Right now it stops if/when xpos is x AND ypos is y.

My question is the "while" doesn't support && or & ?

It supports any expression that evaluates to a bool.


Mattias
 
Varangian said:
I have this simple peace of code... When xpos or ypos reaches x or y
the loop stops.

And that's exactly what your code does. Here's your code in a short but
complete program:

using System;

public class Test
{
static void Main(string[] args )
{
int x = 5;
int y = 6;

int xpos = 0;
int ypos = 0;

while (x != xpos && y != ypos)
{
xpos = xpos + 1;
ypos = ypos + 1;
Console.WriteLine("xpos={0}; ypos={1}",
xpos, ypos);
}
}
}

The output is:
xpos=1; ypos=1
xpos=2; ypos=2
xpos=3; ypos=3
xpos=4; ypos=4
xpos=5; ypos=5

Now, that's what I'd expect - it conforms exactly with what you said
you wanted. If you'd expected something different, please say what
you'd expected.
 
Mattias Sjögren said:
If that's the behavior you want you should use the || operator rather
than &&. Right now it stops if/when xpos is x AND ypos is y.

Nope - he's actually got the right operator. It *continues* while x is
not xpos *and* y is not ypos - in other words, it *stops* when x is
xpos *or* y is ypos.

It does exactly what he says he wants...
 
Rahul Goel said:
while ((x != xpos) || (y != ypos))
{
xpos = xpos + 1;
ypos = ypos + 1;
}

See my response to Mattias - that would stop when xpos reaches x *and*
ypos reaches y.
 
Nope - he's actually got the right operator. It *continues* while x is
not xpos *and* y is not ypos - in other words, it *stops* when x is
xpos *or* y is ypos.

It does exactly what he says he wants...

Oops, you're right of course. Don't know what I was thinking, must be
a bit rusty after the holidays :) Thanks.


Mattias
 
Thanks to you all, seems to be working now. I used Rahul Goel. as the
&& I thought it means a value AND value must be true to exit the loop.
the || or OR means either one or the other.

I wanted that the loop exists when the xpos become equal to x and ypos
becomes equal to y, not when only one of them becomes equal. Now
working fine.
 
Varangian said:
Thanks to you all, seems to be working now. I used Rahul Goel. as the
&& I thought it means a value AND value must be true to exit the loop.
the || or OR means either one or the other.

I wanted that the loop exists when the xpos become equal to x and ypos
becomes equal to y, not when only one of them becomes equal. Now
working fine.

Just to avoid you getting confused in the future - && does mean "and",
and || does mean "or", but the condition is meant to signify what has
to be true to keep going, not to stop.
 
Back
Top