CC type modifier data type bytes char 1
C/C++各種資料型態的儲值範圍 type modifier data type bytes char 1 range -128 ~ 127 unsigned char int 1 4 0 ~ 255 -2147483648 ~ 2147483647 short long unsigned short unsigned long int int int float double 2 4 4 8 -32768 ~ 32767 -2147483648 ~ 2147483647 0 ~ 4294967295 0 ~ 65535 0 ~ 4294967295 3. 4 E-38 ~ 3. 4 E+38 1. 7 E-308 ~ 1. 7 E+308 *可使用 sizeof ( ) 來求出資料型態在記憶體中的儲存長度(bytes)。
Java 各種資料型態的儲值範圍 Name byte short int long float double char boolean Numeric Data Types Domain Storage size 8 -bit signed -27 ~ 27 -1 16 -bit signed -215 ~ 215 -1 32 -bit signed -231 ~ 231 -1 64 -bit signed -263 ~ 263 -1 -3. 4 E 38~3. 4 E 38 32 -bit IEEE-754 -1. 7 E 308~1. 7 E 308 64 -bit IEEE-754 16 bits 0~ 216 -1 (65535) 16 bits true or false
Operator Precedence chart Precedence Operator Highest order casting ++ and -- (prefix) *, /, % +, <, <=, >, >= ==, != && || =, +=, -=, *=, /=, %= Lowest order ++ and -- (postfix)
Java print() & println()的使用範例(1) Statement: Displaying: System. out. println(2+1+"="+2+1); 3=21 Statemetn: System. out. println(""+2+1+"="+2+1); Displaying: 21=21 System. out. print("line 1"); 當第一個字串出現後 接下來其餘各項均當 成字串處理 System. out. print("line 2n"); System. out. println("line 3"); System. out. println("line 4n"); line 1 line 2 line 3 line 4
Java print() & println()的使用範例(2) System. out. println("012="+012 +" 12="+12 +" 0 x 12="+0 x 12 +"n"); Output : 012=10 12=12 0 x 12=18 System. out. println("c: backup=>c: \backup"); System. out. println(""\"); Output : cackup=>c: backup ca c : cackup cacku cack cac
Java 陣列的宣告與使用範例 (1) Step 1: 宣告參考(reference) Data. Type[] array. Name; or Data. Type array. Name[]; Ex. int[] ary. Value; or int ary. Value[]; 現在我們祇是得到一個可以存取資料的reference,陣列的大小與所需的記憶 體空間,系統並未配置給該矩陣變數。 Step 2: 產生真正的陣列 array. Name = new Datatype[number_of_Elements]; Ex. ary. Value = new int[10]; 現在我們有了: ary. Value[0], ary. Value[1], ary. Value[2], ary. Value[3], ary. Value[4], ary. Value[5], ary. Value[6], ary. Value[7], ary. Value[8], ary. Value[9] 1) Zero-based : index starting from zero. 2) The size of the array, ary. Value. length =>10
Java 陣列的宣告與使用範例 (2) 宣告陣列並同時給予初值,此時甚至不必指定陣列的大小, 會自動計算該陣列的大小。 Data. Type array. Name[] = {v 1, v 2, … , vn }; or Data. Type [] array. Name = {v 1, v 2, … , vn }; Ex. int ary. Int[] = { 10, 20, 30, 40, 50 }; int[] ary. Int = { 11, 22, 33, 44, 55 }; The size of the array ary. Int, ary. Int. length => 5
Java 陣列的宣告與使用範例 (3) int i; // an integer variable int[] ia, ib; // Both ia & ib are array ia= new int[5]; ib= new int[5]; // requests for array // Error below, int[] required but int found. i= new int[5]; What following really are ? int ic[], id; // ic is an array, but id is an integer. ic= new int[5]; id= new int[5]; // Error ! Id is not an array ! 1) Type array. Name[]; // array requested, 2) array. Name= new Type[#Of. Element]; // really obtained the array
Java 運算式的簡寫表示法 a = a + (value); ==> a += (value); a = a + (expression); ==> a += (expression); Ex. a = a+2; same as a += 2; a = a + b*b; same as a += b*b; 其中的運算子可以是下述的任一個: +、-、*、/、%、&、|、^、>>、<< a &= (expression); a -= (expression); a >>= (expression); a ^= (expression); … , etc.
Ex. if statement if ( a>0 ) System. out. println (“a>0”); System. out. println(“next statement”); Ex. if-else statement if ( a>0 ) System. out. print(“a>0n”); else System. out. print(“a<=0n”); System. out. printf(“next statement”);
程式碼與對應的流程圖 是描述左圖的程式碼? no int a=-1; a!=0 if ( a!=0 ) yes a>0 if ( a>0 ) System. out. print(“a>0n”); else no printf(“a=0n”) System. out. print(“a=0n”); What is the output ? a=0 敘述next yes printf(“a>0n”)
Transfer score to grade if ( score>=90 ) System. out. print(“Grade A”); else if ( score>=80 ) System. out. print(“Grade B”); else if ( score>=70 ) System. out. print(“Grade C”); else if ( score>=60 ) System. out. print(“Grade D”) else 程式碼對齊: 將else 前移。 System. out. print(“Grade E”);
if ( score>=90 ) System. out. print(“Grade A”); 對應展開上一 頁的if-else的 配對關係 else if ( score>=80 ) System. out. print(“Grade B”); else if ( score>=70 ) System. out. print(“Grade C”); else if ( score>=60 ) System. out. print(“Grade D”) else System. out. print(“Grade E”);
Ex. switch-case without break value=‘A’; switch (value) { case ‘A’ : printf(“A”); value=‘A’ ABCnot ABC case ‘B’ : printf(“B”); value=‘B’ BCnot ABC case ‘C’ : printf(“C”); value=‘C’ Cnot ABC default : printf(“not ABC”); value=‘P’ not ABC }; 敘述next;
Ex. switch-case with break value=‘A’; switch (value) { case ‘A’ : System. out. print(“A”); value=‘A’ A value=‘B’ value=‘C’ BC C value=‘P’ not ABC break; case ‘B’ : System. out. print(“B”); case ‘C’ : System. out. print(“C”); break; default ABC”); }; 敘述next; : System. out. print(“not
break : 跳出迴圈, 至敘述next(迴圈後的下一敘述) continue: 跳至 條件變換 或 條件判斷 處 break可用於for-loop, while, do-while, switch continue 則用於for-loop, while, do-while for (k=1; k<10; ++ k) { ………. …. . break; ………. …. . continue; ……. . . }; while ( a>=1 ) { ………. …. . break; ………. …. . continue; ……. . . }; next-statement;
// break case: int n, sum=0; // 初值設定 for (n=1; n<=10; ++n) { if ( n = = 5 ) break; sum += n; sum = 1+2+3+4 = 10 } // continue case int n, sum=0; for (n=1; n<=10; ++n) { if ( n = = 5) continue; sum += n; } sum = 1+2+3+4+6+7+8+9+10 = 50
位元的介紹 bit (binary digit):位元 => 0 or 1 byte = 8 bits:位元組 word = 2 bytes = 16 bits:字組 位 元 的 稱 呼 (與冪次有關) 1 0 1 0 1 27 26 25 24 23 22 21 20 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
位元的運算例子 (2/2) 若 A = 0 x. D 3 = (1101 0011)2 (nibble: 4 bits) c) 將 high nibble反相: 1 1 1 0 0 1 1 利用^ (xor)運算 A = A ^ 0 x. F 0; d) 保留 low nibble: 0 1 1 0 0 1 1 利用& (and)運算 A = A & 0 x 0 F; 0 0 1 1
- Slides: 44