Skip to content

Latest commit

 

History

History
92 lines (79 loc) · 3.01 KB

File metadata and controls

92 lines (79 loc) · 3.01 KB
title Logical OR Operator: || | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-language
ms.tgt_pltfrm
ms.topic language-reference
f1_keywords
||
dev_langs
C++
helpviewer_keywords
OR operator, logical
|| operator
OR operator
logical OR operator
ms.assetid 31837c99-2655-4bf3-8ded-f13b7a9dc533
caps.latest.revision 8
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Logical OR Operator: ||

Syntax

  
logical-or-expression   
||  
 logical-and-expression  
  

Remarks

The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical OR has left-to-right associativity.

The operands to the logical OR operator need not be of the same type, but they must be of integral or pointer type. The operands are commonly relational or equality expressions.

The first operand is completely evaluated and all side effects are completed before continuing evaluation of the logical OR expression.

The second operand is evaluated only if the first operand evaluates to false (0). This eliminates needless evaluation of the second operand when the logical OR expression is true.

printf( "%d" , (x == w || x == y || x == z) );  

In the above example, if x is equal to either w, y, or z, the second argument to the printf function evaluates to true and the value 1 is printed. Otherwise, it evaluates to false and the value 0 is printed. As soon as one of the conditions evaluates to true, evaluation ceases.

Operator Keyword for ||

The or operator is the text equivalent of ||. There are two ways to access the or operator in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.

Example

// expre_Logical_OR_Operator.cpp  
// compile with: /EHsc  
// Demonstrate logical OR  
#include <iostream>  
using namespace std;  
int main() {  
   int a = 5, b = 10, c = 15;  
   cout  << boolalpha  
         << "The true expression "  
         << "a < b || b > c yields "  
         << (a < b || b > c) << endl  
         << "The false expression "  
         << "a > b || b > c yields "  
         << (a > b || b > c) << endl;  
}  

See Also

C++ Built-in Operators Precedence and Associativity C++ Built-in Operators, Precedence and Associativity
C Logical Operators