C++ 逻辑运算

2020-03-16  本文已影响0人  小潤澤

逻辑运算符

逻辑非

#include<iostream>
using namespace std;

int main(){
  #逻辑非
  int a = 10;
  #C++中,不是0的都为真
  cout<<!a<<end1;

  system("pause");
  return 0;
}

逻辑非可以理解为取反

逻辑与

#include<iostream>
using namespace std;

int main(){
  #逻辑与
  int a = 10;
  int b = 10;
  #C++中,&&表示逻辑与
  cout<<(a && b)<<end1;

  a = 0;
  b = 10;

  cout<<(a && b)<<end1;

  system("pause");
  return 0;
}

只有两个都为真,结果才为真

逻辑或

#include<iostream>
using namespace std;

int main(){
  #逻辑或
  int a = 10;
  int b = 10;
  #C++中,&&表示逻辑与
  cout<<(a | | b)<<end1;

  a = 0;
  b = 10;
  cout<<(a | | b)<<end1;
  
  system("pause");
  return 0;
}

学习资料:https://www.bilibili.com/video/av41559729?p=1

上一篇 下一篇

猜你喜欢

热点阅读