Session 07 ngôn ngữ lập trình c

19 129 0
Session 07 ngôn ngữ lập trình c

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Điều kiện Chương Mục tiêu học  Tìm hiểu cấu trúc lựa chọn • • • •  Lệnh if Lệnh if – else Lệnh nhiều if Lệnh if lồng Lệnh switch Elementary Programming with C/Session Câu lệnh điều kiện    Các câu lệnh điều kiện cho phép thay đổi hướng thực chương trình Một câu lệnh điều kiện trả giá trị sai Ví dụ: Để xác định số số chẵn hay số lẻ tiến hành sau : 1) 2) 3) 4) Nhập vào số Chia số cho để xác định số dư Nếu số dư 0, số “SỐ CHẴN” Ngược lại số dư khơng 0, số “SỐ LẺ” Elementary Programming with C/Session Các cấu trúc lựa chọn C cung cấp hai dạng câu lệnh lựa chọn Elementary Programming with C/Session Lệnh if  Cú pháp: if (expression) statement;  Nếu biểu thức lệnh if có giá trị (true), khối lệnh theo sau lệnh if thực thi Elementary Programming with C/Session Lệnh if (tt.) Chương trình hiển thị giá trị dựa vào điều kiện #include void main(){ int x, y; char a = ‘y’; x = y = 0; if (a == ‘y’){ x += 5; printf(“The numbers are %d and \t%d”,x,y); } } Elementary Programming with C/Session Lệnh if – else if (expression) statement; else statement; Elementary Programming with C/Session Lệnh if – else (tt.)    Nếu biểu thức if trả giá trị true, khối lệnh theo sau lệnh if thực thi Nếu biểu thức if không trả giá trị true lệnh theo sau else thực thi Lệnh else tùy chọn Lệnh else dùng để thực thi lệnh biểu thức if trả giá trị false Elementary Programming with C/Session Lệnh if – else (tt.) Chương trình xác định số số chẵn hay lẻ #include void main(){ int num ,res ; printf(“Enter a number :”); scanf(“%d”,&num); res = num % 2; if (res == 0) printf(“Then number is Even”); else printf(“The number is Odd”); } Elementary Programming with C/Session Lệnh if–else–if Cú pháp: if (expression) statement; else if (expression) statement; else if (expression) statement; … else statement; Elementary Programming with C/Session Lệnh if–else–if (tt.)  Lệnh if - else - if gọi lệnh if-else-if bậc thang  Các biểu thức xác định giá trị theo hướng từ xuống Elementary Programming with C/Session Lệnh if–else–if (tt.) Chương trình hiển thị thơng báo dựa vào giá trị : #include #include main() { int x; x = 0; clrscr (); printf(“Enter Choice (1 - 3) : “); scanf(“%d”, &x); if (x == 1) printf (“\nChoice is 1”); else if ( x == 2) printf (“\nChoice is 2”); else if ( x == 3) printf (“\nChoice is 3”); else printf (“\nInvalid Choice “); } Elementary Programming with C/Session Lệnh if lồng  Lệnh if lồng lệnh if đặt lệnh if else khác  Trong C, lệnh else kết hợp với lệnh if gần khối lệnh lệnh else chưa kết hợp với lệnh if khác Elementary Programming with C/Session Lệnh if lồng (tt.)  Cú pháp:  Lệnh else bên kết hợp với if(exp3) Theo chuẩn ANSI, trình biên dịch hỗ trợ 15 mức lồng  Elementary Programming with C/Session Lệnh if lồng (tt.) #include #include void main (){ int x, y; x = y = 0; clrscr (); printf (“Enter Choice (1 - 3) : “); scanf (“%d”, &x); if (x == 1){ printf(“\nEnter value for y (1 - 5) : “); scanf (“%d”, &y); if (y ‘z’) printf(“\nCharacter not a lower cased alphabet”); else switch (ch) { case ‘a’ : case ‘e’ : case ‘i’ : case ‘o’ : case ‘u’ : printf(“\nCharacter is a vowel”); break; case ‘z’ : printf (“\nLast Alphabet (z) was entered”); break; default : printf(“\nCharacter is a consonant”); break; } } Elementary Programming with C/Session ... “SỐ CHẴN” Ngư c lại số dư khơng 0, số “SỐ LẺ” Elementary Programming with C /Session C c cấu tr c lựa chọn C cung c p hai dạng c u lệnh lựa chọn Elementary Programming with C /Session Lệnh if  C ... if (ch < ‘a’ || ch > ‘z’) printf(“ Character not a lower cased alphabet”); else switch (ch) { case ‘a’ : case ‘e’ : case ‘i’ : case ‘o’ : case ‘u’ : printf(“ Character is a vowel”); break; case... #include #include main (){ char ch; clrscr (); printf (“ Enter a lower cased (a - z) : “); scanf(“ %c , &ch); tiếp … alphabet Elementary Programming with C /Session Lệnh switch

Ngày đăng: 26/03/2019, 11:02

Mục lục

  • Điều kiện

  • Mục tiêu bài học

  • Câu lệnh điều kiện

  • Các cấu trúc lựa chọn

  • Lệnh if

  • Lệnh if (tt.)

  • Lệnh if – else

  • Lệnh if – else (tt.)

  • Slide 9

  • Lệnh if–else–if

  • Lệnh if–else–if (tt.)

  • Slide 12

  • Lệnh if lồng nhau

  • Lệnh if lồng nhau (tt.)

  • Slide 15

  • Lệnh switch

  • Lệnh switch (tt.)

  • Slide 18

  • Slide 19

Tài liệu cùng người dùng

Tài liệu liên quan