当前位置:网站首页>Short circuit and &, short circuit or |, logic and &, logic or |; Conditional operator
Short circuit and &, short circuit or |, logic and &, logic or |; Conditional operator
2022-07-25 22:28:00 【Start】
1. Short circuit and && And logic and &:
&& The law of operation : Only when && The expressions on both sides are true,&& The result of operation is true
public static void main(String[] args) {
// true && true-->true
boolean result1 = true && true;
System.out.println("true&&true:" + result1);// true&&true:true
// true && false-->true
boolean result2 = true && false;
System.out.println("true&&false:" + result2);// true&&false:false
// false && true-->true
boolean result3 = false && true;
System.out.println("false&&true:" + result3);// false&&true:false
// fasle && fasle-->false
boolean result4 = false && false;
System.out.println("false&&false:" + result4);// false&&false:false
}& The law of operation : Only when & The expressions on both sides are true,& The result of operation is true
public static void main(String[] args) {
// true & true-->true
boolean result5 = true & true;
System.out.println("true&true:" + result5);// true&true:true
// true & false-->true
boolean result6 = true & false;
System.out.println("true&false:" + result6);// true&false:false
// false & true-->true
boolean result7 = false & true;
System.out.println("false&true:" + result7);// false&true:false
// fasle & fasle-->true
boolean result8 = false & false;
System.out.println("false&false:" + result8);// false&false:false
}&& and & The difference between :
&& Short circuit function , When && The expression on the left is false When , Directly judge the whole && The result of operation is false,&& The right side is no longer calculated
& No short circuit function , No matter & The result of the expression on the left is true still false,& The expressions on the right have to be evaluated
2. Short circuit or || And logic or | :
|| The law of operation : as long as || One on the left and right is true,|| The result of operation is true
public static void main(String[] args) {
// true || true-->true
boolean result1 = true || true;
System.out.println("true||true:" + result1);// true||true:true
// true || false-->true
boolean result2 = true || false;
System.out.println("true||false:" + result2);// true||false:true
// false || true-->true
boolean result3 = false || true;
System.out.println("false||true:" + result3);// false||true:true
// fasle || fasle-->false
boolean result4 = false || false;
System.out.println("false||false:" + result4);// false||false:false
}
| The law of operation : as long as | One on the left and right is true,| The result of operation is true
public static void main(String[] args) {
// true | true-->true
boolean result5 = true | true;
System.out.println("true|true:" + result5);// true|true:true
// true | false-->true
boolean result6 = true | false;
System.out.println("true|false:" + result6);// true|false:true
// false | true-->true
boolean result7 = false | true;
System.out.println("false|true:" + result7);// false|true:true
// fasle | fasle-->true
boolean result8 = false | false;
System.out.println("false|false:" + result8);// false|false:false
}|| and | The difference between :
|| Short circuit function , When || The expression on the left is true When , Directly judge the whole || The result of operation is true,|| The right side is no longer calculated
| No short circuit function , No matter | The result of the expression on the left is true still false,| The expressions on the right have to be evaluated
3. Conditional operator (conditional operator) Sometimes called ternary operators (ternary operator, perhaps trinary operator), Because it's the only need 3 The operator of two operands :
| Conditions ? expression 1 : expression 2 |
| x < 0 ? y = 10 : z = 20; |
(1) If expression 1 The value of is true ( Not 0 value ), Then the value of the expression is an expression 2 Value ;
(2) If the expression 1 The value of is false (0 value ), Then the value of the expression is an expression 3 Value
Nested conditional operators :
Rule of grammar :
Conditions 1?( Conditions 2? expression 1: expression 2):( Conditions 3? expression 3: expression 4);
The law of execution :
Conditions 1 by true, Continue to judge the conditions 2, Conditions 2 by true, Execute expression 1, Conditions 2 by false, Execute expression 2
Conditions 1 by false, Continue to judge the conditions 3, Conditions 3 by true, Execute expression 3, Conditions 3 by false, Execute expression 4
public static void main(String[] args) {
// obtain 3 The maximum or minimum of the number
int a = 10;
int b = 30;
int c = 20;
int max =a>b?(a>c?a:c):(b>c?b:c);
System.out.println(" Maximum :"+max);
int min = a>b?(b>c?c:b):(a>c?c:a);
System.out.println(" minimum value :"+min);
}
边栏推荐
- IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
- 3dslicer import cone beam CT image
- Get together for ten years, tell your story, millions of gifts are waiting for you
- Title: give a group of arrays, arranged from large to small and from small to large.
- How to resolve a domain name to multiple IP addresses?
- 三菱FX PLC自由口RS指令实现MODBUS通讯
- LabVIEW develops PCI-1680U dual port can card
- Explore the use of self increasing and self decreasing operators
- Square root of X
- 3day
猜你喜欢

完啦,上班三个月,变秃了

Title: give a group of arrays, arranged from large to small and from small to large.

H5 lucky scratch lottery free official account + direct operation

【集训DAY13】Backpack【动态规划】【贪心】

PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示

IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!

【集训DAY13】Internet【并查集】

对需求的内容进行jieba分词并按词频排序输出excel文档

About vscode usage+ Solutions to the problem of tab failure

Xiaobai programmer the next day
随机推荐
IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
vim用法记录
谷歌分析UA怎么转最新版GA4最方便
Builder pattern
【集训DAY12】X equation 【高精度】【数学】
Victoriametrics single node of kubernetes
[C syntax] void*
Based on if nesting and function call
Gan, why '𠮷 𠮷'.Length== 3 ??
Xiaobai programmer's fifth day
【Leetcode】502.IPO(困难)
XSS collect common code
xss-工具-Beef-Xss安装以及使用
Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin
【集训DAY13】Out race【数学】【动态规划】
(1) DDL, DML, DQL, DCL and common data types
Common source code for ArcGIS development
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
Smart S7-200 PLC channel free mapping function block (do_map)