• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Other

Operators in C++: A Complete Guide with Examples

Document preview thumbnail
Preview 2 out of 5 pages

This document explains the different operators in C++, including arithmetic, relational, logical, bitwise, and assignment operators. Learn how to use these operators in your programs with practical examples. Perfect for first-year Computer Science students.

Content preview

Operators in C++
Operators are symbols in C++ used to perform operations on variables and values.
They are the building blocks of C++ programming, enabling various computations
and decision-making processes.



1. Types of Operators in C++
1.1 Arithmetic Operators
Used to perform basic mathematical operations.

Operato Description Exampl Result
r e
+ Addition a+b Sum
- Subtraction a-b Difference
* Multiplication a*b Product
/ Division a/b Quotient
% Modulus (remainder) a % b Remainder

Example:

int a = 10, b = 3;
cout << "Sum: " << a + b << endl; // Output: 13
cout << "Remainder: " << a % b << endl; // Output: 1


1.2 Relational (Comparison) Operators
Used to compare two values, resulting in a boolean value (true or false).

Operato Description Example Result
r
== Equal to a == b true/false
!= Not equal to a != b true/false
< Less than a<b true/false

, > Greater than a>b true/false
<= Less than or equal to a <= b true/false
>= Greater than or equal to a >= b true/false

Example:

int a = 5, b = 10;
cout << (a > b); // Output: 0 (false)
cout << (a <= b); // Output: 1 (true)


1.3 Logical Operators
Used to combine or invert boolean expressions.

Operator Description Example Result
&& Logical a && b true if both are
AND true
` ` Logical OR
! Logical NOT !a true if a is false

Example:

bool a = true, b = false;
cout << (a && b); // Output: 0 (false)
cout << (a || b); // Output: 1 (true)
cout << (!a); // Output: 0 (false)


1.4 Assignment Operators
Used to assign values to variables. Some operators combine assignment with
arithmetic operations.

Operator Description Exampl Equivalent To
e
= Assignment a=b Assign b to a
+= Add and assign a += b a=a+b
-= Subtract and assign a -= b a=a-b

Document information

Uploaded on
January 22, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown
$5.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions