Share
Go down
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Understanding AND Logic

on Wed Jul 25, 2018 9:05 am
Using the AND Operator

Most programming languages allow you to ask two or more questions in a single comparison by using a conditional AND operator , or more simply, an AND operator that joins decisions in a single expression.


When you use one or more AND operators to combine two or more Boolean expressions, each Boolean expression must be true for the entire expression to be evaluated as true. For example, if you ask, “Are you a native-born U.S. citizen and are you at least 35 years old?”, the answer to both parts of the question must be yes before the response can be a single, summarizing yes. If either part of the expression is false, then the entire expression is false.


One tool that can help you understand the AND operator is a truth table. Truth tables are diagrams used in mathematics and logic to help describe the truth of an entire expression based on the truth of its parts. Quick Reference 3-1 contains a truth table that lists all the possibilities with an AND operator. As the table shows, for any two expressions x and y , the expression x AND y is true only if both x and y are individually true. If either x or y alone is false, or if both are false, then the expression x AND y is false.


. Quick Reference 3-1 AND Logic Truth Table

For example, if you want to bill an extra amount to cell phone customers who make more than 100 calls that total more than 500 minutes in a billing period, you can use nested decisions, as shown in the previous section, or you can include both decisions in a
single expression by writing the following question:

callsMade > CALLS AND callMinutes > MINUTES


If the programming language you use allows an AND operator, you must realize that the question you place first (to the left of the AND operator) is the one that will be asked first, and cases that are eliminated based on the first question will not proceed to the second question. In other words, each part of an expression that uses an AND operator is evaluated only as far as necessary to determine whether the entire expression is true or false. This feature is called short-circuit
evaluation . The computer can ask only one question at a time; even when your pseudocode looks like the first example in Figure 3-1, the computer will execute the logic shown in the second example. Even when you use an AND operator, the computer makes decisions one at a time, and makes them in the order you ask them. As you can see in the truth table, if the first question in an AND expression evaluates to false, then the entire expression is false. In that
case, there is no point in evaluating the second Boolean expression. In other words, evaluating an AND expression is interrupted as soon as part of it is determined to be false.

You are never required to use the AND operator because using nested if statements can always achieve the same result. However, using the AND operator often makes your code more concise, less error-prone, and easier to understand.



Figure 3-1  Using an AND operator and the logic behind it

Logic Example


Figure 3-2 A coffee vending machine

Let's say for example x is the 5(five) peso coin, y is the flavor button, and the result is the coffee.

Will it give you a fresh hot coffee or not? Check this Truth Table.


Did you UNDERSTAND AND LOGIC?
If yes, would you please give different example/application of it?
        else comment down below your reactions/questions.
avatar
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 4:13 pm
Wow! This is so amazing. You explained it very well and the examples are really easy to interpret! But can I ask you if I am allowed to use two operators at the same time? Thank you! I hope someone we'll get to answer my question. Very Happy
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Wed Aug 01, 2018 4:22 pm
ilovebts wrote:Wow! This is so amazing. You explained it very well and the examples are really easy to interpret! But can I ask you if I am allowed to use two operators at the same time? Thank you! I hope someone we'll get to answer my question. Very Happy

Thank you Rash! Yes you can use two operators at the same time even three or more.
avatar
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 4:25 pm
Admin wrote:
ilovebts wrote:Wow! This is so amazing. You explained it very well and the examples are really easy to interpret! But can I ask you if I am allowed to use two operators at the same time? Thank you! I hope someone we'll get to answer my question. Very Happy

Thank you Rash! Yes you can use two operators at the same time even three or more.

Thank you Jude! But can you give me an example na gumagamit ng two or more operators?
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Wed Aug 01, 2018 4:52 pm
ilovebts wrote:
Admin wrote:
ilovebts wrote:Wow! This is so amazing. You explained it very well and the examples are really easy to interpret! But can I ask you if I am allowed to use two operators at the same time? Thank you! I hope someone we'll get to answer my question. Very Happy

Thank you Rash! Yes you can use two operators at the same time even three or more.

Thank you Jude! But can you give me an example na gumagamit ng two or more operators?

For example, assume that you need to achieve a score of at least 75 on each of three tests to pass a course.You can declare a constant MIN_SCORE equal to 75 and test the multiple conditions with a statement like the following:

if score1 >= MIN_SCORE AND score2 >= MIN_SCORE AND score3 >= MIN_SCORE then
     classGrade = "Pass"
else
     classGrade = "Fail"
endif
Guru
Posts : 19
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:13 pm
Sir. Is this right for the coffee vending machine example?
Just like in your example 1 represents TRUE and 0 represents FALSE.

Let X be the 5 peso coin and Y be the pressed button.
if X = 1 AND Y = 1 then
   COFFEE
else
   NO COFFEE
end if

If there is a better way to present it in pseudocode please reply.
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:25 pm
justjoking10 wrote:Sir. Is this right for the coffee vending machine example?
Just like in your example 1 represents TRUE and 0 represents FALSE.

Let X be the 5 peso coin and Y be the pressed button.
if X = 1 AND Y = 1 then
   COFFEE
else
   NO COFFEE
end if

If there is a better way to present it in pseudocode please reply.
don't expect that there is only 1 coin nor flavor, so you can use this:

if coin <> 0 AND flavor <>0
Give Coffee
else
No Coffee


Guru
Posts : 19
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:32 pm
Ok sir. I would expand my thinking to a more flexible and realistic manner next time. Thank you for a clear answer.
Posts : 1
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:33 pm
Can you site anothee examples sir so That we can clearly understand the logic sir?? Please???.
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:50 pm
yes! may paexample si sir!
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:57 pm
justjoking10 wrote:Ok sir. I would expand my thinking to a more flexible and realistic manner next time. Thank you for a clear answer.
woah! that's good as a programmer. To practice that, would you give another example of application of AND in our daily life?
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Wed Aug 01, 2018 6:58 pm
airojames wrote:yes! may paexample si sir!
para higit na maunawaan ng mambabasa
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 7:02 pm
sir pwede po ba gamitin yung tatlong logic sa isang program na magkakasama?(and,not,or)
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Wed Aug 01, 2018 7:05 pm
JULIAN wrote:Can you site anothee examples sir so That we can clearly understand the logic sir?? Please???.
What part is not clear to you?
Guru
Posts : 19
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Wed Aug 01, 2018 9:27 pm
Admin wrote:
justjoking10 wrote:Ok sir. I would expand my thinking to a more flexible and realistic manner next time. Thank you for a clear answer.
woah! that's good as a programmer. To practice that, would you give another example of application of AND in our daily life?

Try po sir. S pagkain may Go Grow and Glow foods. Sabi nila para maging healthy dpt kumain non.
if Go <> 0 AND Grow <> 0 AND Glow <> 0 then
PERSON IS HEALTHY
else
PERSON IS UNHEALTHY
endif
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Thu Aug 02, 2018 7:38 am
airojames wrote:sir pwede po ba gamitin yung tatlong logic sa isang program na magkakasama?(and,not,or)
Pwede para maging mas epektibo ang iyong program. Tandaan lamang na dapat tama ang paggamit sa mga logic na ito.
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Thu Aug 02, 2018 7:39 am
justjoking10 wrote:
Admin wrote:
justjoking10 wrote:Ok sir. I would expand my thinking to a more flexible and realistic manner next time. Thank you for a clear answer.
woah! that's good as a programmer. To practice that, would you give another example of application of AND in our daily life?

Try po sir. S pagkain may Go Grow and Glow foods. Sabi nila para maging healthy dpt kumain non.
if Go <> 0 AND Grow <> 0 AND Glow <> 0 then
   PERSON IS HEALTHY
else
   PERSON IS UNHEALTHY
endif
That's right. Talagang nauunawaan mo na ang AND Logic.
avatar
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Thu Aug 02, 2018 8:00 am
airojames wrote:sir pwede po ba gamitin yung tatlong logic sa isang program na magkakasama?(and,not,or)

Of course! Kaso baka di mo sya magamit ng maayos and maglead sya sa magulong program.
avatar
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Thu Aug 02, 2018 8:05 am
Admin wrote:
airojames wrote:yes! may paexample si sir!
para higit na maunawaan ng mambabasa
Naks naman! At your service si Admin!
avatar
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Thu Aug 02, 2018 8:05 am
airojames wrote:yes! may paexample si sir!
Payaman!
avatar
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Understanding AND Logic

on Thu Aug 02, 2018 8:11 am
Admin wrote:
ilovebts wrote:
Admin wrote:
ilovebts wrote:Wow! This is so amazing. You explained it very well and the examples are really easy to interpret! But can I ask you if I am allowed to use two operators at the same time? Thank you! I hope someone we'll get to answer my question. Very Happy

Thank you Rash! Yes you can use two operators at the same time even three or more.

Thank you Jude! But can you give me an example na gumagamit ng two or more operators?

For example, assume that you need to achieve a score of at least 75 on each of three tests to pass a course.You can declare a constant MIN_SCORE equal to 75 and test the multiple conditions with a statement like the following:

if score1 >= MIN_SCORE AND score2 >= MIN_SCORE AND score3 >= MIN_SCORE then
     classGrade = "Pass"
else
     classGrade = "Fail"
endif

Thank you so so much! This helped me a lot.
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Thu Aug 02, 2018 5:51 pm
ilovebts wrote:
airojames wrote:yes! may paexample si sir!
Payaman!
Paawerr!
avatar
Admin
Posts : 132
Join date : 2018-07-25
View user profilehttp://jmdomain.forumotion.asia

Re: Understanding AND Logic

on Thu Aug 02, 2018 6:00 pm
ilovebts wrote:
Admin wrote:
airojames wrote:yes! may paexample si sir!
para higit na maunawaan ng mambabasa
Naks naman! At your service si Admin!
Yes! I love to help others with this forum.
Newbie
Posts : 3
Join date : 2018-08-02
View user profile

Re: Understanding AND Logic

on Thu Aug 02, 2018 6:54 pm
Sir is it possible na magamit sa iisang program ang NOT, OR, AND? Or NOT, OR or NOT, AND?
Guru
Posts : 10
Join date : 2018-08-02
View user profile

Re: Understanding AND Logic

on Thu Aug 02, 2018 7:13 pm
Sir any programming language possible yung NOT logic?

Sent from Topic'it App
Sponsored content

Re: Understanding AND Logic

Back to top
Permissions in this forum:
You cannot reply to topics in this forum