[go: up one dir, main page]

0% found this document useful (0 votes)
78 views16 pages

Computer Fundamentals and Programming Using Dev C++

This document provides information on various computer programming concepts including escape sequences, arithmetic operators, precedence and associativity of operators, relational operators, logical operators, and assignment operators. Examples are given to demonstrate the use and evaluation of expressions using these different operators. Exercises at the end provide practice evaluating expressions involving combinations of operators.

Uploaded by

Noelle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views16 pages

Computer Fundamentals and Programming Using Dev C++

This document provides information on various computer programming concepts including escape sequences, arithmetic operators, precedence and associativity of operators, relational operators, logical operators, and assignment operators. Examples are given to demonstrate the use and evaluation of expressions using these different operators. Exercises at the end provide practice evaluating expressions involving combinations of operators.

Uploaded by

Noelle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CC113(B)

Computer Fundamentals and


Programming Using
Dev C++
Escape Sequence
Command Description

\n New line or next line

\t Tab

\a Alarm or computer beep

\’ Display single quote

\” Display double quote

\\ Display backslash
Arithmetic Operators
Operator Operation Example Result

+ Addition 20 + 5 25

- Subtraction 20 – 5 15

* Multiplication 20 * 5 100

/ Division (Quotient) 20 / 5 4

% Modulus Division (Remainder) 20 % 5 0


Precedence of Arithmetic
Operators
Precedence Operator
Highest – (unary negation)
Middle */%
Lowest +–
Result from Expression Based
on Precedence
Expression Result
7+3*5 22, not 50
16 / 4 – 3 1, not 16
-5 + 3 * 4 7, not -8
20 + 12 % 4 20, not 0
Associativity of Operators
Operator Associativity

(unary negation) Right to left

*/% Left to right

+– Left to right
Comparison Relational
operators areOperators
used to evaluate whether
two numbers are equal, or if one is greater or less than
the other. It is sometimes denoted as relational
operators. The result of a relational operation is a
Boolean value that can only be true (1) or false (0),
according to its Boolean result.

The relational operators are binary, they compare two


operands. A statement with two operands and a
relational operator between them is called a relational
expression.
Name Operator Sample Evaluates
Equal == 100 == 50; False
50 == 50; True
Not Equal != 100 != 50; True
50 != 50; False
Greater
>  100 > 50; True
Than
50 > 50; False
Greater
>= 100 >= 50; True
Than
or Equal 50 >= 50; True
Less Than <  100 < 50; False
50 < 50; False
Less Than <= 100 <= 50; False
Logical Operators
Logical operators enable you to combine
comparisons in one if or else if statement. C++
performs the three basic logical operators namely:
Not, And, Or.

The logical NOT is represented by an exclamation


point (!), it has only one operand, located at its right,
and the only thing that it does is to inverse the value
of it, producing false if its operand is true and true if
its operand is false. Basically, it returns the opposite
Boolean value of evaluating its operand.
Logical NOT
Expression Evaluation
Evaluates to false because the
!(8 == 8)
expression at its right (8 == 8) is true.
Evaluates to true because (20 <= 25)
!(20<= 25)
would be false.
!true Evaluates to false.

!false Evaluates to true.


Logical AND and OR
The logical operators && and || are used
when evaluating two expressions to
obtain a single relational result. The
operator && corresponds with logical
operation AND. This operation results
true if both its two operands are true and
false otherwise.
Logical AND
X Y X && Y
True True True
True False False
False True False
False False False
Logical OR

X Y X || Y
True True True
True False True
False True True
False False False
Assignment Operator
The assignment operator assigns a value to a variable.
x = 20;

This statement assigns the integer value 20 to the variable x. The


part at the left of the assignment operator (=) is known as the
lvalue (left value) and the right one as the rvalue (right value).
The lvalue has to be a variable whereas the rvalue can either be a
constant, a variable, the result of an operation or any
combination of these.
The most important rule when assigning is the right-to-left rule:
The assignment operation always takes place from right to left,
and never the other way.
x = y;
For example:

x = 2 + (y = 5);

is equivalent to:
y = 5;
x = 2 + y;

That means: first assign 5 to variable y and then assign to


x the value 2 plus the result of the previous assignment
of y (5), leaving a with a final value of 7.

The following expression is also valid in C++:


a = b = c = 5;
Exercises
Activity
Instruction. Solve the following:

Let A= 7 B=4 C=3 D=2

1). ((A+A*B) >= (C*B+A +C+D*A)) AND ((A+D*2/B) != (C+D*B))


35 12+7+3+14 (36) 7+2*2/4 (8) 3+2*4 (11)
F T
F
2). NOT ((D+A*B < C*C+B+A +C+D*A) OR (A+B/D <= C+D*B))
30 37 9 11
T T
T
F

You might also like