Mes
    • 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
    # <div id=animation_title>變數、邏輯符號、基本語法</div> ###### tags: `C++` **<a href="https://hackmd.io/CSZ3EUqhSRa8AyDyBMq1Bw" class="redlink">點此回到 C++筆記 目錄 </a>** # 變數型態 | int | 整數 | | ------ | --------------------- | | double | 浮點數 | | char | 一個字 ex: "a"、"@" | | string | 字串 | | bool | 布林值ex : true/false | <br> # 邏輯符號 | 符號 | 意義 | | ---- | ------ | | ! | 反敘述 | | && | and | | I I | or | <br> # 常用語法 ## **1. cout** > std::cout << "要輸出的內容" ; ## **2. cin** > std::cin >> "要輸入的內容" ; + 需注意的是,若輸入了一個不符合變數資料型態的值,簡單來說,C++會立起一個cin bad input flag,導致接下來的cin全部被忽略,例如: ```cpp= int main() { int a,b; for(int i=0; i < 5; i++) { cin >> a; cout << a << endl; cin >> b; cout << "旗子已被立起" << endl; } } 輸入: a (b會沒辦法輸入) 輸出: 0 旗子已被立起 0 旗子已被立起 0 旗子已被立起 0 旗子已被立起 0 旗子已被立起 ``` 解決方法:建立一個當輸入錯誤型態時會進入的迴圈,例如: ```cpp= while (cout << "輸入 = " && !(cin >> a)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "輸入無效,請重新輸入"; } ``` 有興趣可以參考<a href="https://blog.csdn.net/maoliran/article/details/51725396">這裡</a>和<a href="https://www.itread01.com/content/1547991755.html">這裡</a>,或去研究緩衝區和流狀態的東西。 ## **3. switch** ```cpp= int number = 2; switch(number) { case 1: //要做的事 std::cout << " " ; break; case 2: //要做的事 std::cout << " " ; break; } ``` ## **4. if,else if** ```cpp= if(grade == 9) { std::cout << " " ; } else if (grade == 10){ std::cout << " " ; } else { std::cout << " " ; } ``` > if,else能夠以三元運算子表示(a ? b : c) > > ex : if(x == true) val =y; > else val =z; > 能夠被寫成: > val = x ? y :z; ## **5. while** > while( A.條件式 ) { B.當條件成立時,就重覆做的事... } ```cpp= int i = 0 ; int n = 10 ; while(i<n) { std::cout << " "; i++; } ``` ## **6. for** > for( [A]一開始先做什麼事 ; [B]條件式 ; [D]等C每作完一次,就做什麼事 ) { [C]當B條件成立時,就重覆做的事... } ```cpp= for (int i=99;i>0;i--) { cout << i << "bottles of pop on the wall." << endl; cout << "Take one down and pass it around." << endl; cout << i-1 << "bottles of pop on the wall." << endl; } ``` 輸出陣列常用單行的for迴圈來印出,ex: ```cpp= for(int i=0; i < sizeof(array)/sizeof(*arr); i++){ cout << arr[i] << endl; } ``` 也可以利用for range的寫法 ```cpp= for(int i : arr ) { cout << i << endl; } ``` 需要注意的是,傳進 for range 的需為一個能夠使用begin()與end()語法的容器,在使用時可以將型態換為auto,就不需要自行判斷型態了,但最好還是了解一下型態,否則出錯時會檢查不出個所以然。 ### **<a href="http://140.115.11.235/~chen/course/Cpp/ch4/2.htm" class="redlink">for迴圈變形 </a>** 下面將介紹幾種For迴圈的變化形式 第一種是在初始條件運算式中使用兩個或更多的變數來控制迴圈,兩個控制變數之間是以逗點","來區隔: ```cpp= #include <iostream> using namespace std; int main() { int x, y; for ( x = 0, y = 0; x + y <= 10; x++ ) { cout << "請輸入y:"; cin >> y; cout << "x+y = " << x + y << endl; } } ``` 每一次迴圈循環,x都會遞增一,y值都會初始化為0,不要忘記這裡XD 因為在迴圈循環時,for迴圈會先執行繼續條件運算式,也就是在使用者輸入y值之前,程式就要先判斷 x+y<=10 是不是對的,如果y沒有初始為0,可能導致意料外的結果。 <br> 第二種是將for陳述中的遞增運算式去除,因為在某些情況下它可能是多餘的,例如下面這個迴圈: ```cpp= for (int x = 0; x != 123; ) { cin >> x; } ``` 可以看見這裡的目的是當輸入123時要停止迴圈,也就是說不需要遞增運算式。 我們還可以把初始條件運算式寫在迴圈外部,例如: ```cpp= int x = 0; for ( ; x != 123; ) { cin >> x; } ``` 但雖然這樣的寫法仍合於語法,但我們並不鼓勵使用,因為它叫不具有結構性,會讓程式流於鬆散。 <br> 第三種是for迴圈形式的無限迴圈,我們只需要讓for陳述裡的三個運算式全都忽略不寫,就會得到一個永遠都不會結束的迴圈,例如: ```cpp= for(;;){ cout << "永不停止的迴圈" << endl; } ``` 事實上這就等於while(1)的迴圈,需要再手動break掉迴圈。 第四種是沒有body的for迴圈,這個迴圈不執行任何事。那這樣的迴圈能有什麼作用呢? 它可以產生延遲,在一些特殊場合是很有用的技巧(如工業控制),如: ```cpp= for(int t = 0; t < value; t++); ``` value通常會是一個long int,譬如我們設value = 10000000,則這個迴圈會空轉1000萬次,由於每一次循環都必須執行兩個運算式 t\<value 和 t++ ,因此會花掉一些執行時間,對整個程式而言,就如同時間延遲的作用。 ## **7. goto** > label:{} > goto label; ```cpp= #include <iostream> using namespace std; int main() { for (int i = 0 ; i < 10 ; i ++ ) { cout << endl << " i= " << i << " and j= "; for (int j = 0 ; j < 10 ; j ++ ) { cout << j << " "; if (i == 5) { cout << endl; goto here ; } } } cout << "測試"; for (int i = 3 ; i < 10 ; i ++ ) { cout << "i= " << i << "and j= "; for (int j = 4 ; j < 10 ; j ++ ) { cout << j << " | "; if (i == 5) { cout << endl; goto here2 ; } } } here :{ cout << "stop 1 !!"; } here2 :{ cout << "stop 2 !!"; } } ``` > **實際輸出後會發現,cout << "測試"; 與第二個for迴圈都沒有被執行,這是因為當第一個迴圈的i==5時,進入了if,因此吃到了goto here; 導致程式直接跳到了here的標籤處而我們也可以發現here2這個標籤下的cout << "stop 2 !!"; 也被執行了,這是因為標籤在沒有被觸發的情況下是沒有作用的,因此於標籤裡的程式和沒在標籤裡的程式是沒有差別的。** > <style> .green { color:#29E5A9; } .brown { color:#990000; } .pink { color:#DD9FBD; } .red { color:#E71B18 ; } .blue { color:#0b5394; } .purple { color:#AC9FDD; } @-webkit-keyframes A { 0% { color:#C10066;} 10% { color: #CC0000;} 20% { color: #E63F00; } 30% { color:#EE7700; } 40% { color: #DDAA00; } 50% { color:#EEEE00;} 60% { color: #99DD00;} 70% { color:#66DD00;} 80% { color: #00DDDD;} 90% { color: #0044BB;} 100% { color: #A500CC;} } #animation_title{ animation: A 3s ease 0s infinite alternate; -webkit-animation: A 3s ease 0s infinite alternate; } </style> <style> a.redlink { color:#DB1859; } a.redlink:link { color:#DB1859; text-decoration:none; } a.redlink:visiteid { color:#DB1859; text-decoration:none; } a.redlink:hover { color:#19CABC; text-decoration:none; } a.redlink:active { color:#000000; text-decoration:underline; background:#FFFFFF; } </style> <style type="text/css"> h1 { font-size:; color:#0b5394; } h2 { font-size:; color:#0b5394; } p { font-size:; color:; } h3 { font-size: ; color:#990000; } h4 { font-size:; color:#990000; } </style>

    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