งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

1 Special Operators Special Operators ตัวดำเนินการพิเศษ Increment and decrement operators Conditional Operator.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "1 Special Operators Special Operators ตัวดำเนินการพิเศษ Increment and decrement operators Conditional Operator."— ใบสำเนางานนำเสนอ:

1 1 Special Operators Special Operators ตัวดำเนินการพิเศษ Increment and decrement operators Conditional Operator

2 2 Conditional operators รูปแบบ expr1 ? expr2 : expr3; ความหมาย ตัวอย่าง scanf(“%[01]”, &i) ? printf(“ Valid input ”) : printf(“ Invalid input ”) ; ค่าของนิพจน์ expr1 ไม่ใช่ศูนย์ จริง expr2 เท็จ expr3

3 3 ตัวอย่าง

4 4 ตัวอย่าง ( ต่อ ) #include void main( ) { int a, b, c, max; printf(“Enter integer \na = ”); scanf(“%d”, &a); printf(“b = ”); scanf(“%d”, &b); printf(“c = ”); scanf(“%d”, &c); max = (a>b) ? a : b; (max < c) ? max=c : ; printf(“The maximum is %d”, max); }

5 5 ตัวอย่าง ( ต่อ ) max = (a>b) ? (a> c ? a : c) : (b> c ? b : c); a > b จริง เท็จ max = cmax = a a > cb > c max = bmax = c จริง เท็จ

6 6 Increment & decrement operators ใช้ได้กับตัวแปรที่มีชนิดเป็น Integral types เท่านั้น การเปลี่ยนแปลงค่าของตัวแปรนั้นทีละ 1 หน่วย ++ (Increment) เพิ่มค่าของตัวแปรขึ้น 1 หน่วย n = n + 1; n += 1; n++; ++n; -- (Decrement) ลดค่าของตัวแปรลง 1 หน่วย n = n - 1; n -= 1; n--; --n; Postfix operator ตัวดำเนินการที่วางไว้หลังตัวถูกดำเนินการ Prefix operator ตัวดำเนินการที่วางไว้หน้าตัวถูกดำเนินการ

7 7 ตัวอย่าง /* Demonstrate prefix and postfix increment operator */ #include void main() { int x, post = 1, pre = 1; x = post++; printf(“x = %d\tpost = %d\n”, x, post); x = ++pre; printf (“x = %d\tpre = %d\n”, x, pre); } ผลลัพธ์จากการ run โปรแกรมนี้คือ x = 1post = 2 x = 2pre = 2

8 8 ลำดับความสำคัญของตัวดำเนินการ

9 9 การประกาศตัวแปรและกำหนดค่าเริ่มต้น Arithmetic operators int a=1, b=2,c=3, d = 4; นิพจน์ นิพจน์เทียบเท่า ค่าของนิพจน์ ++a * b – c-- 7 - -b * d++ ((++a) * b) – (c--) 1 7 – ((-b) * (d++)) 15

10 10 ตัวอย่าง : int digits, number; : for(digits = 0; number > 0; digits += 1) number /= 10; for (digits=0; number>0; number /= 10, digits++); for (digits=0; number>0; digits++) number /= 10; for (digits=0; number>0; number /= 10, ++digits);

11 11 จงหาผลลัพธ์ของชุดคำสั่งต่อไปนี้ int i, n; for (i=8, n=50; n != 1; ++i) { n = (n % 2 == 0) ? n / 2 : 3 * n + 1; if (i % 6 == 0) printf(“\n”); printf(“%d ”, n); } printf(“No. of hailstones generated : %d\n”, i);


ดาวน์โหลด ppt 1 Special Operators Special Operators ตัวดำเนินการพิเศษ Increment and decrement operators Conditional Operator.

งานนำเสนอที่คล้ายกัน


Ads by Google