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 1
In Progress

C++: and Operators (&&)

Yasin Cakal 9 Nov 2021
Lesson Progress
0% Complete

When controlling our programs, there may be situations where an output can be given only if two or more conditions are met. In such conditions, we use logical operators. This allows for greater flexibility when writing programs for the users, too.

The answer for each logical operator, like conditional operators, is true or false. There are mainly three logical operators:

  • and logical operator: &&
  • or logical operator: ||
  • not logical operator: !

Let’s start with the first logical operator: &&. The and logical operator, which is denoted by && is only satisfied or gives true output if both the conditions are met. If even one of the conditions is not met, the answer returns false and the condition is not satisfied.

For example, 3>5 && 5<3 gives a false answer because one of the conditions is not met. Let’s look at an example.

#include <iostream>
using namespace std;

int main ()
{
  int num;
  cout << "Enter a number\n";
  cin >> num;
  if (num > 5 && num < 20)
    {
      cout << "You have been selected.";
    }
  else
    {
      cout << "Better luck next time!";
    }
}

Output:

Enter a number

15
You have been selected.

Responses

Leave a Reply

%d bloggers like this: