Control Procedure Flow

Source: Internet
Author: User

1. Use the java Operator
+,-, *, \, = Almost all operators can only operate on the primary type. The only exception is "=" "="! = "Can operate on the main type, string supports" + "" + ="
2. Assignment
The assignment of the primary data type is very direct. Because the primary data type contains the actual value rather than the actual value handle, when the object is assigned a value, assign a value to another place. Assume that the primary type is a = B. If a is modified, the value of B is not affected;
If an object is assigned a value, the operation between objects is actually a handle operation. The object is assigned a value, and a handle is assigned to another place. C = D. If c changes, d will also change;
3. Relational operators
Relational operators generate a Boolean result. They evaluate the relationship between calculation object values. If the link is true, the relational expression generates true (true). If the link is not true, false (false) is generated ). Relational operators include less than (<), greater than (>), less than or equal to (<=), greater than or equal to (> =), equal to (=), and not equal (! = ). Equals and not equals apply to all built-in data types, but other comparisons do not apply to boolean types.
4. logical operators
Logical operators AND (&), OR (|), and not (!) Generate a Boolean value (true or false) based on the Logical Relationship of the independent variable. The following example shows how to use relational and logical operators.
1. Short Circuit
When operating logical operators, we will encounter a situation called "Short Circuit. This means that the logic of the expression is evaluated only when the true or false conclusion of the entire expression is obtained. Therefore, all parts of a logical expression may not be evaluated:

 
//: ShortCircuit. java
// Demonstrates short-circuiting behavior
// With logical operators.

Public class ShortCircuit {
Static boolean test1 (int val ){
System. out. println ("test1 (" + val + ")");
System. out. println ("result:" + (val <1 ));
Return val <1;
}
Static boolean test2 (int val ){
System. out. println ("test2 (" + val + ")");
System. out. println ("result:" + (val <2 ));
Return val <2;
}
Static boolean test3 (int val ){
System. out. println ("test3 (" + val + ")");
System. out. println ("result:" + (val <3 ));
Return val <3;
}
Public static void main (String [] args ){
If (test1 (0) & test2 (2) & test3 (2 ))
System. out. println ("expression is true ");
Else
System. out. println ("expression is false ");
}
}///:~

Each test compares the independent variables and returns true or false values. It does not display information related to what is to be called. The test is performed in the following expression:
If (test1 (0) & test2 (2) & test3 (2 ))
Naturally, you may think that all three tests will be executed. But I hope the output results won't surprise you:

 
If (test1 (0) & test2 (2) & test3 (2 ))

The first test generates a true result, so the expression evaluation continues. However, the second test produces a false result.

 

This article is from "Zhao Zhijun's blog"

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.