Help with Logical Operators?

I've been studying them for about a few hours now and I still can't get the hang of them. I've been using this book to guide me but I can't seem to get the hang of it.

In simple terms, can anyone help me understand what they do and how they work?

I just don't understand the !, || and the &&. Help

thanks.
Last edited on
|| is logical OR
&& is logical AND
| binary OR

For example

( You do not know what is || ) && ( You do not know what is && )

( Either I will get the answer at once ) || ( Or I will get the answer tomorrow )


0 | 0 == 0
1 | 0 == 1
0 | 1 == 1
1 | 1 == 1
What I don't get is what Logicals are used for? Why would I need them in my code?

( You do not know what is || ) && ( You do not know what is && )

( Either I will get the answer at once ) || ( Or I will get the answer tomorrow )


I'm confused in how you explained it here? How come I don't know what is && ?

And then the binary OR wasn't even in my book so I have NO clue what that is lol.

What I don't get is what Logicals are used for? Why would I need them in my code?

Any situation in which your code would need to different things based on different conditions.


Wouldn't if's and whiles be used for this though?
if and while shall have condition expressions. Compound conditions are formed by means of logical operators.
Wouldn't if's and whiles be used for this though?

Yes. "if" and "while" statements are exactly where you use conditional operators like || and && .
Last edited on
A simple example:

1
2
3
if(age >= 18 && gender == boy){
    cout << "You're a man!";
}
Last edited on
A simple example:

1
2
3
if(age >= 18 && gender == boy){
cout << "You're a man!";
}


This was very helpful! So if I would say

1
2
3
4
5
6
if (fly == yes || noise == chirp)
{
cout << "you are a bird!"

}
Last edited on
Exactly :) If the first option is fulfilled but not the second, you're not a bird, but if both are, you indeed are a bird :)

1
2
3
if(gender == man || gender == girl{
    cout << "You're a human!";
}

Meaning, that if you're a man or a woman, you're a human :) We could maybe include the option gender==both hehe
Last edited on
in the program when you're writing, you have to make choices that do not know, you can only predict responses to be included in a program and implement its education program. choice, for example, can be done through the "if" statement.

the logical operators are used, for example, when to implement a procedure in the program, there are more cases to predict.
For example if we use education

cin >> x;

we can not know a priori the choice of the user of the program, then insert an "if" statement:

in the example that follows, we will assume that the variable "x", to be useful to the program must have values ​​between 0 and 10, then there are two conditions to be met (x> = 0) and (x <= 10), these two conditions must be met simultaneously in the program, then you should write
1
2
3
4
if (x> = 0)
      {
         if (x <= 10)
            {Statement 1 ....


all this is shortened by using the && (and and) and as the name suggests works if the first statement is true "and" at the same time the second statement:
1
2
if ((x> = 0) && (x <= 10))
    {Statement 1 ...


we take
x = 5;
when that occurs (x> = 0), the assignment is to have a bool value "true" is equivalent to "1", while if the same statement is not verified, it takes a Boolean value of "false" which is equivalent to the value "0 ".
the same goes for the second statement (x <= 10).

then the computer when you run the program, the comparison is "logic" of the values ​​taken by the two instructions. the possibilities are:

instruction1 statement2 comparison result &&
 true (1) ----- true (1) --------- true (1)
false (0) ----- true (1) ----------false  (0)
false (0) ------- false (0) ------- false (0)
true (1) -------- false (0) ------- false (0) 


therefore the conditional statement
1
2
3
if ((x> = 0) && (x <= 10))
    {
       instruction1 ....


will be executed if and only if the two conditions are true then (1) && (1) = (1)

etc etc. ..

Last edited on
You got the basic idea in your response nsahawks7.

It makes it very useful to apply to while-loops and form if-else blocks...especially when you don't know what a certain variable (such as an int or bool) might be when your program runs. It's sometimes essential in making an effective decision statement when there are multiple possibilities.

Another example for the fun of it:
1
2
3
4
5
6
7
8
9
10
11
12
// assuming a bool bDailyLimitReached, which represents whether 
// a daily limit on your atm card has been reached

if (balance == 0 || bLimitReached)
{
	cout << "Transaction FAILED." << endl;
}
else
{
	cout << "Transaction SUCCESS." << endl;
	ATM_transaction();
}

In this case, if either the balance in the account is zero OR the daily limit has been reached (and the bool is assumed to have been set previously) then the transaction fails.

*EDIT: Oh and careful ar2007, try not to confuse people with verbose replies when a simple case will do. I've made that mistake too many times in the past. Most cases, people just want clarification and I believe this is one of those times ;)
Last edited on
I understand it now (to a certain degree).

basically, instead of writing out both the balance is 0 if statement and the bLimitReached if statement then I can make an Operator of the two to keep my code shorter and more efficient. It also cuts down on time.

all this is shortened by using the && (and and) and as the name suggests works if the first statement is true "and" at the same time the second statement:
1
2
if ((x> 0) && (x <= 10))
{Statement 1 ...


I understand what you are saying basically. If both of the codes are correct then the if statement will run (numbers 1-10), but if either one of them are not true, the statement will be skipped.

So, if I used the || and I used something like this:

1
2
3
4
if (x < 5) || (x==6)
{ cout << "blah blah";

};


The numbers can be anything less than 5 OR the number 6, but nothing else.

Is it possible to combine more than two as well? Like:

1
2
3
4
5
6
7
if (tail == yes) && (marine == yes) && (pounds >= 6000)
{

cout << "Your animal is a whale!";

};


Would that work? Thank you!
Is it possible to combine more than two as well? Like:

Absolutely. You can have as many compound conditions are you like.

BTW, you're missing a set of parens on that if statement.
 
if ( (tail == yes) && (marine == yes) && (pounds >= 6000)) 
Thanks for correcting me, it's so much easier now xD.

Awesome community, thanks for the prompt response and helping me out!
Topic archived. No new replies allowed.