c++ cdi+示例_C ++“和”关键字示例

c++ cdi+示例

"and" is an inbuilt keyword that has been around since at least C++98. It is an alternative to && (Logical AND) operator and it mostly uses with the conditions.

“ and”是一个内置关键字,至少从C ++ 98起就存在。 它是&& ( 逻辑AND )运算符的替代方法,它通常与条件一起使用。

The and keyword returns 1 if the result of all operands is 1, and it returns 0 if the result of any condition is 0.

如果所有操作数的结果均为1,则and关键字将返回1,如果任何条件的结果为0,则它​​将返回0。

Syntax:

句法:

    operand_1 and operand_2;

Here, operand_1 and operand_2 are the operands.

在这里,操作数_1和操作数_2是操作数。

Example:

例:

    Input:
    a = 10;
    b = 20;
    
    result = (a==10 and b==20);

    Output:
    result = 1

演示使用“ and”关键字的C ++示例 (C++ example to demonstrate the use of "and" keyword)

Example 1:

范例1:

// C++ example to demonstrate the use of 
// 'and' operator.

#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;
}

Output:

输出:

true
true
false

Example 2:

范例2:

// Input name, age, height and weight
// check you are eligible for the 
// army selection or not?

#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;
}

Output:

输出:

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.


翻译自: https://www.includehelp.com/cpp-tutorial/and-keyword-with-example.aspx

c++ cdi+示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值