Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 & Chapter 3.

Similar presentations


Presentation on theme: "Chapter 2 & Chapter 3."— Presentation transcript:

1 Chapter 2 & Chapter 3

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

3 變數型態和型態轉換 value = (value-count)*(count-num)/many+num/many;
double value = 31.0; int count = 16; float many = 20.0f; char num = 4; value = (value-count)*(count-num)/many+num/many;

4 Explicit casts 明確轉換 static_cast<the_type_to_convert_to>(expression)

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

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

7 運算種類 算術運算 關係運算 邏輯運算 以位元為單位 以運算元為單位

8 算術運算子 算術運算子 意義 + - * / % 取餘數 ++、-- 遞增、遞減 +、- 正負號

9 關係運算子 關係運算子 意義 < 小於 > 大於 == 等於 <= 小於等於 >= 大於等於 != 不等於

10 邏輯運算子 基本邏輯運算子 意義 && 運算元作AND運算 || 運算元OR運算 ! 運算元NOT運算

11 位元運算子 bitwise AND & bitwise OR | bitwise exclusive OR ^ bitwise NOT ~
shift right >> shift left <<

12 AND位元運算子 bitwise AND 1

13 Example letter1:0x41 char letter1=‘A’, letter2=‘Z’, result=0; 1
result=letter1 &letter2; letter1:0x41 1 letter2:0x5A result

14 OR位元運算子 bitwise OR 1

15 XOR位元運算子 bitwise EOR 1

16 NOT位元運算子 bitwise NOT 1

17 位移位元運算子 16387=> 向左shift 2位 1 1 =12

18 位移位元運算子 16387=> 向右shift 2位 1 1 =4096

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

20 變數的範圍(scope) 變數名稱在程式裡的有效區域

21 變數的生命週期 (storage duration)
automatic static dynamic (describe in ch4)

22 自動變數 (auto) auto 為預設值,當變數宣告時,沒有額外以儲存類別關鍵字宣告,即表示其為 auto 類別。
自動變數的生命期和宣告的位置有關 區塊結束變數生命結束 區塊重新執行, 變數重新被建立並指定初值

23 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 count1 += 3; // This affects the inner count1 count3 += count2; } // ...and ends here

24 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

25 靜態變數 (static) 區域靜態變數 全域靜態變數

26 區域靜態變數 當函式結束, 區域靜態變數的生命期並不會跟著結束, 同時還會保有原來的值, 直到整個程式結束時, 變數的生命期才會結束
區域靜態變數若無設定初值,則預設為 0 區域靜態變數只能被該宣告者函式使用

27 全域靜態變數 全域靜態變數的有效範圍只限於該檔案內的函式 範例: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); }

28 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;

29 全域變數與區域變數 區域變數(local variable)只在某個範圍裡有效,超出範圍就變成無效了
全域變數(global variable)的有效範圍是變數宣告之後的所有函式

30 Figure (page 82) Example.cpp value2 Value1 value3 value4 value5 value1
long value1; Value1 int main() { int calue2; int value3; } value2 value3 int value4; value4 int function(int) { long value5; int value1; } value5 value1

31 區域變數的獨立性 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?"); printf("sub:You are %d years old.\n", age);

32 隱藏全域變數 全域變數和區域變數可以同名,然而在函式中使用的變數和全域變數同名時,全域變數的有效範圍會被遮蔽
int i=123; /*全域變數*/ main() { int i=456; /*區域變數*/ printf("%d",i); } 全域變數和區域變數可以同名,然而在函式中使用的變數和全域變數同名時,全域變數的有效範圍會被遮蔽 不建議使用,因為很容易搞混變數名稱而出錯

33 全域、區域變數的關係 相同有效範圍的變數,名稱不可重複。相對地,不同範圍的區域變數,名稱可以相同。區域變數和全域變數的名稱也可以相同。
不同有效範圍的同名區域變數不會相互影響。 區域變數和全域變數同名時,在區域變數的有效範圍,全域變數會受到遮蔽。

34 使用全域變數時的注意事項 在全域變數宣告之後所定義的函式,才能使用該全域變數。 儘量少用全域變數。 小心使用全域變數,並避免誤改全域變數值。
全域變數在宣告後,預設值為0。

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

36 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;

37 宣告名稱空間 namespace mystuff {
// Code that I want to have in the namespace mystuff… }

38 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;

39 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;

40 Example (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;

41 Chapter 3 決策與迴圈

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

43 流程控制 結構化程式設計 循序結構 選擇結構 重複結構

44 循序結構 Example: cout << endl; cout << num1 << num2;

45 選擇結構 if 敘述 if (測試條件) { //敘述 }

46 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 << "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;

47 選擇結構 if-else敘述 if (測試條件) { 敘述區塊1 } else 敘述區塊2

48 Example Ex3_02.cpp

49 選擇結構 巢狀 If 格式 If 條件1 Then If 條件2 Then 敘述區塊1-1 Else 敘述區塊1-2 End If …….
敘述區塊2


Download ppt "Chapter 2 & Chapter 3."

Similar presentations


Ads by Google