jeudi 26 mai 2016

If statement condition operators [duplicate]

This question already has an answer here:

I was trying to look up on to some basics i stumbled on this problem where I was required to give a condition and print a statement based on whether the number was odd or even.

Here's the first code : http://ift.tt/1TEBVNT

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

Answer to the first one turns out to be :

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

Here the second variant : http://ift.tt/20HKvKB

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

Answer to the Second one turns out to be :

x lies between 6 and 20

Up until now i was assuming both would return the same result, I would like to know the reason for both not having same functionality.

Aucun commentaire:

Enregistrer un commentaire