Home > Java > javaTutorial > body text

How to use java conditional operators

WBOY
Release: 2023-05-11 10:16:05
forward
1782 people have browsed it

Concept

1. The conditional operator is also called the ternary operator. This operator has three operands and needs to determine the value of a Boolean expression.

2. This operator mainly determines which value should be given to the variable.

Grammar form

布尔表达式 ? 表达式1 :表达式2
Copy after login

Operation process

If the value of the Boolean expression is true, the expression is returned The value of expression 1, otherwise the value of expression 2 is returned.

public static void main(String[] args) {
    int a, b;
    a = 10;
    // 如果 a 等于 1 成立,则设置 b 为 20,否则为 30
    b = (a == 1) ? 20 : 30;
    System.out.println("Value of b is : " + b);
 
    // 如果 a 等于 10 成立,则设置 b 为 20,否则为 30
    b = (a == 10) ? 20 : 30;
    System.out.println("Value of b is : " + b);
}
Copy after login

The above is the detailed content of How to use java conditional operators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template