Go to Post Quitting when faced with adversity is not the answer. We have to figure out a solution within the constrainsts applied. - Rob [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 12-09-2011, 00:35
zanara zanara is offline
Registered User
no team
 
Join Date: Sep 2011
Location: Singapore
Posts: 3
zanara is an unknown quantity at this point
Simply question about C program

I am a newbie and have little knowlege about programming. I do not understand the role of the logical operators-AND-OR NOT C, and how are they used? Looking forward to your response!
  #2   Spotlight this post!  
Unread 12-09-2011, 08:04
MattSr's Avatar
MattSr MattSr is offline
Mentor/Programmer/Animator
AKA: Matt Cyril Senior
FRC #0488 (Team Xbot)
Team Role: Programmer
 
Join Date: Jan 2008
Rookie Year: 2006
Location: Seattle, Washington
Posts: 21
MattSr is an unknown quantity at this point
Send a message via AIM to MattSr Send a message via MSN to MattSr Send a message via Yahoo to MattSr
Re: Simply question about C program

"-AND-OR NOT C"

I'm a little confused by how you phrased this. Are you asking why and how you would use an "AND"?

Let me know if this does or doesn't answer your question.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
__________________


Much love to my favorite FRC teams:
1983 Skunkworks - 360 The Revolution - 2898 Flying Hegehogs - 2557 Sota Bots - 2942 Tech Robotics
  #3   Spotlight this post!  
Unread 12-09-2011, 08:51
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Simply question about C program

Quote:
Originally Posted by zanara View Post
I am a newbie and have little knowlege about programming. I do not understand the role of the logical operators-AND-OR NOT C, and how are they used? Looking forward to your response!

if (a && b) {do something if a AND b are both true}

if (a || b) {do something if either (or both) a OR b is true. ie, a and b are not both false.}

if (!a) {do something if a is NOT true}



Last edited by Ether : 12-09-2011 at 13:42. Reason: edited for clarity
  #4   Spotlight this post!  
Unread 12-09-2011, 11:04
GaryVoshol's Avatar
GaryVoshol GaryVoshol is offline
Cogito ergo arbitro
no team
 
Join Date: Aug 2005
Rookie Year: 2000
Location: Royal Oak, MI
Posts: 5,757
GaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond reputeGaryVoshol has a reputation beyond repute
Re: Simply question about C program

Quote:
Originally Posted by Ether View Post
if (a || b) {do something if either a OR b is true}
Or if both a and b are true - one or the other or both.
__________________
(since 2004)
  #5   Spotlight this post!  
Unread 12-09-2011, 12:43
Andrew Schreiber Andrew Schreiber is offline
Joining the 900 Meme Team
FRC #0079
 
Join Date: Jan 2005
Rookie Year: 2000
Location: Misplaced Michigander
Posts: 4,069
Andrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond repute
Re: Simply question about C program

I like to think of AND as a multiplication and OR as an addition. The analogy breaks down with negation though.

So you can think of A && B || (C && D) as A * B + (C*D) which is pretty easily solved by plugging 1 in for true and 0 in for false. 1*0+(1*1) = 1 (assuming A = 1, B = 0, C = 1, and D = 1)

I have to rely on tricks like this because I struggle to do boolean stuff for some reason. Figured I'd share the trick.
__________________




.
  #6   Spotlight this post!  
Unread 12-09-2011, 13:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,101
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Simply question about C program

Quote:
Originally Posted by GaryVoshol View Post
Or if both a and b are true - one or the other or both.
Yes.


  #7   Spotlight this post!  
Unread 12-09-2011, 13:41
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,815
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: Simply question about C program

Sometimes it's also helpful to use "truth tables" when dealing with logical operations (especially as they become more complex). Examples of the three operations being discussed here, where T indicates that the argument is logically TRUE (2+2 == 4), and F indicates it is logically FALSE (2+2 == 5)
Code:
 A  |  B  | A OR B
--------------------
 T  |  T  |    T
 T  |  F  |    T
 F  |  T  |    T
 F  |  F  |    F

 A  |  B  | A AND B
--------------------
 T  |  T  |    T
 T  |  F  |    F
 F  |  T  |    F
 F  |  F  |    F

 A  | NOT A
------------
 T  |    F
 F  |    T
This way, you show all possible combinations of A and B (where both are true, A is true and B is false, A is false and B is true, and both are false), and can join various combinations of operations together quite easily by simply adding an operation on columns already existing. For example, if we wanted the combined logical operation A OR ((NOT B) AND C), We first evaluate NOT B, then once we have that column, we can use it to evaluate (NOT B) AND C, and finally with that column we can evaluate the whole thing:

Code:
 A  |  B  |  C  |  NOT B  |  (NOT B) AND C  | A OR ((NOT B) AND C)
-------------------------------------------------------------------
 T  |  T  |  T  |    F    |        F        |         T
 T  |  T  |  F  |    F    |        F        |         T
 T  |  F  |  T  |    T    |        T        |         T
 T  |  F  |  F  |    T    |        F        |         T
 F  |  T  |  T  |    F    |        F        |         F
 F  |  T  |  F  |    F    |        F        |         F
 F  |  F  |  T  |    T    |        T        |         T
 F  |  F  |  F  |    T    |        F        |         F
So, we know that there are exactly 5 combinations of A, B and C that result in a "true" result, and 3 that do not.

I had a sophomore-level class in college that spent a lot of time on stuff like this. There are rules for simplifying logic statements and such that can be very powerful, but essentially rely on analysis like this to know what conditions lead to a True or False result.

Also a note on OR: With very few exceptions, OR means "Inclusive OR" - that it's true in every situation except when both arguments are false. Just about everything I've seen then makes a very distinctive difference for an XOR, or "Exclusive OR" operation - where it's only true if the two arguments are different - one is true and one is false. Essentially A XOR B = (A OR B) AND NOT (A AND B)

Last edited by Jon Stratis : 12-09-2011 at 13:50.
  #8   Spotlight this post!  
Unread 14-09-2011, 03:04
alphonsoferrell alphonsoferrell is offline
Registered User
no team
 
Join Date: Sep 2011
Location: Wichita
Posts: 5
alphonsoferrell is an unknown quantity at this point
Re: Simply question about C program

Quote:
Originally Posted by zanara View Post
I am a newbie and have little knowlege about programming. I do not understand the role of the logical operators-AND-OR NOT C, and how are they used? Looking forward to your response!
I guess what he meant was the AND, OR, and NOT boolean operators. Your question was really confusing at first. I am also a newbie in C but this stuff is quite basic. AND returns true only if both operands are true. OR returns true if one of the operand is true. NOT converts true to false and false to true.
  #9   Spotlight this post!  
Unread 15-09-2011, 22:36
zanara zanara is offline
Registered User
no team
 
Join Date: Sep 2011
Location: Singapore
Posts: 3
zanara is an unknown quantity at this point
Re: Simply question about C program

Quote:
Originally Posted by MattSr View Post
"-AND-OR NOT C"

I'm a little confused by how you phrased this. Are you asking why and how you would use an "AND"?

Let me know if this does or doesn't answer your question.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
okay, it does. Thanks very much for your help
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 19:02.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi