This makes everything much clearer.
Thank you very much for your time.

☆*PhistucK*



On Fri, Jan 28, 2011 at 15:22, Lasse R.H. Nielsen <[email protected]> wrote:

> If and else are not two statements. It's two different forms of the
> if-statement.
>
> The if statement is a compound statement, meaning that it can contain other
> statements as parts of itself. Those statements are either just the
> mandatory then-branch, or the then-branch and an else-branch. The
> if-statement still counts as one statement itself.
>
> The code:
>
>   if (some_condition)
>    some_function();
>   else
>    some_other_function();
> is an example of this. It has two branches, each being exactly one
> statement (in this case ExpressionStatements, which are an Expression
> followed by a semicolon). The presence of the else-branch is marked by the
> first statement being *immediately* followed by the keyword "else" and then
> the else-branch statement.
>
> A block statement is another compound statement. It can contain an
> arbitrary number of other statements. So,
>   if (condition)
>     { x = 2; y = 4; }
>   else
>     some_function();
> is also valid. The then-branch is a single statement, which is a statement
> block containing two other statements. It's traditionally written
>  if (condition) {
>    x = 2;
>    y = 4;
>  } else {
>    some_function();
>  }
> but the "{"'s are not part of the if-statment syntax, but part of the
> block-statement syntax.
>
> With that in mind, you have code on the form:
>   if (c1) if (c2) s1 else s2
>
> In this case, the "else" binds to the closest if, so it's equivalent to:
>   if (c1) { if (c2) s1 else s2 }
> and not
>   if (c1) { if (c2) s1 } else s2
> which is why you get "a is not o".
>
> The syntax for if-statements comes from the C language (or possibly from
> the precursor B). It's not specific to JavaScript.
>
> Best of luck
> /Lasse
>
> On Fri, Jan 28, 2011 at 13:39, PhistucK <[email protected]> wrote:
>
>> I added HTML code because the syntax error does not occur on Internet
>> Explorer 8 (for example) and it is easier to test when you have the entire
>> code ready.
>>
>> So you are basically saying that "if" and "else" do not count as two
>> statements, right?
>>
>> I guess I am confused, because I recently found out you can actually run
>> this code without any error -
>> if (some_condition)
>>  some_function();
>> else
>>  some_other_function();
>>
>> ☆*PhistucK*
>>
>>
>>
>> 2011/1/28 Mikael Helbo Kjær <[email protected]>
>>
>> Hi there.
>>>
>>>
>>>
>>> This is not the usual thing to ask here (I’d go for some sort of
>>> Javascript forum in the future).
>>>
>>>
>>>
>>> I think you don’t need to include any sort of HTML here to get ppl to
>>> look at your JS.
>>>
>>>
>>>
>>> First problem:
>>>
>>> You have a ; after the end of your if block (I’ve marked it below), that
>>> ends the if else statement too early. Else cannot stand alone so to speak.
>>>
>>>
>>>
>>> Second problem: your blocks are not entirely right here. The parser can
>>> only follow the rules here. The if sentence either deals with the next
>>> statement (line) or block. Only if that line or block is directly followed
>>> by an else or else if can that be included. A corrected (untested assumption
>>> here) example could be (there is more than one way to do it):
>>>
>>>
>>>
>>> var a = “0”
>>>
>>> var b = “c”
>>>
>>> if (a == “0”)
>>>
>>> {
>>>
>>>      if (b == “b”)
>>>
>>>     {
>>>
>>>         alert(“a is o”);
>>>
>>>         alert(“b is b”);
>>>
>>>     }
>>>
>>> }
>>>
>>> else
>>>
>>> {
>>>
>>>     alert(“a is not o”);
>>>
>>> }
>>>
>>>
>>>
>>> /Mikael
>>>
>>>
>>>
>>> When I try this code -
>>>
>>> <!DOCTYPE HTML>
>>>
>>> <html>
>>>
>>> <script>
>>>
>>> var a = "o";
>>>
>>> var b = "c";
>>>
>>> if (a == "o")
>>>
>>>  if (b == "b")
>>>
>>>  {
>>>
>>>   alert("a is o");
>>>
>>>   alert("b is b");
>>>
>>>  }; ß Problem here
>>>
>>> else
>>>
>>> {
>>>
>>>  alert("a is not o");
>>>
>>> };
>>>
>>> </script>
>>>
>>> </html>
>>>
>>>
>>>
>>> It shows -
>>>
>>> Uncaught SyntaxError: Unexpected token else
>>>
>>>
>>>
>>> Should that error be triggered? can you explain why?
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Also, running it without that semicolon -
>>>
>>> <!DOCTYPE HTML>
>>>
>>> <html>
>>>
>>> <script>
>>>
>>> var a = "o";
>>>
>>> var b = "c";
>>>
>>> if (a == "o")
>>>
>>>  if (b == "b")
>>>
>>>  {
>>>
>>>   alert("a is o");
>>>
>>>   alert("b is b");
>>>
>>>  }
>>>
>>> else
>>>
>>> {
>>>
>>>  alert("a is not o");
>>>
>>> };
>>>
>>> </script>
>>>
>>> </html>
>>>
>>>
>>>
>>> Shows an alert - "a is not o".
>>>
>>> As far as I know, "if" and "else" count as two statements. If they are,
>>> the "else" block is actually part of the first "if" statement.
>>>
>>> Am I misinformed, or is it a bug?
>>>
>>>
>>>
>>> --
>>> v8-users mailing list
>>> [email protected]
>>> http://groups.google.com/group/v8-users
>>>
>>
>>  --
>> v8-users mailing list
>> [email protected]
>> http://groups.google.com/group/v8-users
>>
>
>
>
> --
> Lasse R.H. Nielsen
> [email protected]
> 'Faith without judgement merely degrades the spirit divine'
> Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K -
> Denmark - CVR nr. 28 86 69 84
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
>

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to