Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Boolean Algebra; Help!

Name: Anonymous 2008-03-06 16:51

#include <iostream>

using namespace std;

int main()

{
   
    float number;
   number = 9;
    if (number == 2 || 3)
    {cout<<"\n\nRight!";}
    else{cout<<"Wrong";}
    cin.get();
   
}


This script (in my understnding) should display "Wrong", it actually displays "Right"

Help?

Name: Anonymous 2008-03-06 17:33

Since nobody else has actually included what the right code is, here you go...

if (number == 2 || number == 3)

The operators == || && all take two numbers and produce either 0 or 1.

== produces 1 if the two numbers are equal and 0 otherwise.
|| produces 0 if both numbers are 0 and 1 otherwise.
&& produces 1 if both numbers are 1 and 0 otherwise.

Hence, (number == 2 || 3) can be read as (number == 2) || 3, regardless of what the left side of || is, the right side is 3, which is not 0, and so the || returns 1 (true).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List