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?
#include <stdio.h>
int main()
{
int i = 10;
int *p = &i;
printf("%d\n", *p++);
}
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:
- Local variable: are declared inside the braces of any function and can be accessed from there.
- 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:
- Arithmetic operators
- Assignment operator
- Logical operator
- Bitwise operator
- Relational operator
- Other operators
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
Post a Comment