Computer Science

OR Operator in C

The OR operator in C is a logical operator that returns true if either one or both of its operands are true. It is represented by the symbol "||" and is commonly used in conditional statements to check multiple conditions at once.

Written by Perlego with AI-assistance

6 Key excerpts on "OR Operator in C"

  • Book cover image for: C Programming
    eBook - ePub

    C Programming

    Learn to Code

    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 Operators
    Sl. 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:
    1. x++ is valid but 5++ is not valid;
    2. – – 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
  • Book cover image for: Just Enough Programming Logic and Design
    x y x OR y True True True True False True False True True False False False Table 3-3 Truth table for the OR operator 85 Understanding OR Logic Copyright 2012 Cengage Learning. 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. Logically, the OR operation corresponds to arithmetic addition. If you assume that 1 is true and 0 is false, then false OR false is false because 0 + 0 is 0. All the other combinations (1 + 1, 1 + 0, and 0 + 1) are true because their results are not 0. When you use the OR operator, you still must realize that the question you place first is the question that will be asked first, and cases that pass the test of the first question will not proceed to the second question. As with the AND operator, this feature is called short-circuiting. The computer can ask only one question at a time; even when you write code as shown at the bottom of Figure 3-13, the computer will execute the logic shown at the top. if itemsSold > ITEMS_MIN OR valueSold >= VALUE_MIN then bonusGiven = BONUS endif No Yes itemsSold > ITEMS_MIN OR valueSold >= VALUE_MIN? No Yes itemsSold > ITEMS_MIN? valueSold >= VALUE_MIN? No bonusGiven = BONUS bonusGiven = BONUS Yes if itemsSold > ITEMS_MIN then bonusGiven = BONUS else if valueSold >= VALUE_MIN then bonusGiven = BONUS endif endif bonusGiven = BONUS Figure 3-13 Using an OR operator and the logic behind it 86 C H A P T E R 3 Making Decisions Copyright 2012 Cengage Learning. 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).
  • Book cover image for: Programming Logic and Design, Introductory
    For example, you can make the following evaluation: callsMade > CALLS OR textsSent > TEXTS When you use the logical OR operator, only one of the listed conditions must be met for the resulting action to take place. Quick Reference 4-4 contains the truth table for the OR operator. As you can see in the table, the entire expression x OR y is false only when x and y each are false individually. QUICK REFERENCE 4-4 Truth Table for the OR Operator x? y? x OR y? True True False False True False True False True True True False If the programming language you use supports an OR operator, you still must realize that the comparison you place first is the expression that will be evaluated first, and cases that pass the test of the first comparison will not proceed to the second comparison. As with the AND operator, this feature is called short-circuiting. The computer can make only one decision at a time; even when you write code as shown at the top of Figure 4-14, the computer will execute the logic shown at the bottom. C#, C11, C, and Java use two pipe symbols ( || ) as the logical OR operator. In Visual Basic, the keyword used for the operator is Or. This book uses OR. Avoiding Common Errors in an OR Selection Make Sure that Boolean Expressions Are Complete As with the AND operator, most programming languages require a complete Boolean expression on each side of the OR operator. For example, if you wanted to display a message when customers make either 0 calls or more than 2000 calls, the expression callsMade 5 0 OR callsMade > 2000 is 147 Understanding OR Logic Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. appropriate but callsMade 5 0 OR > 2000 is not, because the expression to the right of the OR operator is not a complete Boolean expression.
  • Book cover image for: Programming with C++
    • Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
    • 2021(Publication Date)
    Module 6 decision control structures 101 The OR Operator (6.4.3, 6.4.4) The OR operator requires that only one of the conditions is true. Suppose an online store offers free shipping to customers who purchase more than $100.00 of merchandise or are VIP club members. As you can see from the table and the code snippet in Figure 6-18, only one of the conditions has to be true for free shipping. Figure 6-18 Truth table for the OR operator #include using namespace std; { float purchase = 80.00; bool vip = true; if (purchase > 100.00 || vip) cout <<"Free shipping!"; } OUTPUT: Free shipping! The || (OR) Operator int main() purchase > 100.00 vip purchase > 100.00 || vip true true true true false true false true true false false false Figure 6-17 Truth table for the AND operator The && (AND) Operator choice > 3 choice != 9 choice > 3 && choice != 9 true true true true false false false true false false false false if (choice > 3 && choice != 9) { cout << "Invalid input." << endl; } You might find it necessary to use more than one logical operator in a conditional statement. When convert- ing a pseudocode statement, such as if ((vip == False AND purchase < 100) || free_shipping_ coupon == True)) , to code, use parentheses to ensure that the logic is carried out in the correct sequence. Copyright 2022 Cengage Learning. 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.
  • Book cover image for: Programming Microcontrollers in C
    • Ted VanSickle(Author)
    • 2001(Publication Date)
    • Newnes
      (Publisher)
    26 Chapter 1 Introduction to C is not positive, the square root of a negative number is imaginary and the equation has complex roots. Handle both real and complex roots in your program. Relational or Logical Operators The relational operators are all binary operators. When contained in an expression, the program will evaluate the left operand and then the right operand. These operands will be compared, and if the com-parison shows that the meaning of the operator is correct, the program will return 1. Otherwise, the program will return a 0. In the vocabu-lary of C, FALSE is always zero. If calculated by a logical expression, TRUE will always be one. However, if the argument of a conditional expression is anything but zero, it will respond as if the argument is TRUE. In other words, FALSE is always zero and TRUE is anything else. The relational operators are: < (less than) <= (less than or equal to) > (greater than) >= (greater than or equal to) These operators all have the same precedence, which is slightly higher than the following equality operators: == (is equal to) != (is not equal to) The logical operators are && and || . The first operator indicates a logical AND and the second a logical OR . A logical AND will return TRUE if both of its operands are TRUE , and a logical OR will return TRUE if either of its operands is TRUE . The logical OR has lower precedence than the logical AND . The precedence of the logical AND is lower than the precedence of the relational operators and the equal-ity operators. In the evaluation of long logical expressions, the program starts on the left side of the expression and evaluates the expression until it knows whether the whole expression is true or false, and it then exits the evaluation and returns a proper value. For example, suppose there 27 is a character c , and it is necessary to determine if this character is a letter.
  • Book cover image for: An Object-Oriented Approach to Programming Logic and Design
    This was the original intention. You can use the following techniques to avoid confusion when mixing AND and OR operators: l You can use parentheses to override the default order of operations, as in the movie discount example. l You can use parentheses for clarity even though they do not change what the order of operations would be without them. For example, if a customer should be between 12 and 19 or have a school ID to receive a high school discount, you can use the expression (age > 12 AND age < 19) OR validId = Yes , even though the evaluation would be the same without the parentheses. l You can use nesting if statements instead of combining AND and OR operators in a single expression. With the flowchart and pseudocode shown in Figure 3-23, it is clear which movie patrons receive the discount. In the flowchart, you can see that the OR is nested entirely within the Yes branch of the rating = G selection. Similarly, in the pseudocode in Figure 3-23, you can see by the alignment that if the rating is not G, the logic proceeds directly to the last endif statement, bypassing any checking of age at all. 102 C H A P T E R 3 Making Decisions Copyright 2012 Cengage Learning. 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. Chapter Summary l Every decision in a computer program involves evaluating a Boolean expression. You can use if-then-else structures when action is required whether the selection is true or false, and if-then structures when there is only one outcome for the question for which action is required.
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.