Intro to Programming: Manipulating Data

**OBJECTIVES** In this presentation, we will learn about:

The assignment operator (=) always works from right to left.

The left side must be a variable to receive data from the right side.

Arithmetic assignment operators include:

+= Addition assignment operator

-= Subtraction assignment operator

*= Multiplication assignment operator

/= Division assignment operator

%= Remainder assignment operator

Suppose we have the following variables. Here is an example:

`int x = 5;`

x += 3; // Equivalent to x = x + 3; Now x is 8

x -= 2; // Equivalent to x = x - 2; Now x is 6

x *= 4; // Equivalent to x = x * 4; Now x is 24

x /= 3; // Equivalent to x = x / 3; Now x is 8

x %= 5; // Equivalent to x = x % 5; Now x is 3

**UNARY MINUS OPERATOR**

To change the sign of a number, use the unary minus operator (-) before the number.

The operand can be any integer or floating-point number.

Example: Given x = 1.234, -x equals -1.234. Given x = -1.234, -x equals 1.234.

Let's consider a variable:

`float x = 5.7;`

float y = -x; // y is now -5.7

int a = -10;

int b = -a; // b is now 10

**INCREMENT AND DECREMENT OPERATORS**

Increment operator (++): ++x (pre-increment), x++ (post-increment).

Decrement operator (--): --x (pre-decrement), x-- (post-decrement).

`int x = 5;`

// Pre-increment: Increases x by 1 first and then uses the updated value.

int result1 = ++x; // x is now 6, result1 is 6

int y = 10;

// Pre-decrement: Decreases y by 1 first and then uses the updated value.

int result2 = --y; // y is now 9, result2 is 9

int a = 3;

// Post-increment: Uses the current value of a and then increases it by 1.

int result3 = a++; // result3 is 3, a is now 4

int b = 7;

// Post-decrement: Uses the current value of b and then decreases it by 1.

int result4 = b--; // result4 is 7, b is now 6

**RELATIONAL OPERATORS**

Six types of relations between expressions:

== Equal to

!= Not equal to

>

**Greater than**< Less than

>=

**Greater than or equal to**<= Less than or equal to

`int a = 5, b = 10;`

bool isEqual = (a == b); // false

bool isNotEqual = (a != b); // true

bool isGreater = (a > b); // false

bool isLess = (a < b); // true

bool isGreaterOrEqual = (a >= b); // false

bool isLessOrEqual = (a <= b); // true

**CAST OPERATOR**

Convert the data type of a variable, expression, or constant by using the cast operator.

General form: (data-type) x

`int x = 10;`

float y = (float)x; // Casting integer to float, y is now 10.0

double a = 5.7;

int b = (int)a; // Casting double to integer, b is now 5

**SUMMARY**

We've covered the following key operators:

Assignment operator (=): Assigns the right-side value to the left-side variable.

Arithmetic assignment operators (+=, -=, *=, /=, %=): Combine arithmetic operators with assignment.

Unary minus operator (-): Negates numeric values.

Increment and decrement operators (++ and --): Increase or decrease values.

Relational operators (==, !=, >, <, >=, <=): Compare expressions.

Cast operator: Change data type by prefixing it with (data-type).

# Intro to Programming: Manipulating Data

**OBJECTIVES** In this presentation, we will learn about:

The assignment operator (=) always works from right to left.

The left side must be a variable to receive data from the right side.

Arithmetic assignment operators include:

+= Addition assignment operator

-= Subtraction assignment operator

*= Multiplication assignment operator

/= Division assignment operator

%= Remainder assignment operator

Suppose we have the following variables. Here is an example:

`int x = 5;`

x += 3; // Equivalent to x = x + 3; Now x is 8

x -= 2; // Equivalent to x = x - 2; Now x is 6

x *= 4; // Equivalent to x = x * 4; Now x is 24

x /= 3; // Equivalent to x = x / 3; Now x is 8

x %= 5; // Equivalent to x = x % 5; Now x is 3

**UNARY MINUS OPERATOR**

To change the sign of a number, use the unary minus operator (-) before the number.

The operand can be any integer or floating-point number.

Example: Given x = 1.234, -x equals -1.234. Given x = -1.234, -x equals 1.234.

Let's consider a variable:

`float x = 5.7;`

float y = -x; // y is now -5.7

int a = -10;

int b = -a; // b is now 10

**INCREMENT AND DECREMENT OPERATORS**

Increment operator (++): ++x (pre-increment), x++ (post-increment).

Decrement operator (--): --x (pre-decrement), x-- (post-decrement).

`int x = 5;`

// Pre-increment: Increases x by 1 first and then uses the updated value.

int result1 = ++x; // x is now 6, result1 is 6

int y = 10;

// Pre-decrement: Decreases y by 1 first and then uses the updated value.

int result2 = --y; // y is now 9, result2 is 9

int a = 3;

// Post-increment: Uses the current value of a and then increases it by 1.

int result3 = a++; // result3 is 3, a is now 4

int b = 7;

// Post-decrement: Uses the current value of b and then decreases it by 1.

int result4 = b--; // result4 is 7, b is now 6

**RELATIONAL OPERATORS**

Six types of relations between expressions:

== Equal to

!= Not equal to

>

**Greater than**< Less than

>=

**Greater than or equal to**<= Less than or equal to

`int a = 5, b = 10;`

bool isEqual = (a == b); // false

bool isNotEqual = (a != b); // true

bool isGreater = (a > b); // false

bool isLess = (a < b); // true

bool isGreaterOrEqual = (a >= b); // false

bool isLessOrEqual = (a <= b); // true

**CAST OPERATOR**

Convert the data type of a variable, expression, or constant by using the cast operator.

General form: (data-type) x

`int x = 10;`

float y = (float)x; // Casting integer to float, y is now 10.0

double a = 5.7;

int b = (int)a; // Casting double to integer, b is now 5

**SUMMARY**

We've covered the following key operators:

Assignment operator (=): Assigns the right-side value to the left-side variable.

Arithmetic assignment operators (+=, -=, *=, /=, %=): Combine arithmetic operators with assignment.

Unary minus operator (-): Negates numeric values.

Increment and decrement operators (++ and --): Increase or decrease values.

Relational operators (==, !=, >, <, >=, <=): Compare expressions.

Cast operator: Change data type by prefixing it with (data-type).