Bài 4: Các cấu trúc điều khiển

61 662 0
Bài 4: Các cấu trúc điều khiển

Đ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

LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG Bài 4 Các cấu trúc điều khiển Nội dung Cấu trúc chọn lựa Cấu trúc lặp Câu lệnh lựa chọn Có 2 nhóm lệnh - Lệnh if : dùng để thực hiện code dựa trên 1 điều kiện Boolean - Lệnh switch : dùng để thực hiện code dựa trên 1 giá trị Câu lệnh if Cú pháp if (expression) statement1; [else statement2;] Câu lệnh if if (expression) { statement1; statement2; } using System; namespace Example1 { class NumberGame { static void Main() { int number = 5; string userGuess; int guess; VÍ DỤ CÂU LỆNH if Console.WriteLine("I am thinking of a number between 1 and 10"); Console.Write("Please enter your guess: "); userGuess = Console.ReadLine(); guess = Convert.ToInt32(userGuess); if (number == guess) Console.WriteLine("Incredible, you are correct"); } } } Kết quả chương trình Output: I am thinking of a number between 1 and 10 Please enter your guess: 5 Incredible, you are correct Câu lệnh if ….else……. using System; namespace Example2 { class NumberGame { static void Main() { int number = 5; string userGuess; int guess; Console.WriteLine("I am thinking of a number between 1 and 10"); Console.Write("Please enter your guess: "); userGuess = Console.ReadLine(); guess = Convert.ToInt32(userGuess); if (number == guess) { Console.WriteLine("Incredible, you are correct"); } else { Console.WriteLine("Sorry Chump"); } } } } Kết Quả Output : I am thinking of a number between 1 and 10 Please enter your guess: 3 Sorry Chump Output : I am thinking of a number between 1 and 10 Please enter your guess: 5 Incredible, you are correct [...]... too low { Console.WriteLine("Higher, try again"); } } } } Bài tập Viết chương trình cho phép NSD đoán ngày sinh của bạn Sử dụng cấu trúc chọn lựa nhằm thực hiện các yêu cầu sau - Nếu dữ liệu nằm ngoài phạm vi từ 1 đến 31 thì thông báo lỗi - Nếu NSD đoán sai thì thông báo : “Lớn hơn” hay “Nhỏ hơn” - Nếu đoán đúng thì in câu thông báo chúc mừng Các phép toán quan hệ Operator Description == equal 5 ==... ("D1"); break; case "zxy":Console.WriteLine ("D2"); break; } Kết Quả :????? Một số lưu ý cấu trúc switch • Chỉ cho phép xuất hiện biểu thức hằng trong khi liệt kê các trường hợp • Biểu thức trong 1 nhãn (trường hợp) có thể thuộc kiểu nguyên, ký tự, liệt kê hay chuỗi (string) • Nhãn default là tuỳ ý • Kết thúc các lệnh trong 1 trường hợp không rỗng phải là phat1 biểu break, return hay goto, throw (khác... you are correct"); } else if (guess < myBirthday) { Console.WriteLine("Higher, try again"); } else // Guess is in range and not greater than or equal { Console.WriteLine("Lower, try again"); } } } } Cấu trúc switch switch (expression) { case constant-expression1: statement; [break;] …………… case constant-expressionN: statement; [default : statement;] } using System; namespace Example1 { class IceCreamShop... > greater than 5>4 5 > 5 FALSE >= greater than or equal 5 >= 5 TRUE < less than 5

Ngày đăng: 09/05/2014, 14:34

Từ khóa liên quan

Mục lục

  • LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG

  • Câu lệnh lựa chọn

  • Câu lệnh if

  • Slide 4

  • PowerPoint Presentation

  • Slide 6

  • Kết quả chương trình

  • Câu lệnh if ….else…….

  • Slide 9

  • Kết Quả

  • Bài tập (làm tại lớp)

  • Hướng dẫn :

  • Ví dụ

  • Slide 14

  • Tìm hiểu

  • Slide 16

  • Slide 17

  • Một số lưu ý quan trọng

  • Slide 19

  • Slide 20

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

  • Đang cập nhật ...

Tài liệu liên quan