te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Confused about while loop in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Confused about while loop in javascript - Stack Overflow

programmeradmin3浏览0评论

I might be being a bit thicky here but please answer me this. Consider the following code:

a=1;
while(a<=6) {
   console.log(a);
   a++;
}

If I run this I get values in the console from 1 to 6, and then another 6.

Now look at this:

a=1;
while(a<=6) {
    console.log(a);
    ++a;
}

Running this will now get me the values from 1 to 7.

Why is this happening? My understanding was that the statement block would only run if the expression evaluates to true. How can this be possible in the second of my examples? And why does 6 appear twice in the first? Very confusing for me.

If you can explain simply (I'm still learning) that would be great.

I might be being a bit thicky here but please answer me this. Consider the following code:

a=1;
while(a<=6) {
   console.log(a);
   a++;
}

If I run this I get values in the console from 1 to 6, and then another 6.

Now look at this:

a=1;
while(a<=6) {
    console.log(a);
    ++a;
}

Running this will now get me the values from 1 to 7.

Why is this happening? My understanding was that the statement block would only run if the expression evaluates to true. How can this be possible in the second of my examples? And why does 6 appear twice in the first? Very confusing for me.

If you can explain simply (I'm still learning) that would be great.

Share Improve this question edited Jun 26, 2012 at 16:14 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jun 26, 2012 at 16:11 Mat RichardsonMat Richardson 3,6065 gold badges35 silver badges57 bronze badges 8
  • can u show the full code because ++a; and a++; are same if they are individual statement and not posite with other statement. – Romil Kumar Jain Commented Jun 26, 2012 at 16:14
  • 2 The second example prints 1 through 6 -> jsfiddle/USYSH – Manse Commented Jun 26, 2012 at 16:16
  • 1 possible duplicate of What's the difference between ++i and i++ in JavaScript – epascarello Commented Jun 26, 2012 at 16:17
  • 3 @ManseUK not in Firefox it doesn't. The key ingredient is to do it from the browser JavaScript console, whose behavior is what's at issue here. The last number printed is not the result of a console.log() call. – Pointy Commented Jun 26, 2012 at 16:18
  • 1 @epascarello this is not the same question as that. It's more about confusion over the console output. – tybro0103 Commented Jun 26, 2012 at 16:31
 |  Show 3 more ments

3 Answers 3

Reset to default 14

The console prints for you the value of the last statement evaluated. In the second case, you pre-increment, so the value of that is 7 and not 6 as in the first one.

Change you console.log() call to print more stuff and it'll be obvious:

console.log("a is: " + a);

You won't see that prefix on the last line.

In both cases, you're seeing an extra digit because the console is outputting the result of the last statement in the loop.

When that code is not executed directly in the console, you will not see what appears to be an extra digit.

See the fiddle with their response. Both return 1 to 6.

a++ : It returns the value of a before increment.

++a : It returns the value of a after increment.

Loops executes until value of 'a'<= 6.

When you run any code on console, it evalutes the variable value and prints its value also that's why you are getting one more 6 and 7 in the output.

No worries, when you'll run this code, will get the 1-6 values only.

发布评论

评论列表(0)

  1. 暂无评论