General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Operators

Logical

Logical Operators in Computer Programming

Logical operators are used to perform logical operations on boolean values (true or false). They are fundamental in controlling the flow of execution in programs, especially in conditional statements and loops. The most common logical operators are AND, OR, and NOT.

Types of Logical Operators:

  1. AND (&& or and): Returns true if both operands are true. Otherwise, it returns false.

    • Example: true && true is true, but true && false is false.

  2. OR (|| or or): Returns true if at least one of the operands is true. If both operands are false, it returns false.

    • Example: true || false is true, and false || false is false.

  3. NOT (! or not): Inverts the value of the operand. If the operand is true, it returns false, and vice versa.

    • Example: !true is false, and !false is true.

Examples in Programming Context:

Consider the following pseudocode examples demonstrating the use of logical operators:

# Example variables
a = true
b = false

# Logical AND
result1 = a && b  # result1 will be false, as both operands are not true

# Logical OR
result2 = a || b  # result2 will be true, as at least one operand is true

# Logical NOT
result3 = !a  # result3 will be false, as it inverts the value of a

Usage:

Logical operators are particularly useful in conditions and control statements:

  • Conditional Statements: In if statements, logical operators can combine multiple conditions. For example, if (age > 18 && hasLicense) { drive(); }.

  • Loops: While or for loops can use logical operators to determine the continuation condition. For instance, while (isRunning && !isError) { process(); }.

Points to Note:

  • Short-Circuit Evaluation: In many languages, logical AND and OR operators perform short-circuit evaluation. For &&, if the first operand is false, the second operand is not evaluated. For ||, if the first operand is true, the second operand is not evaluated.

  • Boolean Context: Some languages, like Python, evaluate non-boolean types in a boolean context. For instance, 0, None, "" (empty string) are considered false.

  • Precedence: Logical operators have specific precedence rules. Typically, NOT has a higher precedence than AND, which has a higher precedence than OR.

Logical operators are essential in creating complex conditional expressions and are integral to the logic of most programs. They enable a program to make decisions and perform actions based on various conditions.

PreviousArithmeticNextAssignment

Last updated 1 year ago

Was this helpful?