Pages

Friday, September 22, 2017

Easy tutorial for C / C++ Operator - 1

Easy tutorial for C / C ++ Operator - 1

- In C / C ++ language, there are various types of operators that allow constants and variables to be used for arithmetic operations such as + - * /, large / small / equal comparison / relational operations. 

- First, let's look at the types of simple and frequently used operators.

Assignment operator:

- Use the '=' symbol as an operator to store (assign) a value to a variable. In mathematics it is represented by an equal sign, but in programming, the assignment operator is used and the same is used for the '=='.

Example 1:

   int num = 3;

- int means the integer type mentioned on the previous page and num denotes the variable name. Assign (store) 3 to the variable num using the assignment operator '='.

Example 2:

#include <iostream>
using namespace std;

int main() {

     int num ;
     num = 3 ;
     cout << "First  num  = " << num << endl;

     num += 7 ;
     cout << "Second num  = " << num << endl;

     return 0;

}

result:

First num  = 3
Second num = 10

- First, 3 is stored in the integer variable num. If you enter num + = 7 in the next line, it is recognized as num = num + 7 and the resulting num is finally assigned a 10 (Second num). This is known as the cumulative assignment operator. Of course, '- =', '* =', '/ =' are also available.

Arithmetic Operators:

- The +, -, *, and / operators such as arithmetic operators in mathematics and the % operator to get the remainder are called arithmetic operators. 

- Arithmetic operations are the same as regular arithmetic operations. 

- For the % operator, the operand is only available for integer data.

Example 3:

#include <iostream>
using namespace std;

int main() {

     int a, b, c, d ;
          a = 5 ;
          b = 4 ;
          c = a + b ;
          d = c % b ;

     cout << "c = " << c << endl;
     cout << "d = " << d << endl;
    
          return 0;

}

results:

c = 9
d = 1

- Declare integer variables a, b, c, and d, substitute 5 for a, and 4 for b. If you assign a + b to the variable c, the result of the arithmetic operation (5 + 4) 9 is assigned to c.

- The variable d stores the operation result of c % b (9 % 4). The remainder of dividing 9 by 4 is 1 and this value is stored in variable d.

- The % operator is also used to compare even odd numbers. (Eg a % 2; an odd number if the result is 1, an even number if 0)

Increment / Decrement Operator:

This operator is frequently used as an operator that increases or decreases the value stored in a variable by 1. '++' means increment operator and '-' means decrement operator.

Example 4:

#include <iostream>
using namespace std;

int main() {

     int num;
          num = 1 ;
     cout << "(a) num = " << num << endl;
     num ++;
     cout << "(b) num = " << num << endl;
          num --;
     cout << "(c) num = " << num << endl;
     return 0;

}

results:

(a) num = 1
(b) num = 2
(c) num = 1

- Assigns 1 to integer variable num. And when you output it, you can see that 1 is output. We then use the increment operator to increment the value stored in num by 1 (num ++), and we can see that the value of num is 2.

- Finally, if you decrements the value stored in num by 1 (num --) using the decrement operator, you can see that the value stored in num has been changed to 1.

No comments:

Post a Comment