这些运算符用于执行“逻辑与”和“逻辑或”运算,即与数字电子中的与门和或门类似的功能。有一点需要注意的是,如果第一个条件是错误的,那么第二个条件就不会被评估,即它具有短路效应。广泛用于测试几种作出决定的条件。
有条件的运营商是 -
// Java program to illustrate // logical operators public class operators { public static void main(String[] args) { String x = "Sher"; String y = "Locked"; Scanner s = new Scanner(System.in); System.out.print("Enter username:"); String uuid = s.next(); System.out.print("Enter password:"); String upwd = s.next(); // Check if user-name and password match or not. if ((uuid.equals(x) && upwd.equals(y)) || (uuid.equals(y) && upwd.equals(x))) { System.out.println("Welcome user."); } else { System.out.println("Wrong uid or password"); } } }
输出:
Enter username:Sher Enter password:Locked Welcome user.