Connect with us

Hi, what are you looking for?

Programming

C++ For Beginner – Operators

C++ For Beginner - Operators
C++ For Beginner - Operators

In this part of the C ++ tutorial, we will learn about operators. We will also repeat the data types and variables we learned in the past education in this lesson.

For those who do not read the first lesson, you can find it here. In the first lesson, we learned the installation of C ++ in Visual Studio Code, variables, data types, Output, and Input.

Subjects to Learn

  • Arithmetic Operators
  • Assignment Operator
  • Logical Operator
  • Comparison Operator
  • Bitwise Operator

Arithmetic Operators

We will learn about operations found in mathematics and operations specific to computer science.

// Basic C++ File

#include <iostream>
using namespace std;

int main() 
{
  int x = 10;
  int y = 20;

  cout << x+y;
  cout << x-y;
  cout << x*y;
  cout << x/y;
  cout << x%y;
  cout << x++;
  cout << y--;

  return 0;
}

Addition (+) = Adds together values

Subtraction (-) = Subtracts one value from another

Multiplication (*) = Multiplies two values

Division (/) = Divides one value by another

Modulus (%) = Returns the division remainder

Increment (++) = Increases the value of a variable by 1

Deincrement (–) = Deincreases the value of a variable by 1

Assignment Operators

The operators we use to assign one value to another, we call Assignment Operators, the equals operator we used earlier is also included in this subject.

These operators save the operation performed after each operation to the variable and the new value of the variable becomes the result of the operation.

int x = 10;

x += 10; // increase x by 10
x -= 10; // deincrease x by 10
x *= 10; // multiply x by 10
x %= 10; // modulos
x /= 10; // division x by 10

Logical Operator

Logical operators are used to comparing the results of values and include concepts such as, or, and, not.

These values will return a boolean data type result, meaning they will return true or false values. You can use operators to find out if a result is true or false.

#include <iostream>
using namespace std;

int main() 
{
  bool x = true;
  bool y = false;

  cout << "\nAnd:" << y && x;
  cout << "\nOr:" << x || y;
  cout << "\nNot:" << !(x , y);

  return 0;
}
And:0
Or:1
Not:1

The “And” operator returns true if two values are true. For “Or”, 1 value is sufficient. The “Not” operator takes the inverse of a value.

Comparison Operator

These operators are used to compare two values, which we will use later in decision structures.

#include <iostream> 

using namespace std;

int main() 
{

  bool x = true;   
  bool y = false;
   
 cout << "\nEqual:" << (x == y);  
 cout << "\nNot Eaual:" << (x != y);    
 cout << "\nGreater than:" << (x > y);   
 cout << "\nGreater than:" << (x < y) << endl;        
 cout << (x >= y) << endl;    
 cout << (x <= y) << endl;
}
Equal:0
Not Eaual:1
Greater than:1
Greater than:0
Greater than or equal to:1
Less than or equal to:0
Click to comment

Leave a Reply

Your email address will not be published.

You May Also Like

Programming

In this article, we will create a simple timer app for beginners with Swift and SwiftUI. Step 1: Create a Header First, we will...

Programming

Swift optionals can be a bit confusing, in this article we’ll go into detail about optionals. We will look at how they can be...

News

According to a study from Dartmouth College, Dartmouth’s Tuck School of Business, and Indiana University, artificial intelligence systems can be trained to produce human-like...

Cyber Security

TCP/IP is a set of rules that allow computers to communicate with each other on a network such as the Internet. Currently, we do...