Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 5, Topic 3
In Progress

C++: not Operators (!)

Yasin Cakal 9 Nov 2021
Lesson Progress
0% Complete

We have learned to use the and and or operators. What if we need to execute a problem if the condition it not satisfied at all?

We have not operators for this purpose. They are denoted by !.

The bool outcome of the condition is reversed and the outcome is given accordingly. If the condition is satisfied, the bool outcome will be false.

Things to Remember: We can use not in place of !.

Let’s look at an example of code using not operators:

#include <iostream>
using namespace std;

int main ()
{
  bool heads = false;
  if (!heads)
    {
      cout << "The coin flipped to give tails.";
    }
  else
    {
      cout << "The coin flipped to give heads.";
    }
}

Output:

The coin flipped to give tails.

As we can see here, the heads was assigned false value. The output was adjusted so that it told the The coin flipped to give tails. in case the answer was not heads.

Responses

Leave a Reply

%d bloggers like this: