Computer Science
Logical Operators in C
Logical operators in C are used to combine multiple conditions in a single expression. The three logical operators are AND (&&), OR (||), and NOT (!). They are used to evaluate the truth value of expressions and control the flow of a program.
Written by Perlego with AI-assistance
Related key terms
1 of 5
6 Key excerpts on "Logical Operators in C"
- eBook - PDF
- Xingni Zhou, Qiguang Miao, Lei Feng(Authors)
- 2020(Publication Date)
- De Gruyter(Publisher)
Logical operation • A logical operation connects one or more conditions using logical operators and determine whether these conditions are met. • A logical expression evaluates to a Boolean value (true of false). Logical expression • A logical expression is a statement that connects one or more expressions using logical operators and executes logical operations. We use logical expressions to express combination of multiple conditions in C. Figure 3.62: Logical operations and logical expressions. 3.7 Logical operations 121 (1) AND AND yields true if two operands are both true, and false otherwise. Suppose the operands are a and b, which are results of relational operations and are either true (1) or false (0). If a and b are both true (or 1), a AND b yields true; if one of them is false, the result is false. AND is similar to multiplication in their way of working. (2) OR OR yields false if two operands are both false, and true otherwise. OR is similar to addition in their way of working. (3) NOT NOT yields false if the operand is true, and true if the operand is false. We can remember rules of logical operators easily using this tip: AND yields false if one operand is false, OR yields true if one operand is true, NOT flips its operand. 3.7.2.3 Examples of Logical Operations Example 3.14 Classify a triangle There is a triangle whose edges have lengths a, b, and c, respectively. Use logical and condi-tional expressions in C to describe conditions for different types of triangles. [Analysis] The conditions and corresponding C expressions are shown in Figure 3.64. In the first row, an equilateral triangle requires a == b and a == c, which can be represented by a == b && a == c. - eBook - ePub
C Programming
Learn to Code
- Sisir Kumar Jena(Author)
- 2021(Publication Date)
- Chapman and Hall/CRC(Publisher)
Table 6.4 .- The logical operator && and || act upon two operands, but the logical NOT operator acts upon one operand;
- The results of these operators are either true or false.
The truth table of the AND, OR, and NOT operators is shown in Table 6.5 .- In logical AND, when both the inputs are 1, the output is 1, and 0 otherwise;
- In logical OR, when both the inputs are 0, the output is 0, and 1 otherwise;
- For the NOT operator, when the input is 1, the output is 0, and vice versa.
Let us take one programming example and analyze how the compiler executes the code. Figure 6.5 shows an example program to understand the working of the && operator.Figure 6.5 Program showing the operation of Logical AND.Table 6.4 Logical OperatorsSl. No. Operators Meaning 1 && Logic AND 2 || Logic OR 3 ! Logic NOT Table 6.5 Truth Table of the (a) AND, (b) OR, and (c) NOT Operators(a) A B A && B 0 0 0 0 1 0 1 0 0 1 1 1 (b) A B A || B 0 0 0 0 1 1 1 0 1 1 1 1 (c) A !A 0 1 1 0 The output of the above program will be 0 (zero). Because, a < b it will yield 1 and b > c will yield 0. Now a && b will be 0. Finally, 0 is assigned to d. Hence, we get the output d = 0. See Figure 6.6 for easy understanding. The numbers 1 through 4 represent the sequence of execution.Figure 6.6 Execution steps of example program.6.6 Increment and Decrement Operators
The C language provides two special operators ++ and – –, called increment and decrement operators. These are unary operators because they operate only on one operand. The operand must be a variable and not a constant.Example:- x++ is valid but 5++ is not valid;
- – – y is valid but – –7 is not valid.
The ++ operator will increment the value of a variable by 1, and the – – operator will decrement the value of a variable by 1. Let us take a simple example to understand how it works (see Figure 6.7 - eBook - PDF
- Joyce Farrell(Author)
- 2012(Publication Date)
- Cengage Learning EMEA(Publisher)
All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. The expressions hoursWorked > HOURS_IN_WK? and dentalPlan = Y? in Figures 3-3 and 3-4 are Boolean expressions. A Boolean expression is one that represents only one of two states, true or false. Every decision you make in a computer program involves evaluating a Boolean expression. True/false evaluation is “ natural ” from a computer ’ s standpoint, because computer circuitry consists of two-state on-off switches, often represented by 1 or 0. Every computer decision yields a true-or-false, yes-or-no, 1-or-0 result. Mathematician George Boole (1815 – 1864) approached logic more simply than his predecessors did, by expressing logical selections with common algebraic symbols. Boolean (true/false) expressions are named for him. Using the Relational Comparison Operators Table 3-1 describes the six relational comparison operators supported by all modern programming languages. These operators are used to create Boolean expressions. Each of these operators is binary — that is, each requires two operands. An operand is a value on either side of an operator. Usually, both operands in a comparison must be the same data type; that is, you can compare numeric values to other numeric values, and text strings to other strings. Operator Name Discussion = Equivalency operator Evaluates as true when its operands are equivalent. Many languages use a double equal sign ( == ) to avoid confusion with the assignment operator. > Greater than operator Evaluates as true when the left operand is greater than the right operand. - eBook - PDF
Elementary Synchronous Programming
in C++ and Java via algorithms
- Ali S. Janfada(Author)
- 2019(Publication Date)
- De Gruyter Oldenbourg(Publisher)
Table 2.8 specifies these operators in C++. Tab. 2.8: Relational (comparative) operators. Operator: < <= > >= == != Math meanin g : < ≤ > ≥ = ≠ The result of the effect of these operators on their operands will be a logical value (1 for true and 0 for false). If the operands are characters, the comparison will be made based on the order of the character s’ locations in the ASCII code table. 3) Logical operators. These operators in C++ appear as shown in Table 2.9. Tab. 2.9: Logical operators. Operator Notation in logic Meanin g ! ~ negation && ^ and || v or 3 – The ! operator has only one operand. !p is true whenever p is false and vice versa. – p&&q is true if and only if both p and q are true. – p||q is false if and only if both p and q are false. 26 | Fundamental concepts of programming in C++ 4) Assignment operators. Table 2.10 lists the assignment operators supported by C++. With the exception of the first one, all operators are compound. Tab. 2.10: Assignment operators. operator name example equivalent = assignment x = y x = y += addition assignment x += y x = x+y -= subtraction assignment x -= y x = x-y *= multiplication assignment x *= y x = x*y /= division assignment x /= y x = x/y %= remainder assignment x %= y x = x%y The expression x = y makes the compiler assign (substitute) the value of y to x . This assignment operator is also called the assignment statement since it behaves like a statement (see the next section). 3 – Do not confuse the == relational operator with the = assignment operator. Using the == operator instead of the = statement often causes an error. However, using the = operator instead of the == operator does not trigger an error. Nonetheless, it could engender delicate logical errors which could eventually lead to serious problems. – There must not be any space between the two characters in the compound assignment operators; for example, + ˽ = is illegal. - eBook - PDF
MATLAB
An Introduction with Applications
- Amos Gilat(Author)
- 2016(Publication Date)
- Wiley(Publisher)
The program makes these decisions by comparing values of variables. This is done by using relational and logical operators, which are explained in Section 6.1. It should also be noted that user-defined functions (introduced in Chapter 7) can be used in programming. A user-defined function can be used as a sub- program. When the main program reaches the command line that has the user- defined function, it provides input to the function and “waits” for the results. 176 Chapter 6: Programming in MATLAB The user-defined function carries out the calculations and transfers the results back to the main program, which then continues to the next command. 6.1 RELATIONAL AND LOGICAL OPERATORS A relational operator compares two numbers by determining whether a compar- ison statement (e.g., 5 < 8) is true or false. If the statement is true, it is assigned a value of 1. If the statement is false, it is assigned a value of 0. A logical operator examines true/false statements and produces a result that is true (1) or false (0) according to the specific operator. For example, the logical AND operator gives 1 only if both statements are true. Relational and logical operators can be used in mathematical expressions and, as will be shown in this chapter, are frequently used in combination with other commands to make decisions that control the flow of a computer program. Relational operators: Relational operators in MATLAB are: Note that the “equal to” relational operator consists of two = signs (with no space between them), since one = sign is the assignment operator. In other rela- tional operators that consist of two characters, there also is no space between the characters (<=, >=, ~=). • Relational operators are used as arithmetic operators within a mathematical expression. The result can be used in other mathematical operations, in address- ing arrays, and together with other MATLAB commands (e.g., if) to control the flow of a program. - eBook - PDF
- Subrata Saha, Subhodip Mukherjee(Authors)
- 2017(Publication Date)
- Cambridge University Press(Publisher)
When parenthetical expressions are nested, execution will start from innermost parenthetical expression and continued from inner to outer. Note 2: Postfix increment and decrement operators have higher precedence, but the actual increment or decrement of the operand is delayed until the other related operations are not completed. So in the statement a = b/c++; the current value of c is used to evaluate the expression and after completion of all operations in the statement, c will be incremented. Operators and Expressions 87 S UMMARY Operators are the symbols that help the programmer to instruct the computer to perform certain arithmetic, relational or logical operations. Arithmetic operators are +, −, *, / and %. Assignment Operator is =. Shorthand assignment operators are +=, −=, *=, /= and %=. ++ and −− are increment and decrement operators, respectively. Relational operators are >, ≥, <, ≤, == and !=. Logical operators are &&, || and !. Conditional operator is ?:. Bitwise operators are &, |, ^, ~, » and «. An expression is a combination of variables, constants, operators, and functions that evaluates to a certain value. When a conversion occurs automatically by the compiler, it is called implicit type conversion. When a particular data type is converted to another data type forcefully, the process is known as explicit conversion or type casting. Precedence determines the priority or order in which the operations are performed. Associativity is the right-to-left or left-to-right order of execution for a group of operands to operators that have the same precedence. R EVIEW E XERCISES 1. What do you mean by operator? 2. What are the different types of operators available in C? 3. What are the unary operators available in C? 4. Is there any ternary operator available in C? 5. What do you mean by pre-increment and post-increment? 6. What is the use of sizeof( ) operator? 7. What type of value is returned on execution of relational operator? 8.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.





