0% found this document useful (0 votes)
2 views6 pages

C++ Operators Explained with Examples

The document provides a series of programming examples demonstrating the use of various operators including assignment, arithmetic, compound assignment, increment, relational, logical, conditional, and comma operators. Each example includes code snippets and explanations of the outcomes based on given values. The document concludes with additional examples focusing on conditional and logical operators, detailing the results of specific expressions.

Uploaded by

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

C++ Operators Explained with Examples

The document provides a series of programming examples demonstrating the use of various operators including assignment, arithmetic, compound assignment, increment, relational, logical, conditional, and comma operators. Each example includes code snippets and explanations of the outcomes based on given values. The document concludes with additional examples focusing on conditional and logical operators, detailing the results of specific expressions.

Uploaded by

samutvinaemria
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Answer

1. Assignment Operator (=)


int a = 10, b = 4;
a = b;
b = 7;

After assigning, a takes the value of b (which is 4). Changing b afterward doesn’t affect a.
Answer: a = 4, b = 7

2. Arithmetic Operator (%)


29 divided by 6 gives a remainder of 5.
Answer: 5

3. Compound Assignment (+=)


int a = 8;
a += 3 * 2;

Multiply first: 3 × 2 = 6, then add to a: 8 + 6 = 14.


Answer: 14

4. Increment Operator (++a vs a++)


int a = 5, b;
b = ++a;

Prefix ++ increases a first, then assigns. So a = 6, b = 6.


Answer: a = 6, b = 6

5. Relational Operators
If a = 4, b = 2, c = 8 — all comparisons are false.
Answer: None

6. Logical Operators
(x < y) && !(y < 5)
With x = 5 and y = 10 → true && true → true.
Answer: true

7. Conditional (Ternary) Operator


int z = (a > b) ? a : b;
If a = 7, b = 3 → true → 7.
Answer: 7
8. Comma Operator (,)
a = (b = 3, b + 4);
First b = 3, then a = b + 4 = 7.
Answer: 7

9. Mixed Operators
c = (a+b) * 2 % 3;
(2+3)*2 = 10 → 10 % 3 = 1.
Answer: 1

10. Tricky Assignment


a = b = c = 5;
a += b -= c;
b = 0; a = 5.
Answer: a = 5, b = 0, c = 5
Part II – Conditional and Logical Operators

1.
a < b → true → a + b = 13
Answer: 13

2.
(10 > 5) && (5 > 0) → true && true → 1
Answer: 1

3.
a > b → false → b = 7
Answer: 7

4.
Both numbers even → true → 100
Answer: 100

5.
a < b → true → a*b = 36
Answer: 36

6.
!(p > q) → true → q - p = 8
Answer: 8

7.
Both true → a + b = 10
Answer: 10

8.
x odd, y even → true → x*y = 98
Answer: 98

9.
a/b == 4 → true → a + b = 15
Answer: 15

10.
All true → c - a = 4
Answer: 4
11.
Both true → a - b = 3
Answer: 3

12.
x even → x*y = 36
Answer: 36

13.
Both true → p + q = 14
Answer: 14

14.
int a = 12, b = 6;
int result = (!(a < b)) ? (a / b) : (b / a);

a<b → false → !(false) = true → choose a/b = 2


Answer: 2

15.
int a = 3, b = 8;
int res = ( (a*b > 20) && (b > a) ) ? b-a : a+b;

a*b=24 >20 → true

b>a → true

true && true → true → b-a = 5


Answer: 5

16.
int x = 10, y = 2, z = 5;
int result = ( (x > y) || (z > x) ) ? (x - z) : (z - y);

x>y → 10>2 → true

true || anything → true → choose x-z = 10-5=5


Answer: 5

17.
int m = 15, n = 3;
int res = ( (m % n == 0) && !(n == 0) ) ? m/n : n;

m%n=15%3=0 → true

n==0 → false → !(false) = true

true && true → true → m/n = 15/3=5


Answer: 5

18.
int a = 2, b = 4, c = 8;
int result = ( (a<b) && (b<c) && (a+c > b) ) ? (c/b) : (b/c);

a<b → 2<4 → true

b<c → 4<8 → true

a+c > b → 10>4 → true

all true → c/b = 8/4=2


Answer: 2

19.
int x = 6, y = 12;
int res = ( (y % x == 0) || (x > y) ) ? y/x : x*y;

y%x=12%6=0 → true

condition true → choose y/x = 12/6=2


Answer: 2

20.
int a = 5, b = 2, c = 7;
int result = ( (a > b) && !(c < b) ) ? (a+c) : (b+c);

a>b → 5>2 → true

c<b → 7<2 → false → !(false)=true


true && true → true → a+c = 12
Answer: 12

You might also like