NCU NKFW
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 迴圈(Loop) ###### tags: `NKFW 網頁設計入門` --- # 迴圈的用處 * 用來執行不斷重複的動作。 * 使程式較為簡化、易讀。 * 可以執行指令特定次數、在某些條件發生時停下或不斷執行。 --- # while loop ```htmlembedded! i = 0; while (true) { alert(i); // 不要用console.log(i) i += 1; } ``` :::info 試試看執行這段程式會發生什麼事。 ::: --- ## 基本語法 ```htmlembedded! while (條件判斷式) { 執行指令; //...... } ``` --- ## 介紹 * 若是條件判斷式為`true`,則瀏覽器會不斷執行迴圈中的動作,直到條件判斷式為`false`。 * 上面出現過的`while(true) {}`被稱為無限迴圈,會無限執行迴圈內的指令。 * 一般我們不會讓迴圈無限執行,會對迴圈設定終止條件,否則將無法執行迴圈後的程式或無法正常結束程式。 --- ```htmlembedded! i = 0 while (i < 10) { alert(i); i++; } ``` 上圖程式的執行結果為,依次在跳出視窗顯示整數0到9後結束。 --- ## Project1:計算數列和 :::info 數列是一串有限或無限的數字,是數學中的重要概念,我們一般可以透過累加法或公式計算其和。 請運用while迴圈,設計一個函數接收正整數n,計算0+1+2+...+(n-1)+n的數列和回傳。 ::: --- :::spoiler 參考答案 ```htmlembedded! function sumOfSequence(n) { sum = 0; i = 0; while (i <= n) { sum += i; i += 1; } return sum; } ``` ::: --- ## for loop ```htmlembedded! for (i = 0; i < 3; i++) { console.log(i); } ``` :::info 試試看執行這段程式會發生什麼事。 ::: --- ## 常用語法 ```htmlembedded! for (i = 0; i < n; i++) { //...... } ``` --- ## 介紹 * i 是一個變數,可以任意更改名稱。 * 「i = 0」代表將0設為i的初始值。 * 「i < n」相當於while迴圈中的執行條件,在i < n為true時,迴圈中的內容才會執行。 * 「i++」在每次迴圈中的內容執行完後執行。 * 上面示範的程式其實就相當於: ```htmlembedded! i = 0; while (i < n) { ...... i++; } ``` --- * 大部分使用while達成的功能可以用for達成,大部分使用for達成的功能也可以用while達成,但for更加適合用來執行特定次數的指令。 --- ## continue & break * 說明: 大部分都會搭配while跟for loop使用,主要是在某個條件觸發時,進行操作。兩者操作則在下面會進行解釋 * continue: 跳過這一次迴圈。 ```javascript= for(let i=0; i<5;i++){ if(i==3){ continue; } console.log(i); } ``` --- * break: 直接結束迴圈。 ```javascript= let i = 0; while(True){ if(i > 10) break; i++; } ``` --- ## Project2:計算數列和 :::info 試著用for迴圈來實作Project1。 ::: --- :::spoiler 參考答案 ```xml! function sumOfSequence(n) { sum = 0; for (i = 1; i <= n; i++) { sum += i; } return sum; } ``` ::: --- ## Project3:巢狀迴圈 :::info 迴圈如同html中的各種標籤可以具有巢狀結構,也就是在for迴圈中在放入另外一個for迴圈。 有一數列為1+2+2+3+3+3+4+4+4+4+...+n*n,請試著寫一個函數,使用兩層的巢狀迴圈以累加法計算這個數列的和。 ::: --- :::spoiler 參考解答 ```htmlembedded! function sumOfSequence(n) { sum = 0; for (i = 1; i < n + 1; i++) { for (j = 1; j < i + 1; j++) { sum += i; } } return sum; } console.log(sumOfSequence(5)); ``` ::: --- ## Project4: continue&break :::info 使用continue與break的語法,修改下方的程式碼;使num在為3的倍數時不要輸出在控制臺,並在num為50時跳出迴圈、結束程式。 ::: ```javascript! let num = 1; while (true) { // 在這裡撰寫程式 console.log(num); num++; } ``` :::spoiler 參考答案 ```javascript! let num = 1; while (true) { if (num % 3 == 0) { num++ continue; } if (num == 50) break; console.log(num); num++ } ``` :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully