KEMBAR78
Module 6 Additional Operators and Compound Assignment | PDF | Theory Of Computation | Arithmetic
0% found this document useful (0 votes)
3 views35 pages

Module 6 Additional Operators and Compound Assignment

Uploaded by

madajejoniza
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)
3 views35 pages

Module 6 Additional Operators and Compound Assignment

Uploaded by

madajejoniza
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/ 35

Lesson Title: Additional Operators

and Compound Assignment


Module 6
Lesson Objectives:
1.Use a compound assignment in
arithmetic operation in a program.
2.Trace C++ statements with bitwise
operators.
Increment (++) and Decrement (--)
Increment statement ++x or x++, is
same as x=x+1.

Decrement statement --x or x-- , is


same as x=x-1
Increment (++) and Decrement (--)
Post-increment and Post-Decrement
Meaning increment or decrement
happens after the current value is used.
(x++,x--)
Increment (++) and Decrement (--)
Pre-increment and Pre-Decrement
Meaning increment or decrement
happens before the current value is used.
(++x,--x)
Tips for Easy to Remember!
x++,x--
“Use now, subtract/add later”
--x,++x
“Subtract/add now, use later”
Example: Post Increment (x++)

Output
C++ Program
Example: Post – Decrement (x--)

Output
C++ Program
Example: Pre - Increment (++x)

Output
C++ Program
Example: Pre - Decrement (--x)

Output
C++ Program
Example: Increment (++) and Decrement (--)
Output
C++ Program
Compound Assignment
C++ provides a more general way of simplifying a
statement that modifies a variable through simple arithmetic.
Operator Example Same As
= x=5 x=5
+= x+=3 x=x+3
-x x-=3 x=x-3
*x x*=3 x=x*3
/= x/=3 x=x/3
%= x%=3 x=x%3
Bitwise Operators
These following 6 operator are bitwise
operators (work at bit-level, binary
numbers): & (bitwise AND), | (bitwise OR),
^ (bitwise XOR) , ~ (bitwise NOT) , >>
(right shift) , and << (left shift).
Bitwise AND [&]
C or C++ takes two numbers as operands and does
AND on every bit of two numbers. The result of AND
is 1 only if both bits are 1.
INPUT RESULT
A B C
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise OR [ | ]
C or C++ takes two numbers as operands and does
OR on every bit of two numbers. The result of OR if
any two bits is 1.
INPUT RESULT
A B C
0 0 0
0 1 1
1 0 1
1 1 1
Bitwise XOR [ ^ ]
C or C++ takes two numbers as operands and does
XOR on every bit of two numbers. The result of XOR
is 1 if the two bits are different
INPUT RESULT
A B C
0 0 0
0 1 1
1 0 1
1 1 0
Left Shift [ << ]
C or C++ takes two numbers, left
shift the bits of the first operand, the
second operand decides the number of
places to shift.
Right Shift [ >> ]
C or C++ takes two numbers, right
shift the bits of the first operand, the
second operand decides the number of
places to shift.
Bitwise NOT [ ~ ]
C or C++ takes one number and
inverts all bits of it.
Relational and Comparison Operator ( ==,!=,>,<,>=,<=)
Two expression can be compared using relational and
equality operators. For example, to know if the values are equal
or if one is greater than the other. The result of such an operation
is either true or false.
Operator Description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Conditional or Ternary Operator [ ?: ]

The ?: operator returns one of two


values depending on the result of an
expression.
Syntax: (expression 1) ? expression 2: expression 3
Conditional or Ternary Operator [ ?: ]
Process operation:
• If expression 1 evaluates to true, then the
expression 2 is evaluated.
• If expression 1 evaluates to false, then the
expression 3 is evaluated instead.
Another Syntax:
variable = (condition) ? expressionTrue : expressionFalse
Conditional or Ternary Operator [ ?: ]
The true and false data are called
Boolean literals.
The true and false in the memory of
the computer is equivalent to logic 1 and
0 respectively.
Example: Conditional or Ternary Operator [ ?: ]

•7==5 ? 4 : 3
Evaluates to 3, since 7 is not equal to 5.
•5>3 ? a:b
Evaluates to the value of a, since 5 is greater
than 3.
Example: Conditional or Ternary Operator [ ?: ]

Output
C++ Program
Example: Conditional or Ternary Operator [ ?: ]

Output
C++ Program
Example: Conditional or Ternary Operator [ ?: ]

Output
C++ Program
Example: Conditional or Ternary Operator [ ?: ]

19
x&1 1
1 which is TRUE
(Boolean Value)
Activity: Trace the program
OUTPUT
C++ Program
Activity: Trace the program
OUTPUT
C++ Program
Activity: Trace the program

OUTPUT
C++ Program
Activity: Trace the program

OUTPUT
C++ Program
Activity: Trace the program

OUTPUT
C++ Program
Activity: Trace the program

OUTPUT
C++ Program
Activity: Coding Time!
Create a program in C++ in which you
are to find the sum of three numbers. If
the result is greater than 50, the output
would be “undefined” thus, show the
result.

You might also like