最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What does while javascript while(i<9) mean? - Stack Overflow

programmeradmin0浏览0评论

On this site about javascript shorthands, by Sam Deering, a long-hand example is as follows (the site uses a before-and-after format):

var i=0;
while (i<9)
{
  //do stuff
  i++; //say
}

While I don't understand this, I do understand the short-hand version, which is:

var i=9;
while(i--)
{
    //goes until i=0
}

I don't know about if i is the same in both loops, but assume that they both loop 9 times. Because I would like to expand my limits and improve my programming skills, what does the while (i<9) mean, and how can I use it (with other numbers)?

On this site about javascript shorthands, by Sam Deering, a long-hand example is as follows (the site uses a before-and-after format):

var i=0;
while (i<9)
{
  //do stuff
  i++; //say
}

While I don't understand this, I do understand the short-hand version, which is:

var i=9;
while(i--)
{
    //goes until i=0
}

I don't know about if i is the same in both loops, but assume that they both loop 9 times. Because I would like to expand my limits and improve my programming skills, what does the while (i<9) mean, and how can I use it (with other numbers)?

Share Improve this question asked Jun 27, 2016 at 23:39 user31415user31415 4667 silver badges16 bronze badges 4
  • 5 That's most likely an encoding issue. &lt; is the HTML entity for the < character. The author even mentions while (i++<10) as an alternative, so that's clearly what was intended. – p.s.w.g Commented Jun 27, 2016 at 23:41
  • Write your code without shorthands like this. It's work for minifiers. – vp_arth Commented Jun 27, 2016 at 23:45
  • 1 You are receiving downvotes because most people think you are doing it very wrong. Unless you use HTML you can ignore them, &lt; is a proper way to escape < in XHTML. Using < might throw a draconian parse error. – Oriol Commented Jun 28, 2016 at 0:01
  • I did not know I was reading a poorly written example. That was why I was asking, I am not asking about my own style, rather about a website's style. – user31415 Commented Jun 28, 2016 at 16:57
Add a ment  | 

2 Answers 2

Reset to default 3

In XML, e.g. XHTML, you must escape < properly as &lt;.

In HTML it's not necessary to do so inside script elements because their contents are parsed in a special way, but XML does not do these nasty things.

For example, you can write index.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head><title>Some document</title></head>
<body>
<script type="text/javascript">
var i=0;
while (i &lt; 9) i++;
console.log(i); // 9
</script>
</body>
</html>

Most people don't want to XML-escape JavaScript operators in inline scripts, so they use CDATA sections:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head><title>Some document</title></head>
<body>
<script type="text/javascript">
<![CDATA[
var i=0;
while (i < 9) i++;
console.log(i); // 9
]]>
</script>
</body>
</html>

Your HTML file most probably has some encoding quirks. &lt; corresponds to the less than symbol < in HTML entities. So really you can read that as while (i<9).

发布评论

评论列表(0)

  1. 暂无评论