samson_chaechae
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    <style> .mkd { background-color: rgba(0, 150, 250, .2); } </style> <span class="mkd"></span> # 迴圈 loop ## intro 電腦存在的最大意義就是協助人類執行大量且<span class="mkd">重複的運算</span> 而迴圈就是達成這個目的的工具 ## for迴圈 ### 語法 ```cpp= for(A初始運算式; B條件式; C控制運算式){ D迴圈內程式; } ``` #### 流程圖 <center> <img src="https://i.imgur.com/oFlGJ0z.png" width = "450px"/> </center> 執行A初始運算式 $\implies$ 判斷B條件式,成立執行D迴圈內程式 $\implies$ 執行C控制運算式 $\implies$ 判斷B條件式,成立執行D回圈內程式 $\implies$ 執行C控制運算式 ... ... $\implies$ 判斷B條件式,不成立離開 #### A初始運算式 初始運算式是進入迴圈後<span class="mkd">第一個</span>執行的程式 從頭到尾她也只會執行一次 在這裡你可以執行任何命令 通常會在這裡設定一個變數 #### B條件式 條件式是執行完A或是每次C執行完後都會進行的一次判斷 判斷式也是不限制 只要<span class="mkd">可以回傳true或是false就好</span> 判斷為true就會繼續執行D 判斷為false就結束迴圈繼續迴圈後的程式碼 通常是對A內設定的判斷操作 #### C控制運算 每當運行完D就會執行 通常是對A內設定的變數進行操作 #### D回圈內運算式 每當B條件式為true時就會執行 ### for運用 學過sigma的 $\displaystyle\sum_{i=1}^{n}i$其實就和for是很像的東西 ↑等價於 ```cpp= int n = 10,sum = 0; for(int i = 1; i <= n; i++){ sum += i; } cout << sum << '\n';//輸出55也就是1加到10的和 ``` :::info :::spoiler 上面程式的執行過程 int n = 10, sum = 0; int i = 1; i <= n (現在i為1) sum += i (現在sum為1) i++ (現在i為2) i <= n (現在i為2) sum += i (現在sum為3) i++ (現在i為3) i <= n (現在i為3) sum += i (現在sum為6) i++ (現在i為4) i <= n (現在i為4) sum += i (現在sum為10) i++ (現在i為5) i <= n (現在i為5) sum += i (現在sum為15) i++ (現在i為6) i <= n (現在i為6) sum += i (現在sum為21) i++ (現在i為7) i <= n (現在i為7) sum += i (現在sum為28) i++ (現在i為8) i <= n (現在i為8) sum += i (現在sum為36) i++ (現在i為9) i <= n (現在i為9) sum += i (現在sum為45) i++ (現在i為10) i <= n (現在i為10) sum += i (現在sum為55) i++ (現在i為11) cout << sum = 55 ::: #### 小知識 上面說A和C可以隨便任何命令 所以以下的也都是可以的 ```cpp= for(int i = 0,n = 10,sum = 0; i <= n; i++){ sum += i; } cout << sum << '\n';//輸出55也就是1加到10的和 ``` ```cpp= for(int i = 1,j = 10; j < 100; i++){ j *= i; cout << j << ' ';//輸出10 20 60 240 } ``` ```cpp= int i = 1; for(; i < 10;){ cout << i << ' ';//輸出1 2 3 4 5 6 7 8 9 i++; } ``` ```cpp= for(;;){ //這是無限迴圈 } ``` ## while迴圈 ### 語法 ```cpp= while(A條件式){ B迴圈內程式 } ``` #### 流程圖 <center> <img src="https://i.imgur.com/3XfCidV.png" width = "450px"/> </center> 判斷A條件式,成立執行B迴圈內程式 $\implies$判斷A條件式,成立執行B迴圈內程式 $\implies$判斷A條件式,成立執行B迴圈內程式 ... ... $\implies$判斷A條件式,不成立離開迴圈 #### A條件式 當條件式為true時,就會進入迴圈 當條件式為false時,就會離開迴圈,繼續}後的程式 判斷式也是不限制 只要<span class="mkd">可以回傳true或是false就好</span> #### B迴圈內程式 當條件式為true後,進入迴圈 當會圈內的程式執行完成後 就會再回到A條件式進行判斷 ### while運用 while其實是和for等價的 如果把while這樣寫 ```cpp= int i = 0; while(i < 10){ cout << i << '\n'; i++; } ``` 她和for這樣寫 ```cpp= for(int i = 0; i < 10 ; i++){ cout << i << '\n'; } ``` 是一樣的,不過這種情況我們通常都會用for 有時可以看到題目寫本題有<span class="mkd">t筆測資</span> 這時while就會派上用場了 ```cpp= int t; cin >> t; while(t--){//當t減到0時while就會結束 //解題code } ``` 也有可能看到題目會寫<span class="mkd">多筆輸入</span> 假如每筆輸入都要輸入一個n 這時也可以用while ```cpp= while(cin >> n){//當程式執行成功也會回傳true //解題code } ``` 這個也和上面的sigma一樣 ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ int n = 10, sum = 0; int i = 1; while(i <= n){ sum += i; i++; } cout <<sum<< '\n'; } ``` :::info :::spoiler 上面while的全部運作過程 int n = 10, sum = 0; int i = 1; i <= n; (現在i為1) sum += i; i++ (現在i為2) i <= n; (現在i為2) sum += i; i++ (現在i為3) i <= n; (現在i為3) sum += i; i++ (現在i為4) i <= n; (現在i為4) sum += i; i++ (現在i為5) i <= n; (現在i為5) sum += i; i++ (現在i為6) i <= n; (現在i為6) sum += i; i++ (現在i為7) i <= n; (現在i為7) sum += i; i++ (現在i為8) i <= n; (現在i為8) sum += i; i++ (現在i為9) i <= n; (現在i為9) sum += i; i++ (現在i為10) i <= n; (現在i為10) sum += i; i++ (現在i為11) cout << sum = 55 ::: ## do while ### do while語法 ```cpp= do{ A迴圈內程式 }while(B條件式);//這個分號要記得 ``` #### 流程圖 <center> <img src="https://i.imgur.com/HYuUXsG.png" width = "450px"/> </center> 執行A迴圈內程式 $\implies$判斷B條件式,成立執行A回圈內程式 $\implies$判斷B條件式,成立執行A回圈內程式 ... ... $\implies$判斷B條件式,不成立結束迴圈 #### A迴圈內程式 do while和while的不同處就在 do while不管條件式有沒有成立 它都一定會<span class="mkd">先執行一次迴圈內的程式</span> #### B條件式 當do先執行一次了後就和一般的while一樣了 當B條件式為true,執行A回圈內程式 當B條件是為false,離開迴圈 判斷式也是不限制 只要<span class="mkd">可以回傳true或是false就好</span> ### do while運用 do while比起while和for 真的很少用到 用一般的while和while前多打一次一樣的code也可以有一樣的效果 不過還是可以學一下 可以讓程式碼變得更簡潔 ## continue&break ### continue continue顧名思義就是繼續的意思 他的功能就是當執行到continue這個程式,就回到最上面的迴圈,不執行接下來的回圈內程式 例如for就是回到C控制運算式,也就是我們平常寫i++的地方 而while就是回到A條件式 ```cpp= #include<iostream> using namespace std; int main(){ for(int i = 1; i <= 10; i++){ if(i % 2 == 0)continue;//如果判斷i是偶數,就回到i++,不執行cout cout << i << '\n'; } } ``` ### break break的功能就是離開迴圈直接執行迴圈後的下一行程式 我們用一個題目來舉例 apron想買peko的周邊需要2500元 請你幫忙寫一個程式計算apron存了多少錢 每次的輸入就代表存進去的錢 而當apron存到了2500以上就提醒他你可以買周邊了,並且結束這個存錢功能 ```cpp= #include<iostream> using namespace std; int main(){ int money; int sum = 0; while(cin >> money){ sum += money; cout << "現在有"<<sum << "元"<< '\n'; if(sum >= 2500){ cout << "可以買了喔peko" <<'\n'; '\n'; break;//結束迴圈 } } } ```

    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