Tải bản đầy đủ (.ppt) (61 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (204.72 KB, 61 trang )


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

Bài tập (làm tại lớp)
Viết chương trình tạo 1 số ngẫu nhiên từ 1 đến 10,
sau đó cho phèp NSD đoán. In kết quả cuối cùng
(đoán đúng hay sai)
Gợi ý :
Tạo 1 số ngẫu nhiên từ 0 đến cận 1
Random <tên biến>= new Random();
double num = <tenbiến>.NextDouble();

Hướng dẫn :
using System;
class Guess
{
const int MAX=10;
static public void Main()
{
Random r = new Random();
double d = r.NextDouble()*MAX;


Ví dụ
using System;
class TestStr
{
const string CPlusPlus = “C++”;
const string VisualBasic = “Visual Basic”;
const string Java = “Java”
public static void Main()
{

Console.WriteLine(“What is your prefer language (exclude
C#) : “);
string inputStr = Console.ReadLine();
if ( 0 = = String.Compare(inputStr,CPlusPlus,true))
Console.WriteLine(“No problem with C#”);
if ( 0 = = String.Compare(inputStr,Java,true))
Console.WriteLine(“Not easier than C#”);
………………………………

Tìm hiểu
Phương thức tĩnh String.Compare
Có 6 cách sử dụng (overload)

String.Compare trả về -1 nếu chuỗi đầu tiên nhỏ hơn chuỗi
thứ hai.Trả về 1 nếu chuỗi đầu tiên lớn hơn chuỗi thứ hai và
trả về 0 nếu 2 chuỗi bằng nhau

Phương thức CompareTo của 1 biến kiểu string


Một số lưu ý quan trọng
C#:Giá trị trong biểu thức lệnh if
phải là 1 giá trị kiểu Boolean
C/C++:cho phép dùng if để kiểm
tra 1 biểu thức có giá trị khác 0


int i= 1;
if (i) Console.Writeln(“i khac khong”);
C# : phép toán = trong lệnh if
C/C++ : phép toán = trong lệnh if


Xem đoạn lệnh sau
int i=3 , j=4;
i++;
if (i=j) Console.WriteLine(“i bang
j”);
Khảo sát trong C/C++ và C#

If/Else If Constructs
using System;
namespace Example3
{
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 if (guess > number)
{
Console.WriteLine("Lower, try again");
}
else //guess must be 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 Example Evaluation
== equal 5 == 4 FALSE
5 == 5 TRUE
!= not equal 5 != 4 TRUE
5 != 5 FALSE
> greater than 5 > 4 TRUE
5 > 5 FALSE
>= greater than or equal 5 >= 4 TRUE
5 >= 5 TRUE
< less than 5 < 4 FALSE
5 < 5 FALSE
<= less than or equal 5 <= 4 FALSE
5 <= 5 TRUE

Hướng dẫn bài tập
using System;
namespace Example4
{
class BirthdayGame
{
static void Main()
{
int myBirthday = 13;
string userGuess;
int guess;

Console.WriteLine("Please guess the day of
my birth, from 1 to 31");
Console.Write("Please enter your guess: ");

userGuess = Console.ReadLine();
guess = Convert.ToInt32(userGuess);
if (guess < 0)
{
Console.WriteLine("Months don't have
negative days, Einstein");
}
else if (guess > 31)
{
Console.WriteLine("Pretty long month,
genius");
}
else if (guess == myBirthday)
{
Console.WriteLine("Incredible, you are
correct");
}

×