Subscribe by Email


Showing posts with label Assignment. Show all posts
Showing posts with label Assignment. Show all posts

Tuesday, September 6, 2011

What is Assignment and Logical Comparison in C...

An expression as we all know is composed of one or more operations. When the expression is terminated by a semi colon, it becomes a statement which is the small executable unit in any program. The assignment statements are used to assign a value to a variable. The assigned value can be a constant variable or an expression. An assignment statement can be written in general form as:

A=bcd;

Where A is a variable to whom we are assigning a value and bcd is the assigned value. The “=” sign is called assignment operator. Assignments can be chained together. The assigning operator “=” assigns the value to the left hand operand and returns the value of the assignment. Assignment statements are very much needed for variable initialization since variables are initialized using assignment statements. There are 2 ways to do this:
- Un-initialized variable
- Initialized variable
An un-initialized variable has to be initialized in separate statements whereas an initialized variable combines declaration and assignment in to one statement.

Examples are:
Int a;
a=3; ------------------------uninitialized variable
int a= 3; -------------------- initialized variable

Assignment also follows when you use dynamic initialization. Sometimes variables of different types are mixed with each other. It’s a very common and observed phenomenon. In such cases a type conversion takes place. Here also it follows from the principal of assignment statement that “the value of the right side of the expression or of the assignment is converted to the type of the variable on left side i.e., target variable. Both the sides of assignment should be compatible with each other for type conversion. When conversion takes place from smaller data type to a larger data type no data is lost. Precedence of operators while assigning values with an expression should always be kept in mind.
Some programs need the power of decision making or comparison. This is granted through logical expressions which are nothing but the statements resulting into a 0 (true) or 1 (false) value. These are a combination of constants, variables and logical and relational operators. Be careful that two or more variables and operators should not occur in continuation. A logical expression may contain just one signed or unsigned variable or a constant or it may have two or more also joined by varied relational and logical operators. Following are some valid logical operators: the logical OR operator (||), the logical AND operator (&&) and the logical NOT (!) operator. The OR operator combines 2 expressions as its operands. If either of its operand evaluates to true, the OR operator also evaluates to true. This operator is basically used for testing evaluating expressions. The AND operator combines 2 expressions into one and the operator evaluates to one if and only if both the operands evaluate to 1. The NOT operator works on a single operand since it is a unary operator. It is used to negate or reverse the truth value of the operand. It has a higher precedence than of the relational and logical operators. Therefore, this should be enclosed within parentheses. This operator is useful as a test for zero. OR and AND operators have lower precedence than relational operators. Relational operators are used to define relationships between variables. C provides 6 basic relational operators : < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), and != (not equal to). Do not confuse the = and the == operators. “=” is assignment operator whereas “==” is relational equality operator.


Sunday, August 28, 2011

Assignment and logical comparison in the C language

It is necessary to understand how C expresses logical relations. C treats logic as being arithmetical expression. The value 0 (zero) represents false, and all other values represent true. Code written by people uncomfortable with the C language can often be identified by the usage of #define to make a "TRUE" value. Because logic is arithmetic in C, arithmetic operators and logical operators are one and the same. there are a number of operators that are typically associated with logic:

Relational and Equivalence Expressions:
a < b 1 if a is less than b, 0 otherwise. a > b
1 if a is greater than b, 0 otherwise.
a <= b 1 if a is less than or equal to b, 0 otherwise. a >= b
1 if a is greater than or equal to b, 0 otherwise.
a == b
1 if a is equal to b, 0 otherwise.
a != b
1 if a is not equal to b, 0 otherwise

C does not have a dedicated Boolean type as many other languages do. 0 means false and anything else true. Often #define TRUE 1 and #define FALSE 0 are used to work around the lack of a Boolean type. It is a better idea to indicate what you are actually expecting as a result from a function call, as there are many different ways of indicating error conditions, depending on the situation. Another thing to note is that the relational expressions do not evaluate as they would in mathematical texts.

Logical Expressions
a || b
when EITHER a or b is true (or both), the result is 1, otherwise the result is 0.
a && b
when BOTH a and b are true, the result is 1, otherwise the result is 0.
!a
when a is true, the result is 0, when a is 0, the result is 1.

C uses short circuit evaluation of logical expressions. That is to say, once it is able to determine the truth of a logical expression, it does no further evaluation. we need not worry here about trying to access an out-of-bounds array element if it is already known that i is greater than or equal to zero.

Bitwise Boolean Expressions:
The bitwise operators work bit by bit on the operands. The operands must be of integral type. The six bitwise operators are & (AND), | (OR), ^ (exclusive OR, commonly called XOR), ~ (NOT), << (shift left), and >> (shift right). The negation operator is a unary operator which precedes the operand. The others are binary operators which lie between the two operands. The precedence of these operators is lower than that of the relational and equivalence operators; it is often required to parenthesize expressions involving these operators.

Assignment of values:
Programmers should take special care of the fact that the "equal to" operator is ==, not =. This is the cause of numerous coding mistakes and is often a difficult-to-find bug, as the expression (a = b) assigns a a value equal to b and subsequently evaluates to b; but the expression (a == b), called equality operator, checks if a is equal to b. It needs to be noted that, if you confuse = with ==, your mistake will often not be brought to your attention by the compiler. A statement such as if (c = 20) {} is considered perfectly valid by the language, but will always assign 20 to c and evaluate as true. A simple technique to avoid this kind of bug is to put the constant first.


Facebook activity