c++|operators with c++|variables||

Programming codester 

Object oriented programming with c++ 

Operators and variables with c++ 


In the previous tutorial, we learnt about data types and user input cin. 

Question of c programming

What is the out of following program?

  1. #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         int *p = &i;
  6.         printf("%d\n", *p++);
  7.     }
AANSWER:10

Now we start a new tutorial! 

We will learn about different operators in c++(oop) 

Variables: 

Variables are containers to store any data.  

i.e., to store any biscuits, we use boxes. 

for example, int is used for integers, float is used for floating numbers, char is used for any character. 

So variable is the container who stores our values and data. 

Variable scope: 

  1. Local variable: are declared inside the braces of any function and can be accessed from there. 
  2. Global variable: are declared outside any function and accessed anywhere. 

 

We will learn about different operators in c++(oop). 

 

Operators: 

Operators are used to perform operations on variables and values. 

Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction. 

Types of operators: 

  1. Arithmetic operators 
  2. Assignment operator 
  3. Logical operator 
  4. Bitwise operator 
  5. Relational operator 
  6. Other operators 
in this tutorial we learn only arithmetic and assignment operator.

1. Arithmetic operators 

Arithmetic operators are used to perform common mathematical operations. 

 

Operator 

Name 

Description 

Example 

+ 

Addition 

Adds together two values 

x + y 

- 

Subtraction 

Subtracts one value from another 

x - y 

* 

Multiplication 

Multiplies two values 

x * y 

/ 

Division 

Divides one value by another 

x / y 

% 

Modulus 

Returns the division remainder 

x % y 

++ 

Increment 

Increases the value of a variable by 1 

++x 

-- 

Decrement 

Decreases the value of a variable by 1 

--x 

 
Let's see the example of arithmetic operators: 

 

OUTPUT: 

 

% Modulo Operator 

The modulo operator % computes the remainder. When a = 9 is divided by b = 4, the remainder is 1. 

Note: The % operator can only be used with integers. 

Increment and Decrement Operators 

C++ also provides increment and decrement operators: ++ and -- respectively. 

  • ++ increases the value of the operand by 1 

  • -- decreases it by 1 

For example, 

 

OUTPUT: 

 

 

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: 

Example 

int x = 10; 

The addition assignment operator (+=) adds a value to a variable: 

Example 

int x = 10; 
x += 5; 

Operator 

Example 

Equivalent to 

= 

a = b; 

a = b; 

+= 

a += b; 

a = a + b; 

-= 

a -= b; 

a = a - b; 

*= 

a *= b; 

a = a * b; 

/= 

a /= b; 

a = a / b; 

%= 

a %= b; 

a = a % b; 

 

Example: Assignment Operators 

 

OUTPUT: 

 

 

So, guys I hope you learnt basic operator of c++ and also learnt arithmetic and assignment operator in detail. 

In the next tutorial we will be learn other operators in detail. 

C tutorials: 

Thank you for your support! 

Learn and explore! 

 

 

Comments

Popular posts from this blog

C programming | Printf and scanf ||

C programming | structure | Fundamentals of Structure

C programming | Fundamentals & Basic information

Basic question related programming || c programming || embedded programming

C++ || Operators with c++

C++ || string with c++ || Part - 1

Compiler & Interpreter | difference and explanation

C programming | Nested structure | Array, function and pointer of structure|| structure Part-II

C++(oop)| Fundamentals | Concepts||