All times are UTC - 6 hours




Post new topic This topic is locked, you cannot edit posts or make further replies.   Page 1 of 1
 [ 5 posts ] 
Author Message

 Post subject: Do/While loop oddity in C#.
PostPosted: October 11th, 2011, 7:36 am 
I post here sometimes
Champion of Saradomin
Champion of Saradomin
User avatar

Joined: September 20th, 2006, 10:25 am
Posts: 5,106
Location: The pub
Gender: Male
Status: Offline

Donor: Prince (2009)
I have a do/while loop constructed in C# as below:
do
{
    some code;
}
while(variable1 !=0 && variable 2 != 0 && variable 3 !=0);


Now, in my head, that means "Do some code while variables 1,2 and 3 all do not equal 0" AKA finish the do/while loop when all the values reach zero.

This doesn't work, and instead quits the do/while loop after a single iteration when values are not 0. It works as intended if i use OR (||) operators, but not with AND (&&) operators.

WTF?

__________________
There is a village, which is hidden in the shadow of a mountain.
Everybody is suffering from a lack of light.
One day, the eldest of this village leaves for the mountain with a teaspoon in his hands.
The others ask him what he intends to do.
He replies that he is going to move the mountain.
"But you will never succeed!" they cry out.
"No, i will never succeed, but somebody has to start."

~~~The Wising Up Song - Misty's Big Adventure

Hidden: 
Kikori wrote:
Runevillage the forum is pretty well much done. Runevillage the group will eventually die, but as long as we're still friends with each other in the end, it's still a pretty big thought in our heads. Runevillage the family is forever, no matter where we settle down. If absolutely nothing else ever comes of Runevillage in the future, that alone is a pretty damn awesome thought.


Last edited by The123king on October 13th, 2011, 3:32 am, edited 1 time in total.
Top
 Profile
 

 Post subject: Re: Do/While loop oddity in C#.
PostPosted: October 11th, 2011, 11:38 am 
/jəˈʁun/
Village Legend
Village Legend
User avatar

Joined: September 18th, 2004, 12:20 pm
Posts: 6,358
Location: Holland
Gender: Male
Status: Offline

Donor: Knight (2013)
I'm guessing one of your variables equals 0? if the code is as you wrote it above (assuming you meant != and not 1= above) it would then indeed do one iteration of the block and then quit it

__________________
Image


Top
 Profile
 

 Post subject: Re: Do/While loop oddity in C#.
PostPosted: October 15th, 2011, 3:31 pm 
Sorceror of Saradomin
Sorceror of Saradomin
User avatar

Joined: June 27th, 2004, 10:07 am
Posts: 4,727
Location: East-Flanders, Belgium
Gender: Male
Status: Offline
while(variable1 !=0 || variable 2 != 0 || variable 3 !=0)


The above loops until all variables are 0.
It basically means "if even one of them isn't zero, continue".

Your loop means "If all of them are not zero, continue".

__________________
Image


Top
 Profile WWW 
 

 Post subject: Re: Do/While loop oddity in C#.
PostPosted: October 16th, 2011, 2:52 am 
I post here sometimes
Champion of Saradomin
Champion of Saradomin
User avatar

Joined: September 20th, 2006, 10:25 am
Posts: 5,106
Location: The pub
Gender: Male
Status: Offline

Donor: Prince (2009)
Ah, i see now. Seems to be a case of PEBKAC (Problem Exists Between Keyboard And Chair)

You can close this thread...

__________________
There is a village, which is hidden in the shadow of a mountain.
Everybody is suffering from a lack of light.
One day, the eldest of this village leaves for the mountain with a teaspoon in his hands.
The others ask him what he intends to do.
He replies that he is going to move the mountain.
"But you will never succeed!" they cry out.
"No, i will never succeed, but somebody has to start."

~~~The Wising Up Song - Misty's Big Adventure

Hidden: 
Kikori wrote:
Runevillage the forum is pretty well much done. Runevillage the group will eventually die, but as long as we're still friends with each other in the end, it's still a pretty big thought in our heads. Runevillage the family is forever, no matter where we settle down. If absolutely nothing else ever comes of Runevillage in the future, that alone is a pretty damn awesome thought.


Top
 Profile
 

 Post subject: Re: Do/While loop oddity in C#.
PostPosted: October 16th, 2011, 6:51 am 
RV's Thespian
Village Elder
Village Elder
User avatar

Joined: July 5th, 2003, 12:24 pm
Posts: 9,749
Location: South Dakota
Gender: Male
Status: Offline

Donor: Guardian (2012)
Locked by request.

__________________
Image
Awesome sig made by Goten!


Top
 Profile
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  Page 1 of 1
 [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 32 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
The Village and this web site are © 2002-2012

ThePub 2.0 - Designed by Goten & Jackstick. Coded by Glodenox & Henner.
With many thanks to the Website Team!