Đoạn trích bài giảng với người cố vấn trong khóa học của Đại học Codegym. Đăng ký cho khóa học đầy đủ.


"Xin chào, Amigo. Hôm nay chúng ta sẽ nói về câu lệnh if/else ."

"Các chương trình sẽ ít được sử dụng nếu chúng không phản ứng với các hoàn cảnh bên ngoài đang thay đổi. Một chương trình cần biết cách thích ứng với các hoàn cảnh và thực hiện một hành động trong một trường hợp và các hành động khác trong các trường hợp khác. Trong Java, điều này đạt được bằng cách sử dụng 'câu lệnh if/else' – một cấu trúc đặc biệt cho phép thực hiện các khối mã khác nhau nếu một điều kiện được thỏa mãn."

"Nó bao gồm ba phần: ' điều kiện ', ' lệnh 1 ' và ' lệnh 2 '. Nếu điều kiện đúng thì ' lệnh 1 ' được thực thi, nếu không thì 'lệnh 2' được thực thi. Các lệnh này không bao giờ được thực hiện cả hai. Tuyên bố trông ít nhiều như thế này:"

Mã lệnh if/else
if (condition)
    command_1;
else
    command_2;

"Thật thú vị! Tôi nghĩ câu nói đó sẽ khiến việc lập trình trở nên thú vị hơn nhiều!"

"Yep. Đây là một vài ví dụ cho bạn:"

Mã số Giải trình
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
Nếu a nhỏ hơn b, lệnh đầu tiên sẽ được thực hiện. Nếu không, lệnh thứ hai sẽ được thực thi . Các lệnh không bao giờ được thực hiện cả hai.
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
Bạn có thể thay thế một lệnh bằng một khối mã. Phần còn lại là như nhau.
3
if (a < b)
{
    a = 0;
}
else
{
}
Bạn có thể bỏ qua khối khác nếu nó trống.
Ba ví dụ này là hoàn toàn tương đương.
Bạn có thể bỏ dấu ngoặc nhọn nếu chỉ cần thực hiện một lệnh. Nếu bạn có nhiều lệnh, bạn cần giữ nguyên dấu ngoặc.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"Diego chỉ yêu cầu tôi giao cho bạn một vài nhiệm vụ."

2
Nhiệm vụ
Java Syntax,  mức độbài học
Đã khóa
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
Nhiệm vụ
Java Syntax,  mức độbài học
Đã khóa
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
Nhiệm vụ
Java Syntax,  mức độbài học
Đã khóa
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
Nhiệm vụ
Java Syntax,  mức độbài học
Đã khóa
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
Nhiệm vụ
Java Syntax,  mức độbài học
Đã khóa
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
Nhiệm vụ
Java Syntax,  mức độbài học
Đã khóa
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

Đoạn trích bài giảng với người cố vấn trong khóa học của Đại học Codegym. Đăng ký cho khóa học đầy đủ.