471,317 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Simple While statement

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;
}

Dec 28 '05 #1
9 1273
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.

Dec 28 '05 #2
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 28 '05 #3
while ((x != xpos) || (y != ypos))
{
xpos = xpos + 1;
ypos = ypos + 1;
}
"Varangian" <of****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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;
}

Dec 28 '05 #4
Varangian <of****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 28 '05 #5
Mattias Sjögren <ma********************@mvps.org> wrote:
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.


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 28 '05 #6
Rahul Goel <ra*********@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 28 '05 #7
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 28 '05 #8
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.

Dec 28 '05 #9
Varangian <of****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 28 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andreas Paasch | last post: by
27 posts views Thread by Brian Sabbey | last post: by
2 posts views Thread by Anurag | last post: by
3 posts views Thread by ChrisHadley | last post: by
5 posts views Thread by Tim::.. | last post: by
7 posts views Thread by Helpful person | last post: by
7 posts views Thread by CSharper | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.