Skip to content Skip to navigation

Connexions

You are here: Home » Content » Logical Operators

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual Connexions member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • OrangeGrove display tagshide tags

    This module is included inLens: Florida Orange Grove Textbooks
    By: Florida Orange GroveAs a part of collection:"Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "OrangeGrove" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Houston Community College

    This module is included in aLens by: Houston Community CollegeAs a part of collection:"Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "Houston Community College" link to see all content affiliated with them.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection:"Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Programming Fundamentals - A Modular Structured Approach Using C++ is a new course written by Kenneth Leroy Busbee, a faculty member at Houston Community College. This text introduces students to […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • SHN CNX Workshop display tagshide tags

    This module is included inLens: Stategic Horizon Network Workshop on Alternative Couseware -- Connexions Session
    By: ConnexionsAs a part of collection:"Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Author, Ken Busbee, is a faculty member at Houston Community College and completed this textbook on programming after hearing about Connexions. His textbook is available in the Florida Orange […]"

    Click the "SHN CNX Workshop" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

  • CSE & eScience Content

    This module is included inLens: Computational Science and Engineering and eScience Content
    By: Jan E. OdegardAs a part of collection:"Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "CSE & eScience Content" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.

Logical Operators

Module by: Kenneth Leroy Busbee. E-mail the author

User rating (How does the rating system work?)
Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

:
(0 ratings)

Summary: An introduction to the three common logical operators used in programming.

Overview of the Logical Operators

Within most languages, expressions that yield Boolean data type values are divided into two groups. One group uses the relational operators within their expressions and the other group uses logical operators within their expressions.

The logical operators are often used to help create a test expression that controls program flow. This type of expression is also known as a Boolean expression because they create a Boolean answer or value when evaluated. The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false. There are three common logical operators that give a Boolean value by manipulating other Boolean operand(s). Operator symbols and/or names vary with different programming languages. The C++ programming language operators with their meanings are:

Table 1
C++ Operator Meaning Comment Typing
&& Logical and   two ampersands
|| Logical or   two vertical dashes or piping symbols
! Logical not unary the exclamation point

In most languages there are strict rules for forming proper logical expressions.  An example is:

6 > 4 && 2 <= 14

This expression has two relational operators and one logical operator.  Using the precedence of operator rules the two relational comparisons will be done before the logical and operation. Thus:

1 && 1

or

true && true

The final evaluation of the expression is:  1  meaning true.

We can say this in English as: It is true that six is greater than four and that two is less than or equal to fourteen.

When forming logical expressions programmers often use parentheses (even when not technically needed) to make the logic of the expression very clear.  Consider the above complex Boolean expression rewritten:

(6 > 4) && (2 <= 14)

Truth Tables

A common way to show logical relationships is in truth tables.

Table 2: Logical and (&&)
x y x && y
false false false
false true false
true false false
true true true
Table 3: Logical or (||)
x y x ||y
false false false
false true true
true false true
true true true
Table 4: Logical not (!)
x !x
false true
true false

Examples

I call this example of why I hate "and" and love "or".

Everyday as I came home from school on Monday through Thursday; I would ask my mother, "May I go outside and play?" She would answer, "If your room is clean and your homework is done then you may go outside and play." I learned to hate the word "and". I could manage to get one of the tasks done and have some time to play before dinner, but both of them… well, I hated "and".

On Friday my mother took a more relaxed view point and when asked if I could go outside and play she responded, "If your room is clean or your homework is done then you may go outside and play." I learned to clean my room quickly on Friday afternoon. Well needless to say, I loved "or".

For the next example, just imagine a teenager talking to their mother. During the conversation mom says, "After all, your Dad is reasonable!" The teenager says, "Reasonable. (short pause) Not."

Maybe college professors will think that all their students studied for the exam. Ha ha! Not. Well, I hope you get the point.

Exercise 1

Evaluate the following Logical Boolean expressions:

  1. 25 < 7 || 15 > 36
  2. 15 > 36 || 3 < 7
  3. 14 > 7 && 5 <= 5
  4. 4 > 3 && 17 <= 7
  5. ! false
  6. ! (13 != 7)
  7. 9 != 7 && ! 0
  8. 5 > && 7

Solution

Answers:
  1. 0
  2. 1
  3. 1
  4. 0
  5. 1
  6. 0
  7. 1
  8. Error, there needs to be an operand between the operators > and &&.

Demonstration Program in C++

Creating a Folder or Sub-Folder for Source Code Files

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the Demo Program

Download and store the following file(s) to your storage device in the appropriate folder(s). You may need to right click on the link and select "Save Target As" in order to download the file. Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials.

Download from Connexions: Demo_Logical_Operators.cpp

Definitions

Definition 1: logical operator
An operator used to create complex Boolean expressions.
Definition 2: truth tables
A common way to show logical relationships.

Content actions

Give Feedback:

E-mail the module author | Rate module ( How does the rating system work?)

Rating system

Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

(0 ratings)

Download:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.

| A lens (?)

Definition of a lens

Lenses

A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual Connexions member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks