Translate

C++ OPERATOR - What is? -


Operators are symbols used to be involved in the program to do surgery or manipulation.

example:

a = b + c * d / 4
                 a, b, c, d à disebut operand
                 =, +, *, /  à disebut operator

arithmetic operators:
Operator
Description
Example
+
Add
m + n
-
Substract
m – n
*
Multiply
m * n
/
Divide
m / n
%
Modulus
m % n
-
Negat
-m

Operators such as negation operator (-) is called a unary operator, as it requires only a single operand

example:


  1. #include <iostream>

  2. using namespace std;

  3. main()
  4. {
  5.  int m = 82, n = 26;
  6.  cout<<m<<" + "<<n<<" = "<<m+n<<endl;
  7.  cout<<m<<" - "<<n<<" = "<<m-n<<endl;
  8.  cout<<m<<" * "<<n<<" = "<<m*n<<endl;
  9.  cout<<m<<" / "<<n<<" = "<<m/n<<endl;
  10.  cout<<m<<" % "<<n<<" = "<<m%n<<endl;
  11.  cout<<"-"<<m<<" = "<<-m<<endl;
  12. }


Output:
82 + 26 = 108
82 - 26 = 56
82 * 26 = 2132
82 / 26 = 3
82 % 26 = 4
-82 = -82


Because the data type is int, then 82/26 = 3, in order to represent the actual values​​, use the float data type.

Another way of writing using arithmetic operators:

m = m + n       ó        m += n
m = m - n        ó        m -= n
m = m * n       ó        m *= n
m = m / n        ó        m /= n
m = m % n     ó        m %= n





INCREMENT DAN DECREMENT OPERATOR: 
Operator increment  à ++

Operator decrement à --




Example:

  1. #include <iostream>

  2. using namespace std;

  3. main()
  4. {
  5.    int m = 44, n = 66;
  6.    cout<<”m = “<<m<<”, n = “<<n<<endl;
  7.    ++m; --n;
  8.    cout<<”m = “<<m<<”, n = “<<n<<endl;
  9.    m++; n--;
  10.    cout<<”m = “<<m<<”, n = “<<n<<endl;
  11.    return 0;
  12. }


Output
m = 44, n = 66
m = 45, n = 65
m = 46, n = 64


It appears that the operator pre-increment and post-increment has the same effect, namely the one at m manambah value and add it back to m (m = m +1). The same thing also happened on the carrier pre-decrement and post-decrement that gives the same result, which is to reduce the value of one of the n (n = n - 1).
But when used as a sub-expression, post-increment operator and pre-increment showed different results

example:
  1. #include <iostream>

  2. using namespace std;

  3. main()
  4. {
  5.    int m = 66, n ;
  6. n = ++m;
  7.    cout<<"m = "<<m<<", n = "<<n<<endl;
  8. n = m++;
  9.    cout<<"m = "<<m<<", n = "<<n<<endl;
  10.    cout<<"m = "<<m++<<endl;
  11. cout<<"m = "<<m<<endl;
  12.    cout<<"m = "<<++m<<endl;
  13. return 0;
  14. }

Output
m = 67, n = 67
m = 68, n = 67
m = 68
m = 69
m = 70


explanation:
In the first assignment, m is pre-increment, raising its value to 67, which is then put into n. In the second assignment, m is post-increment, so 67 is inserted into the first m n then its value is increased, which is why the value of m = 68 and n = 67. In the third assignment, m is post-increment, so that the value of m (= 68) displayed first (on the screen) and then the value of m raised to 69. In the fourth assignment, m is the pre-increment, so that the value of m is raised first to 70 and then displayed on the screen.
To be more aware, notice the example below.

  1. #include <iostream>

  2. using namespace std;

  3. main()
  4. {
  5.    int m = 5, n;
  6.    n = ++m * --m;
  7.    cout<<"m = "<<m<<", n = "<<n<<endl;
  8.    cout<<++m<<” “<<++m<<” “<<++m<<endl;
  9.    return 0;
  10. }

Output
m = 5, n = 25
8 7 6


explanation:
In assignment for n, m first increased (+ + m) to 6.
M then lowered back to 5, because of --m. So that m present value is 5 and the value of m = 5 is evaluated at the time of assignment multiplication done.
In the last line, the three sub-expressions are evaluated from right to left

BITWISE OPERATOR
Operator
Deskripsi
Contoh
<< 
Geser n bit ke kiri ( left shift )
m << n
>> 
Geser n bit ke kanan ( right shift )
m >> n
&
Bitwise AND
m & n
|
Bitwise OR
m | n
^
Bitwise XOR
m ^ n
~
Bitwise NOT
~m
The whole bitwise operators can only apply to operands of type int or char


Here is given the truth table for logical operators
P = A operator B
AND
OR
A
B
P
0
0
0
0
1
0
1
0
0
1
1
1
A
B
P
0
0
0
0
1
1
1
0
1
1
1
1

XOR

A
B
P
0
0
0
0
1
1
1
0
1
1
1
0
Example:
  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.  int m = 82, n = 26;
  6.  cout<<m<<" << 2"<<" = "<<(m<<2)<<endl;
  7.  cout<<m<<" >> 2"<<" = "<<(m>>2)<<endl;
  8.  cout<<m<<" & "<<n<<" = "<<(m&n)<<endl;
  9.  cout<<m<<" | "<<n<<" = "<<(m|n)<<endl;
  10.  cout<<m<<" ^ "<<n<<" = "<<(m^n)<<endl;
  11.  cout<<"~"<<m<<" = "<<~m<<endl;
  12.  }


Output:
82 << 2 = 328
82 >> 2 = 20
82 & 26 = 18
82 | 26 = 90
82 ^ 26 = 72
~82 = 83

explanation:
Output value of the above, depending on the type of the compiler used. The results of the above is the output of the Turbo C + + compiler.
In Turbo C + + is an integer greater than or equal to 2 bytes of 16 bit, to know to use the command
cout << sizeof (int) << endl; / / To know of the int

Another way of writing using bitwise operators:
m = m << n    ó        m <<= n
m = m >> n    ó        m >>= n
m = m & n      ó        m &= n
m = m | n        ó        m |= n
m = m ^ n       ó        m ^= n                          

OPERATOR RELATIONSHIP
Relational operators are used to compare two values​​. This operator is used in instruction branching.
Operator
Deskripsi
== same with ( not assignment )
!= Not same with
Greater Than
Smaller Than
>= Greater Than or Same With
<= Smaller Than or Same With

Example:

  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.    int m = 5, n =7;
  6.    if(m == n) cout<<m<<” same with ”<<n<<endl;
  7.    else  if(m != n) cout<<m<<” not same with ”<<n<<endl;
  8.    else  if(m > n) cout<<m<<” greater than ”<<n<<endl;
  9.    else  if(m < n) cout<<m<<” smaller than ”<<n<<endl;
  10.    return 0;
  11. }


Output:
5 smaller than 7


 LOGIC OPERATOR
Logical operators are used to connect two or more expressions into a conditional expression.

Operator
Deskripsi
Contoh
&&
logic AND
m && n
||
logic OR
m ||n
!
logic NOT
!m
example:
  1. #include <iostream>
  2. main()
  3. using namespace std;
  4. {
  5.    int m = 166;
  6.    cout<<”(m>=0 && m<=150) à “<<(m>=0 && m<=150)<<endl;
  7.    cout<<”(m>=0 || m<=150) à “<<(m>=0 || m<=150)<<endl;
  8. }


Output:
(m>=0 && m<=150) à 0
(m>=0 || m<=150) à 1

explanation:
Results / outputs of the logical operators are 0 and 1.
0 if output is false and 1 if the output is correct.

OPERATOR CONDITION
Operator conditions used to obtain the value of the two possible
condition1? condition2: condition3
When condition1 true value, then its value is equal to ungkapan2, if not then it is equal to condition3

example:
  1. #include <iostream>
  2. using namespace std;
  3. main()
  4. {
  5.    int m = 26, n = 82;
  6.    int min = m < n ? m : n;
  7.    cout<<”Smaller Integer is “<<min<<endl;
  8.    return 0;
  9. }


Output:
Smaller Integer is

No comments:

Post a Comment