473,605 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1322
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.c om> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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.c om> 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.WriteLi ne("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.co m>
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.o rg> 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.co m>
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*********@gm ail.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.co m>
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.c om> 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.co m>
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
1729
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 comparisons either shop is open, about to close or actually closed. When open, nothing particular is written, when closed, customer is informed orders can't be executed, shop is closed. But the last one, right about 15 minutes before closing time,...
27
2365
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. It is available at: http://staff.washington.edu/sabbey/py_do Good background on thunks can be found in ref. . Simple Thunks
6
1487
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 Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; while (day != "M" || day != "m" || day != "T" || day != "t" || day != "W" || day != "w" || day != "H" || day != "h" || day != "F" || day != "f")
2
1810
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 simple question has floored me completely. Either I pretend that I know or I could let the kowledgable ones pass on some wisdom to me :-) When we say that: (1) "CLP in Db2 V8.2 still imposes a limit of 64K on the stored proc size;
3
1938
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 thres for shared sorts (4KB) (SHEAPTHRES_SHR) = (SHEAPTHRES) Sort list heap (4KB) (SORTHEAP) = 3000 SQL statement heap (4KB) (STMTHEAP) = 3000
5
1728
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 statement so if can be used in ASP.NEt! Thanks ... SQL String
14
5893
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
1655
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 the open statement. I wish to use the same script at various places in my web and hence wish to pass to the javascript function the URL location and the width and height of the new page. I am having no luck trying to get the open statement to...
1
2148
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 table A add constraint A_const foreign key(col1) references B(col2);
7
1803
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 the sql stored proc and I for one consider to have most of the business logic in the C# code (specifically in a controller class of MVC). These are my points to them. 1. Our application depends a lot on the data and we have huge amount of data...
0
8001
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8415
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.