Tuesday, July 14, 2009

Programming in C, super easy logical expressions. Can someone explain this to me, should be really easy?

What are all these symbols like || !a and %26amp;%26amp;?





1. Determine the value of each of the following logical expressions TRUE of FALSE, Assume a = 5, b = 10 and c = -6





(a) a %26gt; b || !a


(b) a %26lt; b %26amp;%26amp; !(a + c)


(c) a - c || b %26gt; (a –c)


(d) !(b ==0) %26amp;%26amp; c %26lt; 0 || a %26gt; 5


(e) a == 0 %26amp;%26amp; ( b!=0 || c %26lt; 0.0)

Programming in C, super easy logical expressions. Can someone explain this to me, should be really easy?
Nothing in C is easy.





Get a copy of The C programming language byn Kernighan and Ritchie and start reading.





%26amp; is a bitwise logical and


%26amp;%26amp; is the logical and operator


|, || same for OR


! is the logical negation operator





Now you know no more than you did before.





You are not going to get a syntax of C here. usually takes a book.
Reply:|| is a binary operator, the expression returns true if either the left or right side is true.





! is a unary operator, it negates (flips truth value)





%26amp;%26amp; is a binary AND operator, the expression is true if both the left and right side is true





%26amp;%26amp; and || can short-circuit, if the first expression is false or true (respectively) the left-hand side isn't evaluated.





Non-0 values are 'true'.
Reply:Understand that zero (0) equals FALSE and everything else is TRUE


|| is logiand OR


%26amp;%26amp; Logical AND


! Negator


== compare operator





a - FALSE


b - FALSE


c - TRUE


d - FALSE


e - FALSE
Reply:You've completed a) to c), I'm assuming you understand all the operators except for '=='.





'==' compares the value to the left and right of it. It is true if the two values are the same, and false otherwise. In English, it would be interpreted as (equals to).





for example, b == 0 would be false if b is 10. I'll let you complete the rest.





Good luck!
Reply:|| means 'or' so, either one of the statements must be true in order for it to be true. %26amp;%26amp; means 'and' meaning BOTH statements must be true in order for it to be true. The '!' means NOT, so whatever is behind that must be false/not there. For example a != b means that can't be equal to b.


No comments:

Post a Comment