473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

";" after if statement??

JS
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s[i].length = 0;
opt = document.createElement('OPTION');
s[i].appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".?
Jul 23 '05 #1
6 2252
JS wrote:
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s[i].length = 0;
opt = document.createElement('OPTION');
s[i].appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".?


I guess, the semicolon should be after "return true" and not after the
closing bracket.

best regards
Deniz
Jul 23 '05 #2
JS wrote:
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s[i].length = 0;
opt = document.createElement('OPTION');
s[i].appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";"
No it isn't. The semicolon, following the closing brace that represents
the end of the Block statement that is the conditionally executed
statement of the IfStatement, is an EmptyStatment. Harmless but
unnecessary.
but I have seen other if statments
that do not end with that.
The IfStatement production does not require a terminating semicolon. If
the Statement ending the production - if ( Expression) Statement - is
not a Block statement then that statement may need to be terminated with
a semicolon, giving the impression that the IfStatment ends with a
semicolon.

ECMAScript performs 'automatic semicolon insertion' so under some
circumstances semicolons that are required by the production rules may
be omitted as the interpreter will insert them itself. See ECMA 262 3rd
edition section 7.9.
Why is if statement sometimes ended by a ";".?


The existence of the EmptyStatment - ; - (which is harmless in most
contexts) and automatic semicolon insertion allows script authors to get
away with having little understanding of when and where semicolons are
necessary in javascript. That lack of understanding can be observed when
looking at example scripts.

Richard.
Jul 23 '05 #3
On 27/05/2005 08:56, Richard Cornford wrote:

[snip]
The existence of the EmptyStatment - ; - (which is harmless in most
contexts) and automatic semicolon insertion allows script authors to get
away with having little understanding of when and where semicolons are
necessary in javascript. [...]


I'd disagree with the former, and agree with the latter.

Empty statements are a part of other languages, including compiled ones,
which have no qualms over enforcing strict adherence to the grammar. I
can't think of enough examples to decide if empty statements are ever
necessary, but they can be used, for example, in iterative statements
where the condition also performs an action that renders the loop body
redundant:

while( condition );

However, this could be equally represented with:

while( condition ) {}

as the StatementList within a Block is optional. This is true in C (and
C++), and Java (the grammar summary for the latter is misprinted).

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Michael Winter wrote:
Richard Cornford wrote: <snip>
The existence of the EmptyStatment - ; - (which is
harmless in most contexts) and automatic semicolon
insertion allows script authors to get away with
having little understanding of when and where
semicolons are necessary in javascript. [...]


I'd disagree with the former, and agree with the latter.

Empty statements are a part of other languages, including
compiled ones, which have no qualms over enforcing strict
adherence to the grammar.

<snip> while( condition );

<snip>

I have nothing against empty statments, and would prefer using one in an
IterationStatement where all the work was done in the control expression
(rather than an empty block).

But having empty statements allows them to be inserted where semicolons
would not otherwise be necessary (you would get some sort of error if
they did not exist), and that is true of compiled languages as well,
except in compiled languages you don't get away with omitting the
necessary semicolons so you end up learning where they need to be very
quickly.

The OP's question relates to an unnecessary EmptyStatement rather than
the missing semicolon at the end of the ReturnStatement (which is fixed
by automatic semicolon insertion). An example, seeing:-

var x = function(){
... // function body
};

- which is correct because the assignment expression becomes an
ExpressionStatement and should be semicolon terminated, someone might
infer:-

function x(){
... // function body
};

- where the semicolon is an EmptyStatement independent of the function
declaration. But getting away with that may serve to blur the
distinction between a function declaration and a function expression as
a side effect.

Richard.
Jul 23 '05 #5
Richard Cornford wrote:
<snip>
var x = function(){
... // function body
};

- which is correct because the assignment expression becomes
an ExpressionStatement and should be semicolon terminated, ...

<snip>

Actually the assignment expression becomes part of a VariableStatement,
which is also semicolon terminated. I probably should have left the -
var - keyword out.

Richard.
Jul 23 '05 #6
In article <d7**********@news.net.uni-c.dk>, JS <ds***@asdf.com.invalid>
writes
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s[i].length = 0;
opt = document.createElement('OPTION');
s[i].appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".?


It might help to understand other people's answers if the code is laid
out to show how the javascript parser will see it :

if (n==0 && args>1)
{
for (i=num; args>i+1; i++)
{
s[i].length = 0;
opt = document.createElement('OPTION');
s[i].appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
}

;

Each extra level of indenting marks a statement or statements nested
inside another statement. (The parser will also automatically add a
semicolon after 'true').

John
--
John Harris
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
5
by: charliewest | last post by:
I've implemented the USING statement to ensure that my newly created sql connection closes when my method is finished using it. The USING statement is wrapped in try/catch error handling statement....
2
by: Richard | last post by:
Our web programmer was looking in his application log an found the following error: 2006-08-31 16:33:35,129 ERROR org.hibernate.util.JDBCExceptionReporter - < SQL0723N An error occurred in a...
2
by: marsarden | last post by:
write code like: int main(void) { int a=10; if(a<20) {} } Compiler ok on dev-cpp . don't we have to add a ";" after if
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
10
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.