Basics

 

諸多 statements(陳述,述句)以分號(;)斷句. 可任意空格或換行.

註解(comment)以 /* 註解內容 */ 或 //註解內容 方式呈現

n            n            程式要有清楚的巢狀結構

n            n            養成寫註解的習慣

 

一個 project 的主程式只能有一個:

 

#include <標準表頭檔>

#include "自訂檔"

// 需要用到的表頭檔及自訂檔等前置處理(preprocessor)

int main()

{

陳述

return 0;

}

 

C/C++中的保留字(keywords)

auto

delete

goto

public

template

break

do

if

register

this

case

double

inline

return

typedef

catch

else

int

short

union

char

enum

long

signed

unsigned

class

extern

new

sizeof

virtual

const

float

operator

static

void

continue

for

private

struct

volatile

default

friend

protected

switch

while

 

其中藍色為C++才有的. 不可使用保留字命名 識別名稱(identifier)及變量(variable)

 

常用的標準表頭檔

<stdlib> <iostream> <fstream> <istream> <ostream> <iomanip>

<math> <time> <algorithm> <vector> <string> ......

 

常用的內建數據型態(data type)

實數: 整數 int, short, long, unsigned,

浮點數 float, double, long double, unsigned float, unsigned double, unsigned long double

複數: complex<實數><實數> (這叫 複數的template)

邏輯: bool (值為 true 或 false, 保留字)

字元: char (如 ’A’,’a’,..., 參見 ASCII字元)

字串: string (如 ”abcdefg”)


算子(operator)

uniary算子:

++, --,

-(負號), !(邏輯的否定), &(address-of), *(referencing)

binary算子:

實數運算:

+(加),-(減),*(乘),/(除), %(mod),

+= , -=, *=, /=, %= ,

==(等於), !=(不等於), < , <= , > , >= ,

二進位運算: >> , << , & , |,

邏輯運算: && , ||

 

有生存空間/範圍(scope)的

迴圈(loop)

for(起始設定;條件;完成後之動作) {陳述}

while(條件) {陳述}

do {陳述} while(條件);

用 continue 略過或 break 結束迴圈.

 

邏輯條件設定

一個判斷點至多兩個叉岔:

if(條件) {陳述}

else{陳述}

或

if(條件1) {陳述}

else if(條件2) {陳述}

 

一個判斷點有好幾個岔:

switch(情況)

{

case 情況一: 陳述

case 情況二: 陳述

........

default: 陳述

}

情況一,二,... 為情況的實例, 不可為變量. 可用 break 跳過各後續 cases. 如不 break, 則依序執行後續 cases.

default 用以處理未列出之case.

 

{}內仍可有 for, while, do while, if陳述, 是為巢狀結構(nested structure).

變量在 某條件式/迴圈{}裡宣告產生,

 

例如,

while( a > 1 )

{

  陳述

  int b = 2;

  陳述

}

 

則, 離開while迴圈後就沒有b這個東西了. 這一對{}就是所謂b的生存空間/範圍(scope). while 換成for, if等也是這樣.