473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1312
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andreas Paasch | last post by:
I'm attempting to trigger things based on time. I have one shop that has opening hours, closing hours and lunch hours store as full hour values, integer, in MySQL. I retrieve them, based on...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
6
by: SB | last post by:
This while loop keeps repeating even when a correct character is entered.... cout<<endl<<"What day would you like to schedule the appointment?"<<endl; cout<<endl<<"Enter 'M' for Monday, 'T' for...
2
by: Anurag | last post by:
This simple one beats me all ends up(sincerely). I have been doing DB2 UDB for some time now, reading a lot of good discussions in this forum, writing some answers, asking a lot more but this...
3
by: ChrisHadley | last post by:
My simple sortheap configuration question is: the sortheap parameter specifies the amount of memory used by agents for sorts. Is this value the total for all agents or for each? Sort heap...
5
by: Tim::.. | last post by:
Can someone tell me how I convert this simple SQL statement so I can use it in ASP.NET??? I have an issue with the quotation marks and wondered if there is a simple rule for converting the sql...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
7
by: Helpful person | last post by:
I am new to Javascript and have a fairly straightforward question. I am trying to use an image as a link to open a new page with the onmouseclick event. In general this seems to work fine with...
1
by: Rahul Babbar | last post by:
Hi, I ran the scripts in a file from Command Line Processor and it gave the error for all the constraints being added, but not the indexes being added. For a simple statement like Alter...
7
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.