forked from onecompiler/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe068db
commit b69d1d4
Showing
2 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
Let us understand the below terms before we get into more details. | ||
|
||
### 1. Operator | ||
|
||
An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. In the below example, `+` is the operator. | ||
|
||
```c | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int x, y, sum; | ||
x = 10; | ||
y = 20; | ||
|
||
sum = x + y; | ||
cout << "Sum : " << sum; | ||
|
||
return(0); | ||
} | ||
``` | ||
### Check result [here](https://onecompiler.com/cpp/3vmb5yays) | ||
|
||
### 2. Operand | ||
|
||
An operand is what operators are applied on. In the above example `x` and `y` are the operands. | ||
|
||
# Types of Operators in C | ||
|
||
## 1. Arithmetic Operators | ||
|
||
C++ arithmetic operators are used to perform arithmetic operations on operands. | ||
|
||
|Operator| Description | Example| | ||
|----|----|----| | ||
| + | Used to perform Addition | 8+2 = 10| | ||
| - | Used to perform Subtraction | 12-2 = 10| | ||
| * | Used to perform Multiplication | 5*2 = 10| | ||
| / | Used to perform Division | 100/10 = 10| | ||
| % | Used to return Remainder | 40%10 = 0| | ||
| ++ | Used to perform Increment | int a=10; a++; // a becomes 11| | ||
| -- | Used to perform Decrement | int a=10; a--; // a becomes 9| | ||
|
||
|
||
### Example | ||
|
||
```c | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int x, y, sum, diff, product, division, mod, inc, dec; | ||
x = 90; | ||
y = 10; | ||
|
||
sum = x + y; | ||
cout << "Sum : " << sum << endl; | ||
|
||
diff = x - y; | ||
cout << "Difference : " << diff << endl; | ||
|
||
product = x * y; | ||
cout << "Product : " << product << endl; | ||
|
||
division = x / y; | ||
cout << "Division : " << division << endl; | ||
|
||
mod = x % y; | ||
cout << "Remainder : " << mod << endl; | ||
|
||
inc = x++; | ||
cout << "x value after incrementing : " << x << endl; | ||
|
||
dec = x--; | ||
cout << "x value after decrementing : " << x; | ||
|
||
return(0); | ||
|
||
} | ||
``` | ||
### Check Result [here](https://onecompiler.com/cpp/3vmb6bf4b) | ||
|
||
## 2. Relational Operators | ||
|
||
C++ relational operators are used to compare two operands. | ||
|
||
| Operator | Description| Usage| | ||
|----|----|----| | ||
| == | Is equal to | x == y| | ||
| != | Not equal to | !=x | | ||
| > | Greater than | x > y | | ||
| >= | Greater than or equal to | x >= y| | ||
| < | Less than| x < y | | ||
| <= | Less than or equal to| x <= y| | ||
|
||
### Example | ||
|
||
```C | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int x = 90; | ||
int y = 10; | ||
|
||
if ( x == y) { | ||
cout << "x and y are equal" << endl; | ||
} | ||
|
||
if ( x != y) { | ||
cout << "x and y are not equal" << endl; | ||
} | ||
|
||
if ( x > y) { | ||
cout << "x is greater than y" << endl; | ||
} | ||
|
||
if ( x < y) { | ||
cout << "x is less than y" << endl; | ||
} | ||
} | ||
``` | ||
### Check Result [here](https://onecompiler.com/cpp/3vmb6ppv9) | ||
|
||
## 3. Bitwise Operators | ||
|
||
C++ bitwise operators are used to perform bitwise operations on operands. | ||
|
||
|Operator| Description| Usage| | ||
|----|----|----| | ||
| `&` | Bitwise AND | `(x > y) & (y > z)`| | ||
| `|` | Bitwise OR | `(x > y) | (y > z)`| | ||
| `^` | Bitwise XOR | `(x > y) ^ (y > z)`| | ||
| `~` | Bitwise NOT | `(~x)`| | ||
| `<<` | Bitwise Left Shift| `x << y`| | ||
| `>>` | Bitwise Right Shift| `x >> y`| | ||
|
||
## 4. Logical operators | ||
|
||
Below are the logical operators present in the C++. | ||
|
||
|Operator| Description| Usage| | ||
|----|----|----| | ||
| `&&` | Logical AND | `(x > y) && (y > z)`| | ||
| `||` | Logical OR | `(x > y) || (y > z)`| | ||
| `!` | Logical NOT | `(!x)`| | ||
|
||
## 5. Assignment Operators | ||
|
||
Below are the assignment operators present in the C++. | ||
|
||
|Operator| Description| Usage| | ||
|----|----|----| | ||
| = | Assign| int x = 10;| | ||
| += | Add and assign| int x=10; x+=30; // x becomes 40| | ||
| -= | Subtract and assign| int x=40; x-=10; // x becomes 30| | ||
| *= | Multiply and assign| int x=10; x*=40; // x becomes 400| | ||
| /= | Divide and assign| int x=100; x /= 10;// x becomes 10| | ||
| %= | Modulus and assign| int x=100; x%=10; // x becomes 0| | ||
| <<= | Left shift and assign| x <<= 2 is same as x = x << 2| | ||
| >>= | Right shift and assign| x >>= 2 is same as x = x >> 2| | ||
| &= | Bitwise and assign| x &= 10 is same as x = x & 10| | ||
| ^= | Bitwise exclusive OR and assign| x ^= 10 is same as x = x ^ 10| | ||
| `|=` |Bitwise inclusive OR and assign | `x |= 10 is same as x = x | 10`| | ||
|
||
### Example | ||
|
||
```C | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int x = 10; // assigning 10 to x | ||
cout << "x value: " << x << endl; | ||
|
||
x+=30; | ||
cout << "x value after += operation: " << x << endl; | ||
|
||
x-=10; | ||
cout << "x value after -= operation: " << x << endl; | ||
|
||
x*=10; | ||
cout <<"x value after *= operation: " << x << endl; | ||
|
||
x/=10; | ||
cout <<"x value after /= operation: " << x << endl; | ||
|
||
x%=10; | ||
cout <<"x value after %= operation: " << x; | ||
|
||
} | ||
``` | ||
|
||
### Check Result [here](https://onecompiler.com/cpp/3vmb6zaez) | ||
|
||
## 6. Misc Operator | ||
|
||
* Ternary Operator | ||
If the operator is applied on a three operands then it is called ternary. This is also known as conditional operator as a condition is followed by `?` and true-expression which is followed by a `:` and false expression. This is oftenly used as a shortcut to replace if-else statement | ||
|
||
### Example | ||
|
||
```c | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int x = 10; | ||
int y = 90; | ||
|
||
int z = x > y ? x : y; | ||
|
||
cout << "Larger Number is: " << z; | ||
} | ||
``` | ||
### Check Result [here](https://onecompiler.com/cpp/3vmb79s3q) | ||
|
||
* sizeof() | ||
|
||
This operator is used to return the size of a variable. | ||
|
||
```c | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
|
||
int x = 90; | ||
int y = sizeof(x); | ||
|
||
cout <<"Size of x is: " << y; | ||
} | ||
``` | ||
### Check Result [here](https://onecompiler.com/cpp/3vmb7cnag) | ||
|
||
## Summary | ||
|
||
| Operator type | Description| | ||
|----|-----| | ||
| Arithmetic Operator|+ , - , * , / , %| | ||
| comparision Operator| < , > , <= , >=, != , ==| | ||
| Bitwise Operator| & , ^ , | | ||
| Logical Operator| && , `||`, ! | | ||
| Assignment Operator|= , += , -= , *= , /= , %=, <<=, >>=, &=, ^=, `|=` | | ||
| Ternary Operator| ? : | | ||
| sizeof operator| sizeof() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
String is an ordered series of characters usually enclosed in single quotes (') or double quotes (“) quotations. | ||
|
||
## How to declare strings | ||
|
||
```java | ||
$string_var = value; | ||
``` | ||
value should be enclosed in either single quotes `'` or double quotes `“`. | ||
|
||
## Example | ||
|
||
```perl | ||
|
||
$str1 = 'Hello World! '; | ||
$str2 = "Happy learning!!"; | ||
|
||
print($str1); | ||
print($str2); | ||
``` | ||
### Check result [here](https://onecompiler.com/perl/3vnmqz42z) | ||
|
||
# String Functions | ||
|
||
Below are some of the useful string functions in Perl. | ||
|
||
| String Function | Description| | ||
|----|----| | ||
|length| This function is used to return the number of characters of a given string| | ||
|substr| This method is used to modify a substring in a string| | ||
|index| Searches for a substring in the given string and returns the position of the first occurrence of the substring if found| | ||
|rindex| Similar to index but searches for a substring from right to left| | ||
|reverse| This function is used to reverse a string| | ||
|lc| This function is used to convert the specified string to lowercase| | ||
|uc| This function is used to convert the specified string to uppercase| | ||
|crypt| This function is used to encrypt password| | ||
|q/string/| used to create single-quoted strings| | ||
|qq/string/| used to create double-quoted strings| | ||
|chr| to return ASCII or UNICODE character of a number| | ||
|hex| used to convert a hexadecimal string to it's equivalent decimal value| | ||
|oct| used to convert an octal number to it's equivalent decimal value| | ||
|ord| returns the ASCII value of the first character of a string| | ||
|sprintf| Formats string provided by the user and returns the formatted string to be used with print()| | ||
|
||
## Example | ||
|
||
```perl | ||
#!/usr/bin/perl | ||
use warnings; | ||
use strict; | ||
|
||
# Find the length of a string | ||
my $str = "Hello world! Happy learning!!"; | ||
print(length($str),"\n"); | ||
|
||
# extract substring from a string | ||
my $substr1 = substr($str, 0, 5); | ||
my $substr2 = substr($str, -10); | ||
|
||
print($substr1,"\n"); | ||
print($substr2,"\n"); | ||
|
||
my $i = "Happy"; | ||
my $p = index($str,$i); | ||
print(qq\Index of substring "$i" is "$p" in the string: "$str"\,"\n"); | ||
|
||
print("Upper case of the string:\n"); | ||
print(uc($str),"\n"); | ||
|
||
print("Lower case of the string:\n"); | ||
print(lc($str),"\n"); | ||
``` | ||
### Check result [here](https://onecompiler.com/perl/3vnqhk3bu) |