PDA

View Full Version : if clauses with no braces?



Joe123
12-14-2007, 08:41 PM
I've seen recently some if clauses where people haven't put braces around what's inside, such as Beefster's new roll script, Saffiths 'isSolid', pkmnfrk's sideview ladders, how does this work?
I thought all ifs, elses, fors and whiles had to have braces around them?

Also, I think I saw a 'do(){}' in the NeoFirst script file, what's all that about?

pkmnfrk
12-14-2007, 08:58 PM
These are all cases of having a single line in each clause.

The idea is that the special structures (if, while, else, etc.) can only have one block each. Normally, a block is designated by the braces ({ and }), and you can only add one to each clause (else counts separately)

However, if you consider that each statement (i.e. line ending with a semicolon) is also a block in and of itself, then you'll see that the braces are unnecessary for a single line.

Joe123
12-14-2007, 09:11 PM
ahh, so:

if(x == 0) Game->PlaySound(1);
is fine, aswell as

if(x == 0){Game->PlaySound(1);}

but whereas:

if(x == 0){Game->PlaySound(1);
Link->HP = 16;}
is fine,

if(x == 0)Game->PlaySound(1);
Link->HP = 16;
is not?

Gleeok
12-14-2007, 09:27 PM
Yeah, that seems right. I stumbled upon the else thing by accident(typo), and think I only use it in one place due to my compulsive urge to add more lines to stuff. I have no idea about "do" though. What script is that in?

bluedeath
12-14-2007, 11:23 PM
I still have much to learn before I could handle stuff like this myself. I dont know where I can find a something like "A scripting tutorial for the dumbest people in the world like me" tutorial :P

DarkDragon
12-15-2007, 02:24 AM
Gleeok's got it right. ZScript follows C with regard to how ifs and loops (for and while) work.

Joe123
12-15-2007, 05:06 AM
OK, go to: http://www.j-factor.com/~l/zeldaclassic/NeoFirst.txt
and search 'do {' in the find function, _L_ seems to use it quite frequently.

Wait, he just appears to have put it round waitframe(); a lot :confused:

Gleeok
12-15-2007, 05:25 AM
do {
Waitframe();
} while (Link->InputA);
}

Huh.....well isn't that some shit right there.

Joe123
12-15-2007, 10:10 AM
Don't you just hate it when you think you've got quite a nice grasp of something, and then it suddenly turns into something completely different? :eek:

What the hell does that do?!
I don't understand 'do', and that while loop either has nothing in it, or no parameters.
:(

EDIT: WAIT a minute!
does that read:
Do{this;}While(this);?

So

do{Waitframe();}
while(Link->InputA);?
and if it is that, why not just put

while(Link->InputA){Waitframe();}?

pkmnfrk
12-15-2007, 12:31 PM
ahh, so:

if(x == 0) Game->PlaySound(1);
is fine, aswell as

if(x == 0){Game->PlaySound(1);}

but whereas:

if(x == 0){Game->PlaySound(1);
Link->HP = 16;}
is fine,

if(x == 0)Game->PlaySound(1);
Link->HP = 16;
is not?

The last example is technically fine, it just doesn't work the way you think. It works like this:


if (x == 0) {
Game->PlaySound(1);
}
Link->HP = 16;



Don't you just hate it when you think you've got quite a nice grasp of something, and then it suddenly turns into something completely different? :eek:

What the hell does that do?!
I don't understand 'do', and that while loop either has nothing in it, or no parameters.
:(

EDIT: WAIT a minute!
does that read:
Do{this;}While(this);?

So

do{Waitframe();}
while(Link->InputA);?
and if it is that, why not just put

while(Link->InputA){Waitframe();}?

Because, do{} while(); has a different meaning. than just while(){};

do{} while(); is guaranteed to run the block at least once. It runs the block, then checks the conditional. If it's true, then it runs the block again, etc, etc. If you just use while(){}, then if the condition is false to begin with, the code in the block will never run.

I forget if you can use the until() clause instead of while(), but it just means it'll run while the conditional is false (which means that you can just use while(!something))

DarkDragon
12-15-2007, 12:38 PM
if you can use the until() clause instead of while(), but it just means it'll run while the conditional is false (which means that you can just use while(!something))


Not unless another dev has added that to language recently.

pkmnfrk
12-15-2007, 12:57 PM
Ok, then you can't :P