“ and”是一个内置关键字,至少从C ++ 98起就存在。它是&&(逻辑AND)运算符的替代方法,它通常与条件一起使用。
如果所有操作数的结果为1,and关键字返回1;如果任何条件的结果为0,则返回0。
语法:
operand_1 and operand_2;
这里,operand_1和operand_2是操作数。
示例
Input: a = 10; b = 20; result = (a==10 and b==20); Output: result = 1
范例1:
//C ++示例演示使用
//“和”运算符。
#include <iostream>
using namespace std;
int main(){
int num = 20;
if (num >= 10 and num <= 50)
cout << "true\n";
else
cout << "false\n";
if (num >= 20 and num <= 50)
cout << "true\n";
else
cout << "false\n";
if (num > 50 and num <= 100)
cout << "true\n";
else
cout << "false\n";
return 0;
}输出:
true true false
范例2:
//输入姓名,年龄,身高和体重
//检查您是否符合
//参军与否?
#include <iostream>
using namespace std;
int main(){
string name;
int age;
float height, weight;
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
cout << "Enter height (in cm): ";
cin >> height;
cout << "Enter weight (in kg): ";
cin >> weight;
if (age >= 18 and height >= 165 and weight >= 55)
cout << name << " , you're selected.";
else
cout << name << " , you're not selected.";
return 0;
}输出:
RUN 1: Enter name: Shivang Enter age: 23 Enter height (in cm): 172 Enter weight (in kg): 67 Shivang , you're selected. RUN 2: Enter name: Akash Enter age: 19 Enter height (in cm): 157 Enter weight (in kg): 70 Akash , you're not selected.