2.3
C
Expressions
In the C language, expressions may
either be arithmetic or logical, but the result would be an integer or a real
value.
2.3.1
Arithmetic
Expressions
The following table will show the
different operators and their uses. The rule shows the type of operands where
the operator can be used and the resulting data type given the operands.
14
Overview
to C Language
Operator
Meaning
Rule
Example
+
addition
integer + integer = integer
5 + 2 = 7
integer + real = real
5 + 2.0 = 7.0
real + integer = real
5.0 + 2 = 7.0
real + real = real
5.0 + 2.0 = 7.0
-
subtraction
integer - integer = integer
5 - 2 = 3
integer - real = real
5 - 2.0 = 3.0
real - integer = real
5.0 - 2 = 3.0
real - real = real
5.0 - 2.0 = 3.0
*
multiplication integer * integer =
integer
5 * 2 = 10
integer * real = real
5 * 2.0 = 10.0
real * integer = real
5.0 * 2 = 10.0
real * real = real
5.0 * 2.0 = 10.0
/
division
integer / integer = integer
5 / 2 = 2
integer / real = real
5 / 2.0 = 2.5
real / integer = real
5.0 / 2 = 2.5
real / real = real
5.0 / 2.0 = 2.5
%
remainder
integer % integer = integer
5 % 2 = 1
-5 % 2 = -1
5 % -2 = 1
-5 % -2 = -1
In evaluating arithmetic expressions,
the following rules should follow: 1. Parenthesis
First. Evaluate expressions that are
enclosed in parenthesis.
If there are nested parenthesis,
evaluate from inside out.
2. Operator Precedence. Evaluation of operators should follow a
hierarchy of priorities. Evaluate expressions with higher priority operators
first.
unary +, - (positive and negative)
highest
*, /, %
binary +, - (addition and
subtraction)
lowest
3. Associativity Rule. If
expression to be evaluated have operators that are in the same precedence
level, evaluate the expression from left to right.
15
Chapter
2
Example.
z = 8
a = 3
b = 9
w = 2
y = -5
z – ( a + b / 2 ) + w * -y
8 3 9 2 -5
4
7
5
10
1
11
2.3.2
Logical and Relational Expressions
In C, evaluation of logical and
relational expressions return 0 for false and 1 (or any nonzero value) for
true.
The relational and equality operators
are shown below.
Relational
Operators
Equality
Operators
< less
than
== equal
<=
less than or equal
!=
not equal
> greater
than
>=
greater than or equal
The following shows the logical
operators and their corresponding truth tables.
Logical
Operator
Meaning
Truth
Table
&&
and
true && true = true
true && false = false
false && true = false
false && false = false
|
or
true || true = true
true || false = true
false || true = true
false || false = false
!
not
!(true) = false
!(false) = true
16
Overview
to C Language
Example.
salary < MIN_SALARY || dependents
> 5
temperature > 90.0 &&
humidity > 0.90
n >= 0 && n <= 100
!(0 <= n && n <= 100)
Rules in evaluating logical and
relational expressions are similar with evaluating arithmetic expressions.
These operators also follow precedence rules. The updated list is as follows:
Operator Precedence
(
)
highest
!, unary +, -
*,
/,
%
binary +, -
<, <=, >, >=
==,
!=
&&
|
lowest
Example.
flag = 0
y = 4.0 z = 2.0 x = 3.0
!flag || ( y + z >= x – z )
0
4.0 2.0 3.0 2.0
6.0
1.0
1
1
1
2.3.3
Converting Mathematical Formula to C Expression
To solve mathematical problems using
the computer, the formula should be translated to the programming language to
be used. In this case, arithmetic operations should be in C expressions.
Example.
b2 – 4ac
b * b – 4 * a * c
a+b
(a + b) / (c + d)
c+d
1
1 / (1 + x * x)
1+x2
a x –(b + c)
a * -(b + c)
17
Chapter
2
2.3.4
Converting English Conditions to C Expression
Solutions to problems may sometimes
depend on a set of conditions. To use the computer to solve such problems,
these conditions should be converted to the C.
Example.
x and y are greater than z
x > z && y > z
x is equal to 1.0 or 3.0
x == 1.0 || x == 3.0
x is in the range of z to y,
inclusive
z <= x && x <= y
x is outside the range of z to y
!(z <= x && x <= y)
z > x || x > y
Self
Evaluation Exercises
1.
Determine if the following
identifiers are valid or invalid.
a) L8r
b) star*ting
c) num_Values
d) 4u
e) one_i_aren’t
2.
Evaluate the following expressions
a.) 10 + 23 % (17 – 4 * 2) / (24 – (7
+ 15 % 2))
b.) 150 - (-6 + 8 * 4 – 22 % 4) – (5
– (15.2 / 2))
c.) (7 == 7.0) && ( 15 >
3) || !((7 >4) || (7 > 3)) d.) (8 > 13 % 3) || (7 > 22 % 3)
&& (5 == 30 / 6) 3.
Convert the following mathematical
equations to C expressions without adding unnecessary parenthesis
a.) 1 + X
1 + 1
6 8
b.) (X)(Y)(Z)
(X2 – Y2) + Y
4 + X
2 – Z
4.
Convert the following statements to C
expressions
a.) X is neither 6 nor 8
b.) X is any number except 1, 2, and
3
c.) REVENUE is at most 80% of SALES
d.) contestant’s HEIGHT is at least
175 cm and AGE is between 18 and 23, inclusive
e.) X is between 100 and 200,
exclusive, except 120, 130, and 180
18
Overview
to C Language
5.
Write the C statement that will
convert an amount in dollars to its peso equivalent.
Assume that PhP1 is equal to $51.75.
Chapter
Exercises
1.
Determine if the following
identifiers are valid or invalid.
a) 3id
b) 1_i_am
c) R3D3
d) int
e) per-capita
f) o_no_o_no
g) me_to-2
h) xYshouldI
i) phone#
j) MAX_SPEED
k) G
l) __yes
2.
Determine if the following are valid
or invalid whole number literals.
a) -10500
b) 435
c) 2,020
d) +50,000
e) 21.5
3.
Determine if the following are valid
or invalid real literals a) 2.34e2
b) 15e-0.3
c) 125
d) 34,500.99
e) 0.005
4.
Determine if the following are valid
or invalid character literals.
a) ‘M’
b) ‘n1’
c) ‘\’
d) ‘”’
e) ‘+’
f) ‘&’
5.
Given x = 2.0 and y = 3.0, evaluate
the following:
a) 2 – 4 * 3 + 26 / 2
b) (3 + 4) * x / 2 + y
c) 5 + 6.6 / 2.2 * 0.5
19
Chapter
2
6.
Given i = 1, j = 2, k = 3, x = 5.5,
and y = 7.7, evaluate the following whether they yield a value of TRUE/FALSE:
a) i < (j – k)
b) (x – y) <= ((j – k) – 1)
c) (k + j) != (i + 1 * 4)
d) ! (1 == 1)
e) i && j
f) i == j && i + j == k || y
== x + 2
g) –i <= j – k && !j
7.
Assume the values a = 1, b = 2, c =
0, d = 5.0, and e = 25. What is the output of the following:
a) a + b * c && a
b) 3 / a + b / e || c
c) 10 + 15 || 0 && 5 > 3 +
3
d) 1 + 2 > 3 * 4 || 5 && 3
> 4 == 0
e) 1 % 2 + 1 == 0 + 1 && 2
8.
Convert the following conditions to C
expressions:
a) Commission = (sales – sales X .10)
* .25
b) Commission = (sales – sales X
10/100) * 25/100
c) Interest = Amount X Rate
d) Semiannual Interest = 600/10%
e) age is from 18 to 21 inclusive
f) water is less than 1.5 and also
greater than 0.1
g) year is divisible by 4
h) speed is not greater than 55
i) y is greater than x and less than
z
j) w is either equal to 6 or not
greater than 3
9.
Write the C statement that will
compute for the area of a triangle given the base and the height.
10.
Write the C statement that will
convert a Fahrenheit (F) measure to a Celsius (C) measure. (C = 5/9 x (F – 32))
11.
Write the C statement that will
convert an amount in peso to its dollar equivalent.
Assume that PhP1 is equal to $51.75.
12.
Write the C statement(s) that will
compute the least number of Php5 and Php1 coins given an amount. Example: There
are 3 Php5 and 2 Php1 in Php17.
20
Walang komento:
Mag-post ng isang Komento