Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Bern

macrumors 68000
Original poster
Nov 10, 2004
1,854
1
Australia
I'm learning Javascript and I'm totally confused. The curly braces "{" and "}" use is annoying the crap out of me. I don't really understand their placement.

For example I have a script that will ask three questions and based upon the answer provided by the user will return a suitable response. Now the use of the curly braces has me totally confused so much so that I cannot decipher the correct method to write such a script.

For the purpose of this question following is an example script for the scenario I mention above:-
<script>

if (confirm("Is the weather clear?")) {
if (confirm(Is it before 5pm?")) {
if (confirm(Is the primary route congested?")) {

alert("Take alternative \"B\" Home")
}
else {
alert("Take primary route home")
}
}
else {
alert("Take alternative \"A\" Home")
}
}
else {
alert("Take alternative \"A\" Home")
}
alert ("Arrive home safely")

</script>

So there's curly braces all over the place and I just don't understand which set of two each are meant to be around what part of the code and why. Which of course makes the whole damned thing confusing when designing a script.

Can anyone offer some advice please?
 
If you lay it out like this, it's a bit easier to understand.

Code:
if (confirm("Is the weather clear?"))
{
	if (confirm(Is it before 5pm?"))
	{
		if (confirm(Is the primary route congested?"))
		{
			alert("Take alternative \"B\" Home")
		}
		else
		{
			alert("Take primary route home")
		}
	}
	else
	{
		alert("Take alternative \"A\" Home")
	}
}
else
{
	alert("Take alternative \"A\" Home")
}
alert ("Arrive home safely")
 
Nermal said:
If you lay it out like this, it's a bit easier to understand.

Code:
if (confirm("Is the weather clear?"))
{
	if (confirm(Is it before 5pm?"))
	{
		if (confirm(Is the primary route congested?"))
		{
			alert("Take alternative \"B\" Home")
		}
		else
		{
			alert("Take primary route home")
		}
	}
	else
	{
		alert("Take alternative \"A\" Home")
	}
}
else
{
	alert("Take alternative \"A\" Home")
}
alert ("Arrive home safely")


Actually it doesn't :eek: I'm not sure which set of braces open and close and for what purpose. I just don't grasp the nested "IF" statements and how to incorporate them correctly using the curly braces. I'm so frustrated with it.
 
if (this happens) { do this and this and this } else { do this and this and this }

The { } are essentially used to group all the things that need to be done.
Does that help? Admittedly it's a bit tricky to understand at first.
 
I'm trying to get a handle on it, but it just isn't gelling for me. Gosh VB.net was easier to understand than this.

The way I read it is for every IF statement there must be a set of curly braces and same for every ELSE statement. However, when I look at how it is written the braces don't seem to match up with each other.

I put letters next to each set of braces that I think are the corresponding set (ie: a to a, b to b, etc) which for some reason throws out your indentation when I post it here (sorry about that).

code:
If (confirm("Is the weather clear?"))
{ a
if (confirm(Is it before 5pm?"))
{ b
if (confirm(Is the primary route congested?"))
{ c
alert("Take alternative \"B\" Home")
} c
else
{ d
alert("Take primary route home")
} d
} b
else
{ e
alert("Take alternative \"A\" Home")
} e
} f
else
{ f
alert("Take alternative \"A\" Home")
} a
alert ("Arrive home safely")
 
The last three (f, f, a) should be a, f, f.

You actually don't have to use braces if there is only one statement to be executed. I hate to confuse you more, but look at this, where I've taken out all the unnecessary braces.

Code:
if (confirm("Is the weather clear?"))
{
	if (confirm(Is it before 5pm?"))
	{
		if (confirm(Is the primary route congested?"))
			alert("Take alternative \"B\" Home")
		else
			alert("Take primary route home")
	}
	else
		alert("Take alternative \"A\" Home")
}
else
	alert("Take alternative \"A\" Home")
alert ("Arrive home safely")
 
Nermal said:
if (this happens) { do this and this and this } else { do this and this and this }

The { } are essentially used to group all the things that need to be done.
Does that help? Admittedly it's a bit tricky to understand at first.

Exactly, the { } are there to *group* statements.

You don't need the curly braces if there is only one statement (rather than a block of statements) following your if, though it is better practice to include them.

Your code could potentially be rewritten as:

Code:
<script>

if (confirm("Is the weather clear?")) {
	if (confirm(Is it before 5pm?")) {
		if (confirm(Is the primary route congested?")) alert("Take alternative \"B\" Home");
		else alert("Take primary route home");
	}
	else alert("Take alternative \"A\" Home");
}
else alert("Take alternative \"A\" Home");

alert ("Arrive home safely");

</script>
 
Nope I just don't get it. :confused:

Are you suggesting that the curly braces only are used to group IF's together? So 3 x IF's would only get one set of curly braces then each ELSE would receive it's own set?

Like:-

IF (this happen) {
IF(this happens)
IF(this happens)
}

ELSE {
(do this)
}

{
ALERT()
}

So the ELSE is pertaining to not all of the IF's receiving a TRUE response (ie the user may have chosen cancel instead of ok for one of the IF's) and the final ALERT only actually takes place if all three IF's are TRUE?

So in the original example the final ALERT corresponds to all three IF's receiving a TRUE or "OK" response, whereas the other four ALERTS correspond to each of the three IF's individually and will pop up depending on which IF receives a negative response? Obviously that is why there are curly braces placed strategically (yet confusingly) around?


I really want to get a grasp of it, but to be honest I'm ready to throw in the towel.
 
Let's simplify this to one if statement.

Code:
if (this happens)
{                            - start of "if" group
        do this;
        do this too;
        also do this;
}                            - end of group
else
{                            - start of "else" group
        do this instead;
        and this;
}                            - end of group

Understand that?
 
You say you've got it, but you've managed to screw it up again :(

Half of your braces are pointing the wrong way.
 
Oops sorry what I meant to say was:-

IF (something(this)) {
}

ELSE {
(this)
}

I had a vision of you slamming your head against the table for a moment there :D

It's when I nest IF and ELSE statements that I come unstuck.
 
Hmmm let me have a crack at this.

Code:
if (this happens)
{
        do this;
        do this too;
        also do this;
}
else
{ 
        do this instead;
        and this;
}
Now you follow that, so it's not too much of a step to be nesting clauses.

The 'do this' line is just a generic line of code, how about letting it be another if-statement?

If you do that you'll get something like:

Code:
if (this happens)
{


        if (this also happens) {
               do this thing;
        } else {
               do this instead;
        }



        do this too;
        also do this;
}
else
{ 
        do this instead;
        and this;
}

Does that help at all?
 
Yes I get that, but in my original post there are nested IF statements. I don't quite see how the curly braces relate to each IF (where the opening and closing curly braces relate to each IF and each ELSE.

If it was said every IF statement had to have a set of opening and closing curly braces and so do each ELSE statement, then my original Javascript would have been written differently to what I have. I would see an opening curly brace at the start of each statement and a corresponding closing curly brace at the end of each statement, but it seems that isn't the case so it doesn't make any logical sense to me.
 
Bern said:
Yes I get that, but in my original post there are nested IF statements. I don't quite see how the curly braces relate to each IF (where the opening and closing curly braces relate to each IF and each ELSE.

If it was said every IF statement had to have a set of opening and closing curly braces and so do each ELSE statement, then my original Javascript would have been written differently to what I have. I would see an opening curly brace at the start of each statement and a corresponding closing curly brace at the end of each statement, but it seems that isn't the case so it doesn't make any logical sense to me.

OK...back to the beginning again. It really does help readability if you indent the code.

Here is your original code, indented, commented and with each if-else block in its own colour!!

Code:
[color=red]if (confirm("Is the weather clear?")) {					//open first if[/color]
	[color=blue]if (confirm(Is it before 5pm?")) {				//open second if
		[color=green]if (confirm(Is the primary route congested?")) {	//open third if
			alert("Take alternative \"B\" Home")
		}							//close third if
		else {							//open else for third if
			alert("Take primary route home")
		}							//close else for third if[/color]
	}								//close second if
	else {								//open else for second if
		alert("Take alternative \"A\" Home")
	}								//close else for second if[/color]
[color=red]}									//close first if
else {									//open else for first if
	alert("Take alternative \"A\" Home")
}									//close else for first if[/color]

[color=purple]alert ("Arrive home safely")						//completely outside the if-else construct[/color]

Yes each if { } and else { } has its own set of curly braces, but as has already been mentioned, these are not strictly necessary if there is only *one* statement to execute. To keep things simple, let's just say that each if and each else must have a matching set of curly braces.

Does it make sense now??
 
Ah yes! Thank you

I think I'm just so use to a completely different type of programming language the way Javascript is written didn't make sense.

Thanks everyone for your valuable input and patience, I really do appreciate it. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.