Download Programming Fundamentals for Engineers: Expressions and Operators in C and more Slides Programming for Engineers in PDF only on Docsity!
Programming Fundamentals for Engineers
2. Expressions
Muntaser Abulafi
Yacoub Sabatin
Dr. Labib Arafeh
Introduction
- (^) A typical programming scenario:
- (^) Read input data using scanf function,
- (^) Perform some calculations involving:
- (^) Evaluating expressions;
- (^) Storing results in certain variables by executing assignment statements , and
- (^) Output result using printf function;
- (^) Read & Output Covered previously;
- (^) Will focus on the calculation point by combining constants and variables with operators ( *+,-,/, , % ) to form expressions ;
- (^) Relational Operators:
- (^) > Greater than;
- (^) >= Greater than or equal to;
- (^) < Less than;
- (^) <= Less than or equal to;
- (^) == Equal to;
- (^) != Not equal to.
- (^) Logical/Boolean Operators:
- (^) && AND;
- (^) || OR;
- (^)! NOT.
- (^) The operators precedence ( priority ):
- Example: a=10.0 + 2.0 * 5.0 - 6.0 / 2.0 ; What is the answer?! ( Answer=17 ) Multiplication & Division parts will be evaluated first and then the addition and subtraction parts….
- (^) Note: To avoid confusion use brackets. The following are two different calculations: a=10.0 + (2.0 * 5.0) - (6.0 / 2.0) a=(10.0 + 2.0) * (5.0 - 6.0) / 2.
- (^) Level 1: ();
- (^) *Level 2: , /, %;
- (^) Level 3: +, -.
Example: Universal Product Code ( UPC) is a bar code for the product, to be able to identify it and know its price. It consists of 12 digits ( 1 , 5 , 5 , and 1 ) the 1 st ( d ) identifies the type of the item, the next 5 identify the manufacturer , the next 5 identify the product , then the final digit is ‘check digit’ which can tell if the previous 11 digits were read correctly or not, so to re-read them by the scanner once again if there is something wrong. The formula to calculate this digit:
- (^) first_sum = d + i2 + i4 + j1 + j3 + j
- (^) second_sum = i1 + i3 + i5 + j2 + j
- (^) total = 3 * first_sum + second_sum
- (^) check digit=9 - ( (total-1) % 10 )
- (^) The following example implements this algorithm:
*/ Computes a Universal Product Bar Code check digit */ #include <stdio.h> main() { int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total; printf("Enter the first (single) digit: "); scanf("%1d", &d); printf("Enter first group of five digits: "); scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5);
**
Assignment Statements
- (^) Once you've declared a variable you can use it, but not until it has been declared - attempts to use a variable that has not been defined will cause a compiler error!
- (^) Using a variable means storing something in it
- (^) You can store a value in a variable by using assignment statements of the form: Variable name (v) = value or an expression(e);
- (^) Example: a=10; /*stores the value 10 in the int variable a */
11
Expression, e, is evaluated and its value is
assigned to v , ( overwriting the previously stored value; if any)
- (^) The assignment produces a result which is the value of v after assignment
- (^) e can be:
- (^) Constant i = 5 ; /* i is 5 */
- (^) variable j = i ; /* j is 5 */
- (^) expression k = 10 * i + j ; /* k is 55 */
- (^) Generally, v is an expression that have an lvalue ( a location in the machine’s memory). So an assignment i + j = 0; is illegal, because i + j has no lvalue or no place to store the Zero!
2 examples:
float a=10/3;
printf("The answer is: %f",a);
Would print:
The answer is: 3.
float a=10/3.0;
printf("The answer is: %f",a);
Would print:
The answer is: 3.
14 Compound Assignments
- (^) i = i + 3; A n assignment statement that adds 3 to the previous value stored in i
- (^) Compound assignment operators: *(+=, -=, *=, /=, %=)*
- (^) Allows to shorten statements: ( i = i + 3; ) to ( i += 3;) += adds value of right operand to variable to left
- (^) Other compound assignments:
- (^) v += e Adds e to v , storing result in v ;
- (^) v -= e S ubtracts e from v , storing result in v
- (^) *v *= e * Multiplies v by e , storing result in v
- (^) v /= e Divides v by e , storing result in v
- (^) v %= e Computes the remainder when v is divided by e , storing result in v;
- (^) What are the final values of X, Y, & Z: X = Y = Z = 10 ;/ The = operator is right* associative, so this assignment is equivalent to: *X=(Y=(Z=10)); / X += 15; Y -= 15; Z %= 3;
- (^) X = ?, Y = ?, Z =?
- (^) Watch out an unexpected results in chained assignments and type conversion: int i; float f; f=i=33.3;
- (^) i is assigned the value 33 (because: int), then f is assigned 33.0 (not 33.3, as you might think)!
Increment & Decrement
- (^) Another way to increment variables is using:
- (^) ++ (increment) (add 1 to operand) and
- (^) -- (decrement) (subtract 1 from operand)
- (^) ++ and -- can be used as:
- (^) prefix operators ( ++i and --I) Variable is changed before the expression is evaluated.
- (^) postfix operators ( i++ and i-- ) Variable is not changed until after it is used in the expression. Simple Assignment Compound Increment i = i + 1 i += 1 ++i j = j - 1 j - = 1 --j
- (^) The expressions:
- (^) k = 2 * ++i
- (^) Add one to i ,
- (^) Store the result back in i ,
- (^) Multiply i by 2, and
- (^) Store that result in k****.
- (^) k = 2 * i++
- (^) Take i 's old value and multiply it by 2 ,
- (^) Increment i , and
- (^) Store the result of the multiplication in k****.
- (^) The -- operator works in the same way.
Example 1: _i = 1; printf (“i is %d \n”, ++i); / prints i is 2 / printf (“i is %d \n”, i); / prints i is 2 /_ Example 2: _i = 1; printf (“i is %d \n”, i++); / prints i is 1 / printf (“i is %d \n”, i); / prints i is 2 /_ Example 3: _a= 3; printf("a=%d, a+1=%d \n", a, ++a); / incremented before evaluation, then passed / / will print out a=4, a+1=4 /_