Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

While Loop bug?

Tex Evans
Registered User
Join date: 17 Oct 2006
Posts: 5
11-19-2008 19:22
My understanding of While loops is that they won't run the code block if the condition is false (i.e. test condition first, then run code block if true)

But the do...while loop will always run the code block at least once (i.e, run code, then test)

So why does this code not work correctly?

default
{
state_entry()
{
llSay(0, "Hello, Avatar!";);
}

touch_start(integer total_number)
{

integer intLowerValue = 4;
integer intHigherValue = 10;

while(intLowerValue > intHigherValue);
{
llOwnerSay("Why did this loop run? Condition is false";);

}
}
}

Am I missing something or is http://wiki.secondlife.com/wiki/While incorrect?
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
11-19-2008 19:26
From: Tex Evans
while(intLowerValue > intHigherValue);

while(intLowerValue > intHigherValue);
(intLowerValue > intHigherValue);
intHigherValue);
;
_____________________
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
11-19-2008 19:35
/me takes peoples semicolons away!

edit: any time you see a line of code followed by some {}'s, that first line of code probably should not have a ; at the end of it..

Try:
while(intLowerValue > intHigherValue)
{
llOwnerSay("Why did this loop run? Condition is false";);
}

What your code is saying is actually something like:

while(intLowerValue > intHigherValue)
{
}

llOwnerSay("Why did this loop run? Condition is false";);
Viktoria Dovgal
Join date: 29 Jul 2007
Posts: 3,593
11-19-2008 19:41
1TBS FTW.
Tex Evans
Registered User
Join date: 17 Oct 2006
Posts: 5
11-19-2008 19:50
Uggg, I was pulling my hair out trying to figure out why the code didn't work. Stupid semi-colons.

Thanks for showing me the light
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-19-2008 19:54
From: Tex Evans
Uggg, I was pulling my hair out trying to figure out why the code didn't work. Stupid semi-colons.

Thanks for showing me the light

Part of the learning curve and it has bitten most of us at one point or another. The other one to hurry up and get out of your system is this one:

CODE

if( something = something)


instead of:
CODE

if(something == something)
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
11-19-2008 19:55
From: Viktoria Dovgal
1TBS.
a tablespoon of what?

seriously what is 1tbs in this context? LTBS leetified? If so, what is LTBS?
_____________________
-

So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.

I can be found on the web by searching for "SuezanneC Baskerville", or go to

http://www.google.com/profiles/suezanne

-

http://lindenlab.tribe.net/ created on 11/19/03.

Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard,
Robin, and Ryan

-
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-19-2008 19:58
From: SuezanneC Baskerville
a tablespoon of what?

seriously what is 1tbs in this context? LTBS leetified? If so, what is LTBS?

"The One True Brace Style" aka K&R
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
11-19-2008 20:25
From: Jesse Barnett
Part of the learning curve and it has bitten most of us at one point or another. The other one to hurry up and get out of your system is this one:

CODE

if( something = something)


instead of:
CODE

if(something == something)


It's a habit for me when comparing variables against values to have the value in the left hand position and the variable in the right (5 == fun); that way if I typo on the ==, I get a compiler error.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
11-20-2008 06:35
From: Jesse Barnett
"The One True Brace Style" aka K&R

Yep.. It's a code formatting style that puts opening { at a different indentation level than their partner }.

Since if makes it harder to see the structure of the code, it's useful for when your boss is always looking over your shoulder, trying to decide if you make too much money. Never let management be able to read your code or they'll begin to think they can write it, too!!
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
11-20-2008 08:39
From: Sindy Tsure
Since if makes it harder to see the structure of the code,

hmm really? compare an extended "if else if else if else if else if else" in both code styles and tell me that avoiding cuddled braces is more readable... or that cuddled braces don't in fact clarify control flow (as well as avoid the ; problem).

Next up - spaces or tabs :)
_____________________
http://slurl.com/secondlife/Together
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
11-20-2008 08:52
From: Escort DeFarge
Next up - spaces or tabs :)

Tabs!!!

They make stuff compile faster than spaces do.
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224
- If you see "if you were logged in.." on the left, click it and log in
- Click the "Vote for it" link on the left
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
11-20-2008 15:29
Tabs of course, spaces get fucked up too easily.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
11-20-2008 15:39
From: Jesse Barnett
"The One True Brace Style" aka K&R

CODE

while (something) {
...
}


Instead of:

CODE

while(something)
{
...
}


It makes it REAL clear that you've got surplus semicolons; {

And, no, it doesn't make code hard to read. Indentation matters.
From: Escort DeFarge
Next up - spaces or tabs :)
In LSL? "The tab is a lie!"
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-20-2008 17:13
Tweaked and customized an indent program as a plugin for Scite to output using K & R. This coupled with Strife's whitespace stripper to get rid of 50 lines of comments makes reading and debugging posted code much more pleasant. The biggest advantage of K&R to me is the fact that you are able to "compress" the code with the same readability.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-20-2008 17:15
From: Strife Onizuka
Tabs of course, spaces get fucked up too easily.

Hey let's all report Strife!!!!!! :p

(kidding! Love you as always Strife and miss your Moderating awesomeness)
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
11-20-2008 20:22
From: Jesse Barnett
Hey let's all report Strife!!!!!! :p

lol, the good old days.

The irony is, we didn't care if people cursed as long as it wasn't directed at people...

I've been trying to cut down on my cursing though, but sometimes I slip up and say "Fuck" then I realize my mistake and say "shit"... :rolleyes:

But really, bad whitespacing is something to curse about. It makes code hard to read and unless you have a tool to clean it up for you... it takes a bunch of time to get it fixed. Not mentioning that it makes maintaining the script harder for the author. Seriously, it's just better to do it right the first time; saves everyone time and effort.

Don't get me wrong, while I prefer tabs/4 spaces, I'll take what ever I can get as long as it's consistent.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
11-21-2008 10:32
IMHO indentation should be all the visible cue you need for structure

if (X){
}

produces lower chanses of having an extra semicolon, but higher chance to forget the opening brace

if (X)
{
}

produces the exact opposite likleyhoods

if you find yourself checking for semicolons more often than braces you'll prefer the first

if you dislike extra white space, same applies.

as for bosses looking over your shoulder, condensed code looks more complex, like they're getting more work out of you, but whitespace can make it look larger, and can hide large portions off screen if you're in one of those back stabbing labs...

I prefer the former personally
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
11-21-2008 10:53
... I never considered the office politics issues of coding style. Well, except for one thing...

One place we were working on the internal C coding standard, and there were people who were strongly for and against each of these styles. I came up with weasel words that managed to convince both sides they'd won long enough to get it signed off.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
11-21-2008 11:23
From: Void Singer
IMHO indentation should be all the visible cue you need for structure

I just happen to prefer that opening braces are indented with more than just one space on at the end of the previous line..

From: Strife Onizuka
I'll take what ever I can get as long as it's consistent.

True - picking a style and sticking with it is half+ the battle..
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
11-21-2008 13:25
From: Sindy Tsure
True - picking a style and sticking with it is half+ the battle..

100% agree with this.

The other half is about maintainability. I'm going to detail *why* I think cuddled braces are superior.

Ironically, for smaller scripts I think the more extended brace style is actually easier to "grok", but only for smaller scripts.

As the code length increases and the overall structure of the script gets more complex, unnecessarily extended code blocks increasingly obfuscate the overall structure and intention of the design.

If you directly compare 800-1000 line scripts (which is about as big as we are "allowed";) in each brace style and I think you'll appreciate why I say this.

My 5c
/esc
_____________________
http://slurl.com/secondlife/Together
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
11-21-2008 13:36
Here on my home machine, I get about 60 lines of code on the screen at once. At work, where I don't have to buy the monitor myself, it's probably closer to 75. Back when K&R was written, late '70s, they probably had 24 line terminals. Or maybe even working on those horrible teletype paper things.

/me wonders what the K&R coding style would look like if they had big honkin' LCDs with no real worries about screen real estate back then...
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
11-21-2008 13:56
....hmmm.... perhaps I should have said "if you directly compare 800-1000 line scripts *that you did not write yourself*. The same consideration applies but it's even more obvious!

/esc

BTW I surely don't think that this is a "winnable" argument, or even that "I know for sure". I'm just stating the evidence as I see it :)
_____________________
http://slurl.com/secondlife/Together
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
as an example...
11-21-2008 14:24
so... an example... check out these two touch_start events :

http://pastebin.com/m2a237228

To my view, the first overemphasizes the keywords, the second provides a balanced emphasis of the actual actions, and it's easier to see where the control flow blocks begin and end.

In the first, the white space immediately disconnects me between the declaration and the reason the event/flow block is called. This "semantic break" increases for me as the complexity of the code increases. The following is what the white space does "in my head" when reading the first...

if (test) NEW BLOCK do_this END else NEW BLOCK do_something_else END

...and the second...

if (test), do_this, else, do_something_else END

...maybe it depends on how your personal head works :)
_____________________
http://slurl.com/secondlife/Together
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
11-21-2008 20:13
true but odd, I actually learned most of my style raw, not from comparing differing styles, and not from the text book style which tended to seperate all braces (although true be told, mine was a compromise in some places, when writing jscript I had the habit of stacking ending braces).... I also disliked the convention of writing all function variables at the begining... I prefer them before the section I'm working with them, saves me from having do nothing varibles on edits, and allows me to throw them in for structures as I'm writing (sorry, I don't that level of preplanning, stick your flow chart elswhere)

the thing it comes down to for single programmers is confort and ease of use... if you are used to writing a certain way, and doing that makes it easier for you to work with, I say go for it.... when you work with others though you'd better be prepared to compromise, so it never hurts to use a style that if not easy for others to write, is easy enough for them to follow. and if you do it for someone else, you'd better believe the person signing the checks gets to dictate stupid things like style and comment formatting if they read it.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
1 2 3