Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

AND vs. OR

Fahad Farid

In programming languages, you can add multiple checks to the if parameter.This can be used to carry out specific tasks if particular conditions have been met.

However, if you use AND, all of the conditions will have to be met for the condition to be true.

These checks can also be used to meet particular conditions, such as with OR, where only one of the statements needs to be true.

C++

In C++, && is used to denote AND, and || is used to denote OR.

Let’s write a C++ code that prints “All is Good” if the fruit is an apple and if the vegetable is spinach. If only one of the conditions match, it will print, “Something is fishy.”

#include <iostream>
#include <string>
using namespace std;

int main() {
  string fruit = "apple";
  string vegetable = "spinach";

  if(fruit == "apple" && vegetable=="spinach")
  cout<<"All is Good"<<endl;
  else if(fruit == "apple" || vegetable=="spinach")
  cout<<"Something is fishy"<<endl;
}

Python

In Python, and is used to denote AND, while or is used to denote OR.

Let’s write a Python code that prints “All is Good” if the fruit is an apple and if the vegetable is spinach. If only one of the conditions match, it will print, “Something is fishy.”

fruit = "apple"
vegetable = "spinach"

if(fruit == "apple" and vegetable=="spinach"):
  print("All is Good")
elif(fruit == "apple" or vegetable=="spinach"):
  print("Something is fishy")

RELATED TAGS

c++

CONTRIBUTOR

Fahad Farid
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring