如果语句条件运算符

时间:2016-05-26 20:36:28

标签: c++ if-statement conditional-operator

我试图查看一些基础知识,我偶然发现了这个问题,我需要根据这个数字是奇数还是偶数打印一个条件并打印一个声明。

这是第一个代码:https://ideone.com/vS8wgs

#include <iostream>
using namespace std;

int main() {



  int x = 18;


   if(x%2 == 0 && 2<=x<=5){

    cout << "x lies between 2 and 5" << endl;

   }
   if(x%2 == 0 && 6<=x<=20){

    cout << "x lies between 6 and 20" << endl;

   }

    return 0;
}

对第一个问题的答案结果是:

x lies between 2 and 5
x lies between 6 and 20

这是第二个变体:https://ideone.com/rQ7KoW

#include <iostream>
using namespace std;

int main() {


int x =18;

 if(x%2 == 0 && x >= 2 && x <=5){

    cout << "x lies between 2 and 5" << endl;

   }

   if(x%2 == 0 && x >= 6 && x<=20){

    cout << "x lies between 6 and 20" << endl;

   }

    return 0;
}

对第二个问题的答案结果是:

x lies between 6 and 20

到目前为止,我假设两者都会返回相同的结果,我想知道两者没有相同功能的原因。

0 个答案:

没有答案