Chapter 2 & Chapter 3

Preview:

DESCRIPTION

Chapter 2 & Chapter 3. 變數型態和型態轉換. 原則 : 範圍小的型態轉成大的型態 page 69. 變數型態和型態轉換. double value = 31.0; int count = 16; float many = 20.0f; char num = 4; value = (value-count)*(count-num)/many+num/many;. Explicit casts 明確轉換. static_cast(expression). - PowerPoint PPT Presentation

Citation preview

Chapter 2 & Chapter 3

變數型態和型態轉換 原則 : 範圍小的型態轉成大的型態 page 69

變數型態和型態轉換double value = 31.0;

int count = 16;

float many = 20.0f;

char num = 4;

value =

(value-count)*(count-num)/many+num/many;

Explicit casts 明確轉換 static_cast<the_type_to_convert_to>(expressio

n)

Keywords about casting

static_cast: 程式編譯時靜態檢查轉換 dynamic_cast: 程式執行時動態檢查 const_cast: 常數轉換 reinterpret_cast: 重新解釋的轉換 - 無條件的

型態轉換

運算子的優先權 table (page 67) 當運算子同時具有一元運算及二元運算的意義 ,

一元運算優先權高於二元運算

運算種類 算術運算 關係運算 邏輯運算

以位元為單位 以運算元為單位

算術運算子

算術運算子 意義+ 加- 減* 乘/ 除

% 取餘數++ 、 -- 遞增、遞減+ 、 - 正負號

關係運算子

關係運算子 意義< 小於> 大於

== 等於<= 小於等於>= 大於等於!= 不等於

邏輯運算子

基本邏輯運算子 意義

&& 運算元作 AND 運算

|| 運算元 OR 運算

! 運算元 NOT 運算

位元運算子bitwise AND &

bitwise OR |

bitwise exclusive OR ^

bitwise NOT ~

shift right >>

shift left <<

AND 位元運算子

bitwise AND 0 1

0 0 0

1 0 1

Example

char letter1=‘A’, letter2=‘Z’, result=0;

result=letter1 &letter2;

letter1:0x41

0 1 0 0 0 0 0 1

letter2:0x5A

0 1 0 1 1 0 1 0

result 0 1 0 0 0 0 0 0

OR 位元運算子

bitwise OR 0 1

0 0 0

1 0 1

XOR 位元運算子

bitwise EOR 0 1

0 0 1

1 1 0

NOT 位元運算子

bitwise NOT 0 1

1 0

位移位元運算子 16387=>0100000000000011

0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0

0000000000001100=12

向左 shift 2 位

位移位元運算子 16387=>0100000000000011

0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

0001000000000000=4096

向右 shift 2 位

範圍與生命週期 空間 時間 有效範圍,指的是變數本身的作用及影響範圍 生命週期是變數於程式執行階段,實際存在的

時間區段

變數的範圍 (scope)

變數名稱在程式裡的有效區域

變數的生命週期 (storage duration)

變數生命週期尤其儲存期間 (storage duration)決定

storage duration 分三種 automatic static dynamic (describe in ch4)

自動變數 (auto)

auto 為預設值,當變數宣告時,沒有額外以儲存類別關鍵字宣告,即表示其為 auto 類別。

自動變數的生命期和宣告的位置有關 區塊結束變數生命結束 區塊重新執行 , 變數重新被建立並指定初值

Example (EX2_06.cpp)// EX2_06.CPP// Demonstrating variable scope#include <iostream>

using namespace std;

int main(){ // Function scope starts here int count1 = 10; int count3 = 50; cout << endl << "Value of outer count1 = " << count1 << endl;

{ // New scope starts here... int count1 = 20; // This hides the outer count1 int count2 = 30; cout << "Value of inner count1 = " << count1 << endl; count1 += 3; // This affects the inner count1 count3 += count2; } // ...and ends here

Example (EX2_06.cpp) (cont.)

cout << "Value of outer count1 = " << count1 << endl << "Value of outer count3 = " << count3 << endl;

// cout << count2 << endl; // uncomment to get an error

return 0;} // Function scope ends here

靜態變數 (static)

區域靜態變數 全域靜態變數

區域靜態變數 當函式結束 , 區域靜態變數的生命期並不會跟

著結束 , 同時還會保有原來的值 , 直到整個程式結束時 , 變數的生命期才會結束

區域靜態變數若無設定初值,則預設為 0 區域靜態變數只能被該宣告者函式使用

全域靜態變數

全域靜態變數的有效範圍只限於該檔案內的函式

範例: CH6-6C1.C

static int i=1;void main(){   void f();   f();   printf("main:i=%d", i);}

範例: CH6-6C2.C

static int i=2;void f(){   printf("f:i=%d\n", i);}

Example

#include <iostream>

using namespace std;

static int i;

void main(){

void f(void);f();

cout << "main:i=" << i << endl;}

void f(void){

static int i=2;cout << "\nf:i=" << i << endl;

}

全域變數與區域變數 區域變數( local variable )只在某個範圍裡

有效,超出範圍就變成無效了 全域變數( global variable )的有效範圍是變

數宣告之後的所有函式

Figure (page 82)Example.cpp

int main(){ int calue2; … { int value3; }}

int function(int){ long value5; int value1; …

}

int value4;

long value1;

Value1

value2value3

value4value5value1

區域變數的獨立性

main(){

int age; /* 區域變數 */printf("main:How old are you?");scanf("%d", &age);sub();printf("main:You are %d years old.\n", age);

}

sub(){

int age; /* 區域變數 */printf("sub:How old are you?");scanf("%d", &age);printf("sub:You are %d years old.\n", age);

}

隱藏全域變數

全域變數和區域變數可以同名,然而在函式中使用的變數和全域變數同名時,全域變數的有效範圍會被遮蔽

不建議使用,因為很容易搞混變數名稱而出錯

int i=123; /* 全域變數 */main(){

int i=456; /* 區域變數 */printf("%d",i);

}

全域、區域變數的關係 相同有效範圍的變數,名稱不可重複。相對地,

不同範圍的區域變數,名稱可以相同。區域變數和全域變數的名稱也可以相同。

不同有效範圍的同名區域變數不會相互影響。 區域變數和全域變數同名時,在區域變數的

有效範圍,全域變數會受到遮蔽。

使用全域變數時的注意事項 在全域變數宣告之後所定義的函式,才能使用

該全域變數。 儘量少用全域變數。 小心使用全域變數,並避免誤改全域變數值。 全域變數在宣告後,預設值為 0 。

Namespace

是否可以在程式中宣告名稱為 cout 的變數

Example-Modify from EX2_08.cpp

//Modify the example of Ex2_08.cpp

#include<iostream>

int cout =0;

int main(){

std::cout << "enter an integer: ";std::cin >> cout;std::cout << "\nYou entered " << cout

<< std:: endl;return 0;

}

未使用假指令using namespace std;

宣告名稱空間

namespace mystuff{

// Code that I want to have in the namespace mystuff…

}

Example (Ex2_09.cpp)

// EX2_09.CPP// Declaring a namespace#include <iostream>

namespace myStuff{

int value = 0;}

int main(){

std::cout << "enter an integer: ";std::cin >> myStuff::value;std::cout << "\nYou entered " << myStuff::value

<< std::endl;

return 0;}

Example (Ex2_10.cpp)

// EX2_10.CPP// Using a using directive#include <iostream>

namespace myStuff{

int value = 0;}

using namespace myStuff;

int main(){

std::cout << "enter an integer: ";std::cin >> value;std::cout << "\nYou entered " << value

<< std::endl;

return 0;}

Example (Modify from Ex2_10.cpp)

// Modify from EX2_10.CPP// Using a using directive#include <iostream>

namespace myStuff{

int value = 0;}

using namespace myStuff;using namespace std;

int main(){

cout << "enter an integer: ";cin >> value;cout << "\nYou entered " << value << endl;

return 0;}

Chapter 3

決策與迴圈

In this chapter

資料的比較 由比較結果決定程式執行順序 邏輯運算子及邏輯運算式 多重選擇 迴圈的撰寫與使用

流程控制 結構化程式設計

循序結構 選擇結構 重複結構

循序結構 Example:

cout << endl;

cout << num1 << num2;

cout << endl;

選擇結構 if 敘述

if ( 測試條件 )

{

// 敘述 }

Example (Ex3_01.cpp)

// EX3_01.CPP// A nested if demonstration#include <iostream>

using namespace std;

int main(){ char letter = 0; // Store input in here

cout << endl << "Enter a letter: "; // Prompt for the input cin >> letter; // then read a character

if(letter >= 'A') // Test for 'A' or larger if(letter <= 'Z') // Test for 'Z' or smaller { cout << endl << "You entered a capital letter." << endl; return 0; }

if(letter >= 'a') // Test for 'a' or larger

if(letter <= 'z') // Test for 'z' or smaller

{

cout << endl

<< "You entered a small letter."

<< endl;

return 0;

}

cout << endl << "You did not enter a letter." << endl;

return 0;

}

if ( 測試條件 )

{

敘述區塊 1

}

else

{

敘述區塊 2

}

選擇結構 if-else 敘述

Example Ex3_02.cpp

選擇結構 巢狀 If 格式

If 條件 1 Then

If 條件 2 Then

敘述區塊 1-1

Else

敘述區塊 1-2

End If

…….

Else

敘述區塊 2

End If

Recommended