sunfrancis12
    • 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
    # Ch04 while 迴圈 > 上一章: [Ch03 if 條件判斷](https://hackmd.io/@sunfrancis12/SJ_9xCjzT) > 下一章: [Ch05 for 迴圈](https://hackmd.io/@sunfrancis12/HyL-bCszT) > 回目錄: [NTCU程式戰鬥營C講義](https://hackmd.io/@sunfrancis12/ByfdXdjG6) ## 什麼是迴圈(Loop) 我們剛剛前面實作了許多程式,像是閏年判斷,假如今天我們需要判斷的不是一筆資料,而是一千筆、一萬筆呢? 我們總不可能把程式複製一千遍、一萬遍吧,這樣就太笨了,而且要是並非每次的資料都是一千筆,有可能是兩、三千筆,那麼直接將程式寫死也太不切實際了。 為此,我們需要一個可以**重複執行而且具有條件判斷**的結構,迴圈的概念也就此誕生。 ## While迴圈 While迴圈的用法跟邏輯跟if差不多,只是差別在於執行一次跟多次的差別 While用法: * 當滿足條件時,在while{}裡的程式將會再執行一次,直到條件不滿足或者程式中斷或終止 ```c= while(條件) ``` 假如我們要寫一個可以印出1-10的程式,while的條件可能為如下 ```c= while(num<=10){ prinf("%d",num); num++; } ``` 範例程式: ```c= #include<stdio.h> int main(){ int num=1; while(num<=10){ printf("%d ",num); num++; } } ``` 輸出結果: ```c= 1 2 3 4 5 6 7 8 9 10 ``` <a style="color:orange">【例題】</a> > 輸入一個大於0的整數n,並印出1-n的所有數字,而且以10個數字為一排 > 範例測資: > > 輸入: >13 > 輸出: > 1 2 3 4 5 6 7 8 9 10 > 11 12 13 範例程式: ```c= #include<stdio.h> int main(){ int num; scanf("%d",&num); int count=1; while(count<=num){ printf("%d ",count); if(count%10==0) printf("\n"); count++; } } ``` 輸入內容: ```c= 51 ``` 輸出結果: ```c= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ``` <a style="color:orange">【例題】</a> > 輸入一個數字,並判斷其是否為質數 > 範例測資: > 輸入: > 17 > 輸出: > 17是質數 範例解答: ```c= #include<stdio.h> int main(){ int num; printf("請輸入一個數字: "); scanf("%d",&num); int count=2,sum=0; //sum為因數(扣掉1和自己)的數量 while(count<num){ if(num%count==0) sum++; //當1~num之間有數字可以整除num,則sum++ count++; } if(sum>0 || num==1) printf("%d 不是質數",num); else printf("%d 是質數",num); } ``` 輸入內容(依次輸入): ```c= 17 63 1 ``` 輸出結果(依次輸出): ```c= 請輸入一個數字: 17 17 不是質數 請輸入一個數字: 63 63 不是質數 請輸入一個數字: 1 1 不是質數 ``` ## 特殊條件迴圈 我們可以寫一個迴圈,把輸入的數字印出來 ```c= #include<stdio.h> int main(){ int num; printf("請輸入一個數字: "); while(scanf("%d",&num)){ printf("輸入的數字為: %d\n",num); printf("請輸入一個數字: "); } } ``` :::info 更多關於迴圈的技巧以及條件會在之後的章節提到 ::: 輸入內容: ```c= 12 13 47 38430 ``` 輸出結果: ```c= 請輸入一個數字: 12 輸入的數字為: 12 請輸入一個數字: 13 輸入的數字為: 13 請輸入一個數字: 47 輸入的數字為: 47 請輸入一個數字: 34840 輸入的數字為: 34840 ``` 我們也可以結合上一張學到的`Condition`來寫個無窮迴圈 ```c= #include<stdio.h> int main(){ int num=1; while(1){ printf("%d ",num); num++; } } ``` ## 跳脫、跳過迴圈 我們再利用迴圈跑程式時候,常常會遇到某些可以跳過執行,或者跳出迴圈的情況。 我們需要兩個指令: * `continue`(跳過執行) * `break`(跳脫迴圈) 通常我們會搭配if()做使用: ```c= if(....) continue; //跳過下方將要的執行的程式,回到while判斷條件 if(....) break; //跳出整個現在的迴圈 ``` 使用`continue`跳過可以被三整除的數字: ```c= #include<stdio.h> int main(){ int num; printf("請輸入一個數字: "); scanf("%d",&num); while(num>0){ if(num%3==0){ num--; continue; } printf("現在num的值是%d\n",num); num--; } } ``` 輸入內容: ```c= 10 ``` 輸出結果: ```c= 請輸入一個數字: 10 現在num的值是10 現在num的值是8 現在num的值是7 現在num的值是5 現在num的值是4 現在num的值是2 現在num的值是1 ``` 當輸入為0,用`break`跳出迴圈: ```c= #include<stdio.h> int main(){ int num,sum=0; printf("請輸入一個數字: "); while(scanf("%d",&num)){ if(num==0) break; printf("輸入的數字為: %d\n",num); printf("請輸入一個數字: "); sum++; } printf("總共輸入了%d個數字",sum); } ``` 輸入內容: ```c= 6 3 6 8 2 4 9 0 ``` 輸出結果: ```c= 請輸入一個數字: 6 輸入的數字為: 6 請輸入一個數字: 3 輸入的數字為: 3 請輸入一個數字: 6 輸入的數字為: 6 請輸入一個數字: 8 輸入的數字為: 8 請輸入一個數字: 2 輸入的數字為: 2 請輸入一個數字: 4 輸入的數字為: 4 請輸入一個數字: 9 輸入的數字為: 9 請輸入一個數字: 0 總共輸入了7個數字 ``` <a style="color:orange">【例題】</a> > 輸入一個數字,並判斷其是否為質數 > > 範例測資: > 輸入: > 17 > 24 > 46 > 59 > > 輸出: > 17是質數 > 24 不是質數 > 46 不是質數 > 59 是質數 範例解答: ```c= #include<stdio.h> int main(){ int num; printf("請輸入一個數字: "); scanf("%d",&num); int count=2,sum=0; //sum為因數(扣掉1和自己)的數量 while(count<num){ if(num%count==0){ sum++; break; } count++; } if(sum>0 || num==1) printf("%d 不是質數",num); else printf("%d 是質數",num); } ``` --- > 上一章: [Ch03 if 條件判斷](https://hackmd.io/@sunfrancis12/SJ_9xCjzT) > 下一章: [Ch05 for 迴圈](https://hackmd.io/@sunfrancis12/HyL-bCszT) > 回目錄: [NTCU程式戰鬥營C講義](https://hackmd.io/@sunfrancis12/ByfdXdjG6)

    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