Logical AND Operator in C
Logical operators perform logical operations on a given expression by joining two or more expressions or conditions. It can be used in various relational and conditional expressions. This operator is based on Boolean values to logically check the condition, and if the conditions are true, it returns 1. Otherwise, it returns 0 (False). In C programming, logical operators are classified into three types such as the logical AND (&&) operator, the logical OR operator (||), and the logical NOT (!) operator. Here, we learn about the Logical AND operator and its various examples in the C programming language.
Logical AND Operator
The logical AND operator is represented as the '&&' double ampersand symbol. It checks the condition of two or more operands by combining in an expression, and if all the conditions are true, the logical AND operator returns the Boolean value true or 1. Else it returns false or 0.
Note: If the value of both is non-zero, the condition will remain true. Otherwise, the logical AND (&&) operator returns 0 (false).
There are two conditions in the above syntax, condition1 and condition2, and in between the double (&&) ampersand symbol. If both the conditions are true, the logical AND operator returns Boolean value 1 or true. Otherwise, it returns false.
Truth table of the Logical AND (&&) operator
Example 1: Program to demonstrate the Logical AND Operator in C
1 1 0 1
Example 2: Program to find the largest number using the Logical AND operator
Enter the first number: 20 Enter the second number: 10 Enter the third number: 50 50 is the largest number of all
Example 3: Program to use the Logical AND (&&) operator to check whether the user is teenager or not.
Enter the age: 17 17 is a teenager age. 2nd execution: Enter the age: 10 10 is not a teenager age.
Example 4: Program to validate whether the entered number is in the defined range or not.
Enter a number between 1 to 50: 19 The entered number is in the range 0 and 50. 2nd Run Enter a number between 1 to 50: 51 The entered number is in the range 50 and 100. 3rd execution: Enter a number between 1 to 50: 0 Please enter the number is in the defined range.
Example 5: Program to validate the username and password entered by the user is correct or not using the predefined username and password.
Enter the username: system Enter the password: [email protected] The user's credentials are correct. 2nd execution Enter the username: system Enter the password: [email protected] The user's credentials are incorrect.
Next TopicShift Operators in C