鄭余玄
    • 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
    --- slideOptions: theme: serif --- # for 迴圈、流程控制 ###### 03.11 鄭余玄 ###### 2017 資訊之芽 語法班 --- # while 迴圈複習和技巧 ---- ## while 複習 - `while (表示式) { ... }` 當表示式成立就一直執行 ```c++ // 執行 10 次 int n = 1; while (n <= 10) { // 若輸出次數 < 10 則繼續輸出 std::cout << "我愛資芽" << std::endl; n++; // 輸出次數加一, n = n + 1; } ``` ```c++ // 執行 10 次 int n = 10; while (n > 0) { // 若剩餘次數 > 0 則繼續輸出 std::cout << "我愛資芽" << std::endl; n--; // 剩餘次數減一 } ``` <!-- .element: class="fragment" data-fragment-index="1" --> ---- ## 觀察 ```c++ int n = 10; while (n > 0) { // 若剩餘次數 > 0 則繼續輸出 std::cout << "我愛資芽" << std::endl; n--; } ``` ```c++ while (n != 0) { // 若剩餘次數 != 0 則繼續輸出 ``` <!-- .element: class="fragment" data-fragment-index="1" --> ```c++ while (n) { // 若剩餘次數 != 0 則繼續輸出 ``` <!-- .element: class="fragment" data-fragment-index="2" --> ---- ## ++ 複習 - `++a` 會將 `a` 的值加一,並回傳新值 - `a++` 會回傳原有的值,再將 `a` 值加一 <table><!-- .element: class="fragment" data-fragment-index="0" --> <thead> <th></th> <th>++前置遞增</th> <th>後置遞增++</th> </thead> <tbody> <tr> <td>原始運算式</td> <td> <pre><code class="c++ hljs" >a = <span class="hljs-number">5</span>; ++a; b = a * <span class="hljs-number">7</span> + <span class="hljs-number">1</span>; </code></pre><!-- .element: class="fragment" data-fragment-index="2" --> </td> <td> <pre><code class="c++ hljs" >a = <span class="hljs-number">5</span>; b = a * <span class="hljs-number">7</span> + <span class="hljs-number">1</span>; a++; </code></pre><!-- .element: class="fragment" data-fragment-index="5" --> </td> </tr> <tr> <td>遞增運算式</td> <td> <pre><code class="c++ hljs" >a = <span class="hljs-number">5</span>; b = ++a * <span class="hljs-number">7</span> + <span class="hljs-number">1</span>; </code></pre><!-- .element: class="fragment" data-fragment-index="1" --> </td> <td> <pre><code class="c++ hljs" >a = <span class="hljs-number">5</span>; b = a++ * <span class="hljs-number">7</span> + <span class="hljs-number">1</span>; </code></pre><!-- .element: class="fragment" data-fragment-index="4" --> </td> </tr> <tr> <td>結果</td> <td> <pre><code class="c++ hljs" >a == <span class="hljs-number">6</span> b == <span class="hljs-number">43</span> </code></pre><!-- .element: class="fragment" data-fragment-index="3" --> </td> <td> <pre><code class="c++ hljs" >a == <span class="hljs-number">6</span> b == <span class="hljs-number">36</span> </code></pre><!-- .element: class="fragment" data-fragment-index="6" --> </td> </tr> </tbody> </table> ---- ## +1 - ++ 會改變它所作用的變數的值,+1 不會 <table> <thead> <th></th> <th>遞增運算子</th> <th>算術運算子</th> </thead> <tbody> <tr> <td>運算</td> <td> <pre><code class="c++ hljs" >a = <span class="hljs-number">5</span>; b = ++a; </code></pre> </td> <td> <pre><code class="c++ hljs" >a = <span class="hljs-number">5</span>; b = a + <span class="hljs-number">1</span>; </code></pre> </td> </tr> <tr> <td>結果</td> <td> <pre><code class="c++ hljs" >a == <span class="hljs-number">6</span> b == <span class="hljs-number">6</span> </code></pre> </td> <td> <pre><code class="c++ hljs" >a == <span class="hljs-number">5</span> b == <span class="hljs-number">6</span> </code></pre> </td> </tr> </tbody> </table> ---- ## 結合 while 判斷式 ```c++ // 執行 ? 次 int n = 1; while (n++ <= 10) { // 先判斷完,再 n = n + 1 std::cout << "我愛資芽" << std::endl; } ``` ```c++ // 執行 ? 次 int n = 10; while (n-- > 0) { // 先判斷完,再 n = n - 1 std::cout << "我愛資芽" << std::endl; } ``` <!-- .element: class="fragment" data-fragment-index="1" --> ```c++ int n = 10; while (n--) { // 先判斷完 n != 0,再 n = n - 1 std::cout << "我愛資芽" << std::endl; } ``` <!-- .element: class="fragment" data-fragment-index="2" --> ---- ## 執行次數 <table> <tbody> <tr> <td></td> <td> <pre><code class="c++ hljs" ><span class="hljs-keyword">while</span>(n) { S n--; } </code></pre> </td> <td> <pre><code class="c++ hljs" ><span class="hljs-keyword">while</span>(n--) { S } </code></pre> </td> <td> <pre><code class="c++ hljs" ><span class="hljs-keyword">while</span>(--n) { S } </code></pre> </td> </tr> <tr> <td>S 執行次數</td> <td>n 次</td> <td>n 次</td> <td>n - 1 次</td> </tr> <tr> <td>迴圈結束後 n 的值</td> <td>0</td> <td>-1</td> <td>0</td> </tr> </tbody> </table> --- # for 迴圈 ---- ## 流程控制 - 條件判斷 - `if (表示式) { ... }` - `if (表示式) { ... } else { ... }` - 重複執行 - `while (表示式) { ... }` - `do { ... } while (表示式);` - **`for (初始式; 表示式; 迭帶式) { ... }`** - 迴圈控制 - **`break`** 和 **`continue`** ---- ## for 迴圈 ```c++ for (初始式; 表示式; 迭帶式) { ... } ``` ```c++ for (int i = 1; i <= 10; ++i) { std::cout << "我愛資芽" << std::endl; } ``` 相當於: <table> <tr> <td> <pre><code class="c++ hljs">初始式; <span class="hljs-keyword">while</span> (表示式) { ... 迭帶式; } </code></pre> </td> <td> <pre><code class="c++ hljs"><span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; <span class="hljs-keyword">while</span> (i &lt;= <span class="hljs-number">10</span>) { <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"我愛資芽"</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>; ++i; } </code></pre> </td> </tr> </table> ---- ## for 迴圈流程圖 ![](https://i.imgur.com/xPEOzDq.png) ---- ## 警示型迴圈與計數型迴圈 - 如何決定使用 `for` 或 `while` 迴圈,按特性: - 警示型迴圈 (`while`):該迴圈的結束條件是在某不明確事件,例如輸入到 `-1` (警示值)、讀入到檔案結束 (eof) - 計數型迴圈 (`for`):該迴圈在執行明確次數後會結束的話 ---- ## 練習時間 用 for 來完成 [209 - σ.σ](http://neoj.sprout.tw/problem/209/) 吧! ---- ## 99 乘法表 ```c++ for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= 9; ++j) { cout << i << " * " << j << " = " << i*j << '\t'; } cout << endl; } ``` ---- ## 一維陣列遍歷 ```c++ double l[100], w[100], h[100], volume = 0; for (int i = 0; i < n; ++i) { volume = volume + l[i] * w[i] * h[i]; } ``` - 陣列索引是從 **0** 開始 - 注意陣列大小 - 變數初始化 ---- ## 多重表達式 可以使用「`,`」運算子,加上多重初始式、迭代式 ```c++ for (int i = 0, j = 100; i < 10 && j < 5; ++i, ++j) { std::cout << i << " " << j << std::endl; } ``` ---- ## 無窮迴圈 ```c++ while (true) { std::cout << "我愛資芽" << std::endl; } ``` ```c++ for ( ; ; ) { std::cout << "我愛資芽" << std::endl; } ``` ---- ## break 和 continue - `break` 強制跳離迴圈 - `continue` 跳至下一次迴圈開始 ```c++ // 判斷 n 是否為質數 int n = 87; bool is_prime = true; for (int i = 2; i < n; ++i) { if (n % i == 0) { is_prime = false; break; // 如果是合數,就不用再檢驗 } } ``` <!-- .element: class="fragment" data-fragment-index="1" --> ```c++ // 輸出 1 ~ 10 的奇數 for (int i = 1; i <= 10; ++i) { if (i % 2 == 0) continue; // 跳過偶數 std::cout << i << std::endl; } ``` <!-- .element: class="fragment" data-fragment-index="2" -->

    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