Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

描述示例有歧义 #1433

Closed
pyj1971341376 opened this issue Jul 15, 2024 · 2 comments
Closed

描述示例有歧义 #1433

pyj1971341376 opened this issue Jul 15, 2024 · 2 comments

Comments

@pyj1971341376
Copy link

while 循环比 for 循环的自由度更高。在 while 循环中,我们可以自由地设计条件变量的初始化和更新步骤。

例如在以下代码中,条件变量 。每轮进行两次更新,这种情况就不太方便用 for 循环实现:

i每轮进行值的双倍更新,用for循环不是更规范直接嘛?while确实灵活度更高,但是举一个条件变化更为灵活的示例是不是更好一些呢,也更有助于初学者进行理解使用

@studyingegret
Copy link

请问你指的是哪一页哪里的文字?方便我也看看(发个URL)

@krahets
Copy link
Owner

krahets commented Jul 15, 2024

Hi,书中的示例对 i 进行了“两次更新”而非仅“双倍更新”,即:

while i <= n:
    res += i
    # 更新条件变量
    i += 1
    i *= 2

我们还可以对示例稍作拓展,更加体现 while 的灵活性:

while i <= n:
    i += 1
    res += i
    i *= 2
    res *= i

@krahets krahets closed this as completed Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants