You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/operators.md
+66-40Lines changed: 66 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ description: "Learn java"
7
7
---
8
8
9
9
Operators are symbols that tell the compiler to perform some specfic operations.
10
-
In JAVA, there are many operators, and they are grouped into several categories.
10
+
In C++, there are many operators, and they are grouped into several categories.
11
11
We are going to learn some of the important operators only. You will see these operators in most of the section and this will cover almost every problem you will encounter in your programming journey.
12
12
13
13
Ok, So there are basically 6 categories of operators:
@@ -31,9 +31,9 @@ Arithmetric operators are used to perform arithmetic operations. They are used t
31
31
32
32
Ok so let's learn about the incrementer and decrementer operators now because they are very important.
33
33
34
-
### Incrementer and Decrementer Operators/ Unary Operators
34
+
### Incrementer and Decrementer Operators
35
35
36
-
In Java there are two types of incrementer and decrementer operators:
36
+
In C++ there are two types of incrementer and decrementer operators:
37
37
38
38
-**Incrementer Operator**: used to increment the value of the operand by 1.
39
39
-**Decrementer Operator**: used to decrement the value of the operand by 1.
@@ -77,25 +77,25 @@ Logical operators are used to perform logical operations. They are used to perfo
77
77
78
78
let's see an example:
79
79
80
-
```Java
80
+
```cpp
81
81
// program to find whether a number is divible by both 3 and 5
82
82
// or divisible by only one of them
83
-
importjava.util.Scanner;
84
-
publicclassMain
85
-
{
86
-
publicstaticvoidmain(String[] args) {
87
-
Scanner sc=newScanner(System.in);
88
-
System.out.println("Enter a number: ");
89
-
int a=sc.nextInt();
90
-
91
-
if(a%3==0&& a%5==0)
92
-
System.out.println("The number is divisible by both 3 and 5");
83
+
84
+
#include<iostream>
85
+
usingnamespacestd;
86
+
87
+
intmain() {
88
+
int a;
89
+
cout<<"Enter a numbers: ";
90
+
cin>>a;
91
+
if(a%3==0 && a%5==0)
92
+
cout<<"the number is divisible by both 3 and 5";
93
93
else if(a%3==0 || a%5==0)
94
-
System.out.println("The numbers is divisible by only one of them");
94
+
cout<<"the numbers is divisible by only one of them";
95
95
else
96
-
System.out.println("The numbers is not divisible by any of them");
96
+
cout<<"the numbers is not divisible by any of them";
97
+
return 0;
97
98
}
98
-
}
99
99
```
100
100
101
101
let's see the explanation:
@@ -174,7 +174,7 @@ eg: let 8 >> 1 == ?
174
174
175
175
here one thing to keep in mind that `>>` operator shifts the bits to the right and `<<` shifts the bits to the left. there is a way to find right shift and left shift of a number.
176
176
177
-
```java
177
+
```cpp
178
178
Right Shift --> a >> n ==> a *2^n
179
179
Left Shift --> a << n ==> a /2^n
180
180
```
@@ -183,39 +183,65 @@ Left Shift --> a << n ==> a / 2^n
183
183
184
184
This category contains the following operators:
185
185
186
+
- `sizof()`: it returns the size of the operand in bytes.
187
+
188
+
```cpp
189
+
int a=5;
190
+
cout<<"the size of a is "<<sizof(a)<<" bytes";
191
+
```
192
+
193
+
The next one is:
194
+
186
195
- `condition ? true_expression : false_expression`: it evaluates the true_expression if the condition is true and false_expression if the condition is false.
(a > b) ? cout<<"a is greater than b"<<endl : cout<<"a is less than b";
204
+
return0;
200
205
}
201
-
}
206
+
```
207
+
208
+
- `cast`: it converts the value of the operand to the type of the right operand.
202
209
210
+
```cpp
211
+
eg: int ch ='a';
212
+
int ch = (int)'a';
203
213
```
204
214
205
-
- `typecasting`: it converts the value of the operand to the type of the right operand.
206
-
eg:
207
-
```java
208
-
publicclassMain
209
-
{
210
-
publicstaticvoidmain(String[] args) {
211
-
char ch ='a';
212
-
int typecasted_character = (int)'a';
213
-
System.out.println(ch);
214
-
System.out.println(typecasted_character);
215
+
-`comma(,)`: causes a series of operations to occur in the order they are written.
216
+
217
+
```cpp
218
+
intmain() {
219
+
int a=5, b=6;
220
+
(a, b) = (b, a);
221
+
return 0;
215
222
}
216
-
}
217
223
```
224
+
225
+
- `Reference(&) operator`: it is used to get the address of the operand.
226
+
227
+
```cpp
228
+
int main() {
229
+
int a=5;
230
+
cout<<"the address of a is "<<&a<<endl;
231
+
return 0;
232
+
}
233
+
```
234
+
235
+
-`dereference(*) or pointer`: it is used to get the value of the operand. it is also known as \* pointer. Pointer is used to access the memory location of the variable.
236
+
237
+
```cpp
238
+
intmain() {
239
+
int a=5;
240
+
cout<<"the value of a is "<<*&a<<endl;
241
+
return 0;
242
+
}
243
+
```
244
+
218
245
## **Operators Precedence**
219
246
220
247
Operators Precedence is the order in which the operators are evaluated. Below is the precedence of the operators.
0 commit comments