# C++ 隨手筆記#1 [TOC] ## Visual Studio專案建置 ### 新建cpp >[color=Red] **檔案<i class="fa fa-file-text"></i>** :arrow_right: **新增** :arrow_right: **專案**  > [color=Orange]新建成功的話你將會在**Solution Explorer**看見已建置的專案  :::info 如果在 **Visual Studio** 沒有看到 **Solution Explorer**,可以至**檢視(View)** :arrow_right: **方案總管(Solution Explorer)** :exclamation: :exclamation: :exclamation: ::: ### 編譯專案 >[color=Yellow]如果要編譯專案看有沒有錯誤的話,可以至 **方案總管(Solution Explorer)** :arrow_right: **右鍵專案** :arrow_right: **建置** 就可以進行編譯 :mega:  :::warning 如果是空白專案直接編譯,編譯器一定會生氣給你看。C++必須有**進入點(Entry point)**,也就是**main()**,我們可以參照下方程式,就可以解決上述問題 :+1: 範例程式: ``` C++= int main() { } ``` ::: ## Hello World! ### 甚麼是函式庫? >[color=LightGreen] 很多人都會問,函式庫是什麼,要怎麼去使用它。舉個例子,假如你今天衣服髒了,你可以選擇手洗,但是就比較麻煩;所以大部分的人偏好是丟進洗衣機,出來就是一件乾淨的衣服。==函式庫就類似洗衣機的概念,你今天把東西丟給它,它會直接幫你處理好==。:star2: ### 使用函式庫 >[color=Yellow]踏上C++的不歸路後,你將會與它白頭偕老,它的使用方式很簡單 ==**#include<函式庫>**== >[color=Orange]下方使用的函式庫為 ==**iostream**==,是C++最常見的函式庫之一,引用它之後就可以使用一些**輸入**和**輸出**的function >[color=LightBlue]==**cin**== 和 ==**cout**== 也是最常用來輸入文字和輸出文字的function之一 >[color=Purple] ==**system("pause")**== 用來暫停視窗,讓我們看見顯示結果 :tv: ``` C++= #include <iostream> int main() { std:: cout << "Hello World!"; system("pause"); } ``` ### 輸出結果 >[color=Red]按照先前的編譯方法後,接著點選 **偵錯(Debug)** :arrow_right: **啟動但不偵錯(Start Without Debugging)** :::info 也可以使用快捷鍵 **CTRL+F5** 進行偵錯 :mega: :::  >[color=Gold]成功印出**Hello World!** :tada:  ## 變數(Variable) ### 甚麼是變數 >[color=Red]**變數**,工程師的談話間一定會有的一個詞。沒有了變數,就沒有了程式。就像是海洋沒有了水,何來的水中生物,是吧? >[color=Orange]變數擁有不同的種類,諸如*int*、*float*、*string*...等等。每種變數就像是不同的**容器**,只能裝載**特定**的東西。例如: 1,4,20整數類別只能裝在**int**;1.5,0.003,999.0小數點類別的只能裝在**float**;a,b,h字元類別的只能裝在**char** ### 使用變數 >[color=Yellow]宣告方式通常是 ==**類別**== ==**命名**==,程式碼如下: ``` C++= #include <iostream> int main() { int myintname; char mystringname; float myfloatname; } ``` >[color=LightGreen]也可以給變數一個 ==**初始數值**==,程式如下: ``` C++= int main() { int myintname=3; char mystringname= 'n'; float myfloatname=3.1415926; } ``` ### 輸出變數 >[color=LightBlue]一樣使用到先前的 ==**cout**== 函式,我們將要顯示在結果上的變數放至 ==**<<**== 後方 >[color=Purple] 這次額外加入了 ==**using namespace std**==,如果不加上此宣告的話,**cout**的使用都要加上**std**,但如果加上後以後有關**std**的函式都可省略 ``` C++= #include <iostream> using namespace std; int main() { int myintname=3; char mystringname= 'n'; float myfloatname=3.1415926; cout << myintname << endl; cout << myfloatname << endl; cout << mystringname << endl; system("pause"); } ``` 輸出結果:  ## 運算元(Operator) ### 關係運算元(Relational Operators) | 運算元 | 名稱 | 範例 |布林 | | :--------: | -------- | -------- |--------| | > | greater than | 10>1 | true | | < | less than | 8<5 | false | | <= | less than or equal | 5<=5 | true | | == | equal to | 2==5 | false | | != | not equal to | 2!=5 | true | | ! | negation operator | !false | true | ### 增量運算元 >[color=Red]這邊設定**i**的初始值為1 | 運算元 | 範例 | 結果 | | :--------: | -------- | -------- | | += | i+=1 | 2 | | -= | i-=1 | 0 | | *= | i*=6 | 6 | | /= | i/=2 | 0.5 | ## if的三種應用 ### if >[color=Orange] 當**if** ==條件符合==時,便會執行括號裡的內容 >[color=Red]如果我有錢的話,我就會去環遊世界。這句話成立的條件是==我有錢==,當然這也可以用程式來表示: ``` C++= if( 我有錢 ) { 環遊世界 } ``` ### if/else >[color=Yellow] 當**if** ==條件符合==時,便會執行括號裡的內容,不然就執行else >[color=LightGreen]如果我有錢的話,我就會去環遊世界。但是我沒有錢,所以在寫程式 :cry: ``` C++= if( 我有錢 ) { 環遊世界 } else { 寫程式 } ``` ### else if >[color=LightBlue]通常銜接在**if**之後,如果前一個條件==不符合==,接著才會判斷另一個條件 >[color=LightGreen]我挑選另一半的標準是學歷台清交,如果沒有的話那至少身高要170公分 :face_with_monocle: ``` C++= if( 學歷台清交 ) { 可以交往 } else if(身高至少170) { 可以交往 } ``` >下一篇 [C++隨手筆記#2](https://hackmd.io/@Neroal/B1gXca1HI) ###### tags: `C++` `Variables` `Operator` `if/else`
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.