0% found this document useful (0 votes)
2 views

C Operators

The document provides an overview of C++ operators, including arithmetic, assignment, comparison, logical, and bitwise operators, along with examples and sample code. It also covers conditional statements such as if, else, and else if, and includes activities for practice. The document emphasizes the importance of operators in performing operations on variables and values in programming.

Uploaded by

Yurika Cuntapay
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Operators

The document provides an overview of C++ operators, including arithmetic, assignment, comparison, logical, and bitwise operators, along with examples and sample code. It also covers conditional statements such as if, else, and else if, and includes activities for practice. The document emphasizes the importance of operators in performing operations on variables and values in programming.

Uploaded by

Yurika Cuntapay
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

C++ Operators

3rd Quarter
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two
values:
Example:
int x = 100 + 50;
Although the + operator is often used to add together two values, like
in the example above, it can also be used to add together a variable
and a value, or a variable and another variable:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
C++ divides the operators into the
following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
Arithmetic Operators
Example
Sample Activity
Write the code of the following output:
Activity!
Write a program that will ask 2 numbers and will automatically compute for the
following:
• Sum
• Product
• Difference
• Quotient
• Modulus
• Increment by 1
• Decrement by 2

Note: make sure that each computation is labeled. Ex: The sum is: , The Product is: etc.
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
The addition assignment operator (+=) adds a value to a variable:
Bitwise Operators
In C++, bitwise operators perform operations on integer
data at the individual bit-level. These operations include
testing, setting, or shifting the actual bits.
Bitwise AND Operator (&)
• The bitwise AND & operator returns 1 if and only if both the operands
are 1. Otherwise, it returns 0.
• Truth Table
a b a&b

0 0 0

0 1 0

1 0 0

1 1 1
Sample Code
#include <iostream>
using namespace std;

int main() {
// declare variables
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;

return 0;
}
Bitwise OR Operator ( | )
• The bitwise OR | operator returns 1 if at least one of the operands is
1. Otherwise, it returns 0.
• Truth Table

a b a|b

0 0 0

0 1 1

1 0 1

1 1 1
Sample Code
#include <iostream>

int main() {
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a | b = " << (a | b) << endl;

return 0;
}
Bitwise XOR Operator ( ^ )
• The bitwise XOR ^ operator returns 1 if and only if one of the
operands is 1. However, if both the operands are 0, or if both are 1,
then the result is 0.
• Truth Table
a b a^b

0 0 0

0 1 1

1 0 1

1 1 0
Sample Code
#include <iostream>

int main() {
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a ^ b = " << (a ^ b) << endl;

return 0;
}
Review Activity! let x= 10, y=15,
z=8, ans;
____1. ans= x + y;
____2. ans=y-x;
____3. ans=z%x;
____4. Z&X
____5. X|Y
____6. Z^Y
Right Shift Operator ( >> )
• The right shift operator shifts all bits towards the right by a certain
number of specified bits. It is denoted by >>.
• When we shift any number to the right, the least
significant bits are discarded, while the most
significant bits are replaced by zeroes.
Left Shift Operator (<<)
• The left shift operator shifts all bits towards the left by a certain
number of specified bits. It is denoted by <<.
Sample Code
#include <iostream>
using namespace std;

int main() {

// a = 21(00010101)
unsigned char a = 21;

// The result is 00101010


cout << "a << 1 = " << (a << 1) << endl;

// The result is 000001010


cout << "a >> 2 = " << (a >> 2);

return 0;
}
Practice Activity! Let X=15
1. X<<5
2. X<<2
3. X>>3
4. X>>15
5. X<< 20
6. X>> 10
Comparison Operators
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make decisions.
The return value of a comparison is either 1 or 0, which means true (1) or false (0).
These values are known as Boolean values, and you will learn more about them in
the Booleans and If..Else chapter.
In the following example, we use the greater than operator (>) to find out if 5 is
greater than 3:
Example:
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
Logical Operators
As with comparison operators, you can also test for true (1) or false (0)
values with logical operators.
Logical operators are used to determine the logic between variables or
values:
If, Else If
C++ has the following conditional
statements:
• Use if to specify a block of code to be executed, if a specified
condition is true
• Use else to specify a block of code to be executed, if the same
condition is false
• Use else if to specify a new condition to test, if the first condition is
false
• Use switch to specify many alternative blocks of code to be executed
The if Statement
• Syntax
if (condition) {
// block of code to be executed if the
condition is true
}
• Example
if (20 > 18) {
cout << "20 is greater than 18";
}
The else Statement
• Syntax
if (condition) {
// block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
• Example
int time = 20;
if (time < 18) {
cout << "Good day.";
}
else {
cout << "Good evening.";
}
The else if Statement
• Syntax
if (condition1) {
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
}
else {
// block of code to be executed if the condition1 is false and condition2 is false
}
• Example
int time = 22;
if (time < 10) {
cout << "Good morning.";
}
else if (time < 20) {
cout << "Good day.";
}
else {
cout << "Good evening.";
}
ACTIVITY!
Write a code that will
check if the password you
entered is correct!
Default password is: acsci2025

OUTPUT WITH CORRECT PASSWORD OUTPUT WITH INCORRECT PASSWORD


Enter your username: Enter your username:
Enter your password: Enter your password:
CONGRATULATIONS! SORRY! WRONG PASSWORD

You might also like