Java Beginner: Conditional judgment and its statements

Source: Internet
Author: User

Today we are talking about conditional judgments, which are the most basic techniques used in programming. So what is conditional judgment? Translated into vernacular is "if ...." So.... "Means. For example, if it rains today, I will stay at home; If it snows, I'll go out to snowball fights. Wait, wait. In Java, we say something like this: if ... To represent such a logic. Let's introduce the conditional expressions in Java and then introduce some common conditional judgment statements.

First, the conditional expression, the most commonly used is the >=, <, <=, = =, the equals function of the string class, the other kinds of libraries return the value of the Boolean value of the built-in function, the user-defined return value is a Boolean value function. We are not here to discuss the following situations for the time being, let's just say a few things earlier:

1. >=, < <=, there's nothing to be said about it, it's not equal in math, 7>=5, returns a Boolean value of True, 9>10, which returns a Boolean value of false.

2. = =, this is equal to the meaning, the reason is to come out alone, because the double equals sign is conditional judgment, and as a human, we often write a single equal sign, for example, 8==8, will return a true Boolean value, but 8=8? This is going to be a mistake, because you're actually trying to assign the integer data 8 on the right to the left 8, and the 8 on the left is not a variable name. If you do this: int n; n = 8; This will be an assignment operation, just a behavior and will not return any values.

3. The Equals function of the string class, in Java, it is best not to use = = to compare whether two strings are the same. Because if you use = = to compare two strings, you are actually comparing the memory reference addresses of two strings, which is generally different, but the Java compiler does not error because Java thinks you are comparing the memory reference addresses of two strings. If you want to compare the contents of two strings, you need to use the Equals function. Let's make a simple example, as follows:

From the above can be seen, = = for S1 and S3 comparison did not take effect, or said, gave us a value of false (because the operation is not executed), and = = for S1 and S2 comparison, unexpectedly played a role, you may ask, you just said do not use = =? Yes, but I said, if you use = = to compare two strings, you are actually comparing the memory reference addresses of two strings. Let's take a look at our program, String s1 = "Canada"; String s2 = "Canada"; String s3 = new String ("Canada"); The way S1 and S2 are defined, they point to the same memory address, and the way S3 is defined is actually a new memory address for S3. So when using = = Compare S1 and S3, because the comparison is memory address, so it is not the same, return false,if inside the operation is not executed. When using = = to compare S1 and S2, the two points to the same memory address, so true is returned and the if operation is performed.

String1.equals (string2) is the comparison between the string string1 and the string2 content in parentheses is exactly the same, if the same returns true, otherwise false is returned.

String1.equalsignorecase (string2) is the comparison between the string string1 and the string2 content in parentheses, ignoring the case. Returns true if it is the same without regard to case, otherwise false.

Alternatively, you can use the CompareTo and Comparetoignorecase methods of string, which are not commonly used and are described in detail in a later string operation.

Next, let's talk about the format of conditional judgment statements, which generally have the following common formats:

1. if (condition) {action}, this is the most basic conditional judgment statement, indicating that when a condition is true, an action is performed.

2. if (condition) {The action performed by 1}else{executes 2}, which means that when a condition is true, perform action 1, otherwise, perform operation 2.

3. if (condition 1) {Execution of the manipulation 1}else if (condition 2) {performed Operation 2}else if (condition 3) {operation performed 3}...else if (condition N) {action performed n}else{performed}, which means that when condition I is established, the operation I is executed, If neither is true, the last action is performed.

The above is a common conditional judgment statement, below we look at a simple example, in this example, we let the user input two floating point data, and then calculate the first floating point data in the LN value, if it is greater than the second data, output a message, if less than the second data, output additional information, if equal, And then output other information. The code is as follows:

Well, today we have a brief talk about the condition of Java, conditional judgment in all programming is very important, almost all programs, from dozens of lines of simple programs to tens of thousands of large projects, will be frequently used to judge the conditions, so please read a lot of practice.

Java Beginner: Conditional judgment and its statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.