UNIT 2 2.8 Operators Flashcards

1
Q

is a symbol that tells the compiler to perform specific mathematical, relational or
logical manipulations.

A

Operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Three General Classes of C Operators

A
  1. Arithmetic
  2. Relational
  3. Logical
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Arithmetic Operators:
* (asterisk)
/ (slash)
\+ (plus)
- (minus) 
- (unary minus)
% (modulus)
\++ (plus plus)
- - (minus minus) 
( ) (parenthesis)
A
*  = multiplication
/ = division
\+ = addition
- = subtraction
- = negation
% = modulus division
\++ = increment operator
- - = decrement operator
( ) = parenthesis
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Important Notes:
1. The operators +, -, * , and / all work the same way in C as they do in most computer
languages.

A

2+2 = 4 ganon

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Important Notes:

2. When / is applied to an integer or character, any remainder will be truncated.

A
Example
11/2 = 5, 
10/3 = 3, 
13/6 = 2, 
3/4 = 0, 
-34/3 = -11
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Important Notes:
3. The modulus division operator, % yields the remainder of an integer division. % cannot
be used on type float and double.

A
Example
11 % 2 = 1
5 % 3 = 2
147 % 20 = 7
6 % 3 = 0
-25 % 2 = -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Important Notes:
4. When an increment or decrement operator precedes its operand, C performs the
increment or decrement operation prior to using the operand’s value. If the operator
follows its operand, C uses the operand’s value before incrementing or decrementing it.

A

Example
X = 10;
Y = ++X;
X is 11; Y is 11

X = 10;
Y = X++;
X is 11; Y is 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

is to add exactly one to the value of a variable.

A

Increment

++ increment operator
X++ post increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

is to subtract exactly one from the value of a variable

A

Decrement
– decrement operator
++X pre increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Increment and Decrement Examples:

A

Example
1. X=X + 1 or X+=1 is the same with X++

  1. Y=10, X=3
    Z=Y + X++

Z=Y+X; 13
X=X + 1; 4

Z=Y+ ++X
X=X + 1; 4
Z=Y + X; 14

Z=Y + X–
Z=Y + X; 13
X=X – 1; 2

Z=Y+ –X
X=X – 1; 2
Z=Y + X; 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

It require only a single operand like unary plus and unary minus.

A

Unary Operators

Example
\+X, -Y,
- 
unary plus operator (+) 
– causes no change to the quantity which follows.
  • unary minus operator (-)
    – causes the sign of the following quantity to be
    changed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

It requires two operands

A

Binary Operators

Example
+ (addition) X + Y
* (multiplication) X – 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

is C’s “shorthand” operation.

A

Combined Operators

Example
X = X + Y;
using shorthand can be expressed as X+ = Y;

There are combined operators for all of C’s Binary Operators.

Example
Long Hand Notation 
X=X + Y; 
X=X – Y;
X= X * Y; 
X=X/Y; 
X=X % Y; 
(Combined) Shorthand
X+ = Y;
X - = Y;
X* = Y;
X/ = Y;
X% = Y;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

refers to the relationship values can have with one another. It is used to determine
the relationship of one quantity to another.

A

Relational

Relational Operators Recognized by C

Symbols

> greater than
:a > b

<
less than
:a < b

> =
greater than or equal to
:ave >= 90

<=
less than or equal to
:ave <=90

= =
equal to
:a = = b

! =
not equal to
:a != 100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

refers to the ways the relationships can be connected together using the rules of formal
logic.

A

Logical

Logical Operators Recognized by C

&& and
|| or
! not

Note:
In C, true is any value other than 0; false is 0
1 - true
0 - false

Example:
N = 10 > 5 &amp;&amp; !(10 <9) || 3 <= 4
1 &amp;&amp; !( 0 ) || 1
1 &amp;&amp; 1 || 1
= 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

uses question mark and colon symbols

A

Ternary Operator

Syntax:
Expression1?
expression2 :
expression3;

Wherein, expression1, expression2 and expression3 are expressions;

expression1 is evaluated,
if true then
expression2 is evaluated and become the value of the expression;
otherwise,
expression3 is evaluated and its value becomes the value of the expression.

Example
 x=10, y = 5;
x>y?
 sum= x + y: 
diff = x – y;
17
Q

is used to string together several expressions.

A

comma operator

Example
X= (Y=3, Y++, Y*2); Y=10;

Y = 3 
X= (Y= Y-5, 25/Y);

Y = 4
X=5

Y = 8

FinallyX = 8

18
Q

are combinations of operators, constants and variables.

A

Expressions

19
Q

Precedence of Operators

A
1. Arithmetic 
! 
\++
 - -
* 
/ 
%
 \+
 -
2. Relational 
< 
<= 
> 
>=
= =
 ! =
  1. Logical
    &&
    | |
4. Other Operators ? :
= 
\+= 
-= 
*= 
/=