Boolean operators.

Posts 1–15 of 29 · Page 1 of 2
Boolean operators.
So yeah, I haven't really posted in a while, due to me not really doing anything in C++ due to school. But picking up now once more. Anyways, I've just been starting, so yeah. I'm doing lessons from pretty much everywhere now, I learn something new from every other book, whether it be the first lesson.

So yeah, I was reading the 2nd Lesson on Cprogramming.com and I got really confused at one part. ._.

A. !( 1 || 0 ) ANSWER: 0
B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

AND OR NOT. The three operators of Boolean. Can someone explain to me what they mean? Like I know that !=not ||=or &&=and.

But I mean, how they are used, and if possible, can you give me an example in a code, cause it's much easier for me to understand. Also, since I don't want to repost another thread. The break statement is used to break loops, correct? And the continue statement skips to the next part of the code? ._.;

Sorry if it sounds confusing, but yeah. I'm still not finished Basics due to school, but I have 2 weeks of in which I intend to cover most of the basics.

Thanks to those help, and thank you for your time.

EDIT: An example I tried was
Code:
int main()
{
   int a=10;
   if (a != 20){
            cout <<"Hi. I'm failing basics.";
         }
   return 0;
}
I understand that example, because it basically means if a does not equal 20, print the following. But I mean, the other ones are confusing to me, mostly or as well as and.
If you mess around with d3d alot ull find that boolean operators become easier to understand.
Let's say I want to make chams and the stride is 32
if(stride == 32){chams;}
so this if statement only requires stride to be equal to 32 for it to activate

however, if I have this:
if(stride == 32 && startIndex == 0){chams;]
it means that my chams will only work IF I have stride as 32 AND startindex as 0 AT THE SAME TIME

what about:
if(stride == 32 || stride == 40){chams;}
this means that my chams will work if either my stride is 32 or my stride is 40 so if either condtion is present then chams will activate
Boolean operators are actually very useful. I can give you a quick example here.

Code:
int x = 10;
(x == 10 || x == 20)
I'm sure you can guess what that does. This evaluates to true if x is either equal to 10 or 20. I guess you can use "or" operator if you have 2 possible answers of some sort...

Now for the "and" operator.

Code:
int x = 5;
int y = 10;

(x == 5 && y == 10)
Now this evaluates to true because, well it's obvious, x does equal 5 and y equals 10.

Code:
(x == 6 && y == 10)
This evaluates to false. Even though y equals 10, x doesn't equal 5. Adding the "Not" operator to this will evaluate to true.

Code:
!true = false
!false = true
That's pretty understandable right?

Err.. if anyone sees something wrong just tell me, i'm like half asleep right now.
Quote Originally Posted by Void View Post
Boolean operators are actually very useful. I can give you a quick example here.

Code:
int x = 10;
(x == 10 || x == 20)
I'm sure you can guess what that does. This evaluates to true if x is either equal to 10 or 20. I guess you can use "or" operator if you have 2 possible answers of some sort...

Now for the "and" operator.

Code:
int x = 5;
int y = 10;

(x == 5 && y == 10)
Now this evaluates to true because, well it's obvious, x does equal 5 and y equals 10.

Code:
(x == 6 && y == 10)
This evaluates to false. Even though y equals 10, x doesn't equal 5. Adding the "Not" operator to this will evaluate to true.

Code:
!true = false
!false = true
That's pretty understandable right?

Err.. if anyone sees something wrong just tell me, i'm like half asleep right now.
Okay, I understood everything you said. Last part, though. !true=false. Meaning not true is false, right?
And Not false is true?

I got confused @ that bit. ._.

Quote Originally Posted by falzarex View Post
If you mess around with d3d alot ull find that boolean operators become easier to understand.
Let's say I want to make chams and the stride is 32
if(stride == 32){chams;}
so this if statement only requires stride to be equal to 32 for it to activate

however, if I have this:
if(stride == 32 && startIndex == 0){chams;]
it means that my chams will only work IF I have stride as 32 AND startindex as 0 AT THE SAME TIME

what about:
if(stride == 32 || stride == 40){chams;}
this means that my chams will work if either my stride is 32 or my stride is 40 so if either condtion is present then chams will activate
Thanks for that example, it actually did help.
Exactly..
Also, you were right about break, it "breaks out" of a loop.

Code:
int x;
while(1)
{
      cin >> x;
      if(x == 2)
      {
              break;
      }
}
Here, we have an infinite loop and it will keep asking the user for input. Everytime the user does not input the number 2, it will just loop over and over again. Once the user inputs 2, it will "break out" of the infinite loop and continue with whatever follows.

As for continue, i'm not sure but I believe it does the opposite.

Code:
int x;
int count = 0;
while(1)
{
     cin >> x;
     if(x == 2)
     {
           continue;
     }
     count++;
}
Another infinite loop here. This time everytime the user doesn't input 2 it will increment "count" by one. When the user does input 2. It will skip immediately perform another loop, skipping everything. "count" won't be incremented, it will ask for user input again without incrementing.

Not too sure about continue here
Quote Originally Posted by Void View Post
Exactly..
Also, you were right about break, it "breaks out" of a loop.

Code:
int x;
while(1)
{
      cin >> x;
      if(x == 2)
      {
              break;
      }
}
Here, we have an infinite loop and it will keep asking the user for input. Everytime the user does not input the number 2, it will just loop over and over again. Once the user inputs 2, it will "break out" of the infinite loop and continue with whatever follows.

As for continue, i'm not sure but I believe it does the opposite.

Code:
int x;
int count = 0;
while(1)
{
     cin >> x;
     if(x == 2)
     {
           continue;
     }
     count++;
}
Another infinite loop here. This time everytime the user doesn't input 2 it will increment "count" by one. When the user does input 2. It will skip immediately perform another loop, skipping everything. "count" won't be incremented, it will ask for user input again without incrementing.

Not too sure about continue here
Got it, I understand break now, just needa look more up on continue. Maybe get a bit more into boolean operators as well. Thanks.
I don't think I ever needed to use continue before.. .__.
Quote Originally Posted by Void View Post
I don't think I ever needed to use continue before.. .__.
Code:
int GetBestTarget()
{
    for(int i=0;i<playercount;i++)
    {
        if(!IsVisible(players[i]))
        {
            continue;
        }
        if(GetDist2D(players[i],myplayer)<bestdist)
        {
            besttarget=players[i];
            bestdist = GetDist2D(players[i],myplayer);
        }
    }
    return besttarget;
}
does the same as

Code:
int GetBestTarget()
{
    for(int i=0;i<playercount;i++)
    {
        if(IsVisible(players[i])) //notice I removed the not operator(the !)
        {
            if(GetDist2D(players[i],myplayer)<bestdist)
            {
                besttarget=players[i];
                bestdist = GetDist2D(players[i],myplayer);
            }
        }
    }
    return besttarget;
}
edit: I prefer the first one, since it shows what is happening more clearly(if he is not visible, continue(skip), if he is visible go through the other codens
Code:
if (x>10 && y<1)>>it means if both x and y statement can be true at the same time then return true.
return true;
Code:
if (x>100||x=10) >> this means if either one of them is true return 1.
return 1;
non of the above examples uses a boolean...
they are all examples of if and while statements

a boolean is a datatype that can only have the value true or false

true ==1
false ==0

example copied from a site:
Code:
 
#include <iostream> 
using namespace std;  
 
int main() { 
  bool b; 
 
  b = false; 
  cout <<  "b is " << b << endl; 
 
  b = true; 
  cout <<  "b is " << b << endl; 
 
  
  if(b)                               // control the if statement 
     cout <<  "This is executed.\n"; 
 
  b = false; 
  if(b) 
     cout <<  "This is not executed.\n"; 
 
  // outcome of a relational operator is a true/false value 
  cout <<  "10 > 9 is " << (10 > 9) << endl; 
 
  return 0; 
}
A. !( 1 || 0 ) ANSWER: 0
B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
example A.

you start with the or.
1 or 0 = 1 inverted 0

example B.

1 or 1&&0
1= 1
1&&0 = 0

so
1 or 1&&0
is 1 0r 0 = 1 inverted =0

example C.

( 1 || 0 ) && 0
always first between the brackets so...
1 or 0 = 1
1&& 0 = 0 inverted is 1
Quote Originally Posted by lalakijilp View Post
non of the above examples uses a boolean...
they are all examples of if and while statements

a boolean is a datatype that can only have the value true or false

true ==1
false ==0

example copied from a site:
Code:
 
#include <iostream> 
using namespace std;  
 
int main() { 
  bool b; 
 
  b = false; 
  cout <<  "b is " << b << endl; 
 
  b = true; 
  cout <<  "b is " << b << endl; 
 
  
  if(b)                               // control the if statement 
     cout <<  "This is executed.\n"; 
 
  b = false; 
  if(b) 
     cout <<  "This is not executed.\n"; 
 
  // outcome of a relational operator is a true/false value 
  cout <<  "10 > 9 is " << (10 > 9) << endl; 
 
  return 0; 
}


example A.

you start with the or.
1 or 0 = 1 inverted 0

example B.

1 or 1&&0
1= 1
1&&0 = 0

so
1 or 1&&0
is 1 0r 0 = 1 inverted =0

example C.

( 1 || 0 ) && 0
always first between the brackets so...
1 or 0 = 1
1&& 0 = 0 inverted is 1

You're not wrong, boolean does return true or false, true being 1, false being anything but 1. AJ is asking about the boolean OPERATORS I believe. The operators being !, && and ||. != Not, && = And, || = Or.

Now you go through an analyze how each works.

I'll start with the operator which is evaluated first.

&& is the first boolean operator to be evaluated if there is more than one operator present. It compares two values, if either value is false, then the whole && comparison is false, i.e. 1 && 0 would return false because one of the values is false. 1&&1 would return true because both values are true.

|| is evaluated second after &&, || compares 2 values just like &&. If either value compared is true, then the whole || comparison is true, i.e. 1 || 0 evaluates to true because one of the statements is true. 0 || 0 returns false, because neither value is true.

! is to be evaluated lastly and is most likely outside of the parenthesis containing the other boolean operators. Whatever the value is found within the other comparisons, would be reversed. IE. !(1||0), 1||0 will return true, because one of the values is true. But the ! operator outside of the statement reverses that making the statement false. So !(1||0) will return false.

Parenthesis are useful aswell, because the operators must be evaluated in a certain order, you can modify that order through parenthesis. IE

!(1||0) &&0). Normally you would evaluate the && operator first, but because 1||0 are inside parenthesis before that, you will evalue that first. So 1||0 evaluates to True, because one of the values is true. Then you evaluate that value with the && operator so the problem would look like this at this point.
!(1&&0). Now we will evaluat 1&&0 since you evaluate && before !. 1&&0 evaluates to false, because one of the values is false. So we would now have this.
!(0) And now lastly we evaluate the ! operator. So of course, if the answer is NOT 0, then it is of course 1, or true.

=D Hope I helped ya.

BTW fags, don't call me a C+P'er, google it. =) Hand typed.
Code:
int main(void){

int myDick = 69;
int inYourPussy = 69;

float myHand = 0.0;
float onYourTits = 0.0;

bool imHorny = false;

if(myDick==inYourPussy||myHand==onYourTits||imHorny){
      cout<<"Your a fag"<<endl;
}
In the expression if(myDick==inYourPussy||myHand==onYourTits||imHorn y)

myDick is equal to inYourPussy so it is true so that expression will now be evaluated as

if(1||myHand==onYourTits||imHorny)

myHand is also equal to onYourTits so the expression will now be evaluated as

if(1||1||imHorny)

and imHorny is false so the expression is

if(1 || 1 || 0 )

since || means OR it means if any one of them is true than the statement will be true

and it will output "Your a fag"
Nice analogies ;P
Thanks a ton guys! LOL. Nice examples
And COOKIES, HOW DARE YOU TELL THEM MY NAME?
Nah, I'm JP. But thanks alot man, I understood exactly what you said. :P
Errr actually cookies is wrong, everything other than 1 is true. bool n = (36) in this expression n is true

!(1||0) &&0) <--- your missing a bracket....but i suppose it would be here
!((1||0) &&0)
Since the INSIDE brackets are first we evaluate (1||0) which is true
!( 1 && 0 )
both have to be true so this express is now
! (0)
the ! operator flips the bit so it would be
1
Posts 1–15 of 29 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us