Logical operators in c programming with example. In this page you can learn about the logical operators in c for beginners. These operators are used to conduct logical operations on the expressions given to them.
In the C programming language, there are three logical operators. Logic AND (&&), logical OR (||), and logical NOT (!) are the three.
Logical Operators in C Programming With Example
&& (logical AND) – When both requirements are met, it returns true.
|| (logical OR) – When at least one of the conditions is true, it returns true.
! (logical NOT) – It flips the operand “((x>5) && (y5)” on its head.
The logical NOT operator turns true “((x>5) && (y5)” into false.
Logical Operators in C Programming With Example For Beginners
#include <stdio.h>
int main()
{
int m=4,n=2;
int o=2,p=3;
if (m>n && m !=0)
{
printf("&& Operator : Both Conditions Are True\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only One Condition is True\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both Conditions Are True\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
&& Operator : Both Conditions Are True
|| Operator : Only One Condition is True
! Operator : Both conditions are true. But, status is inverted as false
Operators (&&, ||, and!) are used in this program to execute logical operations on the given expressions.
Only when both criteria (m>n and m! =0) are true, the && operator – “if clause” – becomes true. Otherwise, it will be false.
When any of the conditions (o>p || p!=20) is true, the “if clause” becomes true. When none of the conditions are true, it becomes false. The operator is used to reverse the operand’s state.
True (1) is returned if the conditions (m>n && m!=0) are met. The “!” operator inverts this value.
As a result, “! (m>n and m! =0)” yields false (0).
Operators of Various Types
Addition, subtraction, multiplication, division, and modulus are examples of arithmetic operators.
Assignment operators: In C programs, these are used to assign values to variables.
The relational operators are used to compare the values of two variables.
Logical operators are used to conduct logical operations on the two variables provided.
Bitwise operators: These operators are used to conduct bit operations on two variables that are given.
Operators that are conditional (ternary): If the condition is true, the conditional operator returns one value; if the condition is false, it returns another value.
The increment/decrement operators are used to increase or decrease the value of a variable.
These operators are used to make a one-step increase or decrease in the value of a variable.
&, *, sizeof(), and ternary operators are special operators.
C Program to Add Two Matrices Using an Array
C Program to Find Reverse of a String Using Recursion
C Program to Implement Quick Sort Using Recursion
Quick Sort in C Program With Explanation
Quick Sort Program in C Using Recursion
Bubble Sort in Python Using Function
Bubble Sort in Python Using While Loop
Bubble Sort Program in Python Using List
Python Program to Print Inverted Pyramid Pattern