The concept of expression. Assignment operation. Converting and casting of types

The concept of expression. Assignment operation. Converting and casting of types


Content
  1. What is called an expression in programming languages?
  2. What is the general form of the assignment operation? Examples
  3. How is the assignment operation used when initializing variables?
  4. How is type conversion done in expressions?
  5. How are the conversions that are associated with the bool type?
  6. What is the general kind of type cast operation?
  7. What are the features of the use of parentheses and “space” characters in expressions?

1. What is called an expression in programming languages?

An expression is a union of statements, literals, and variables. Expressions are used to perform calculations, some actions, etc. Expressions can use the function names.

Examples of expressions.

a*8+5
Math::Sqrt(x)+Math::Sin(x+2)
sigma + gamma/20.0

 

2. What is the general form of the assignment operation? Examples

An assignment operation is used to specify a value for a variable. The value that is assigned can be the result of some expression.

In C/C++, the assignment operation is indicated by the symbol = (equal).

General view of the assignment operation:

variable = expression;

where

  • variable – the name of the variable to which the value of the expression is assigned;
  • expression – some expression that can be used according to the C/C ++ syntax.

Examples of the assignment operation.

a = 8;
b = a + Math::Sqrt(x)+Math::Sin(x+2);
sigma = (double) (2.0 / 8);

 

3. How is the assignment operation used when initializing variables?

 General view of the assignment operation when initializing variables:

type variable_name = expression_or_value;

where

  • type – the type of the variable that is initialized;
  • expression_or_value – value of expression or constant value. If the value of the expression is used, then the variables included in this expression must already be declared at the moment (see example).

Examples of initializing variables.

...

// Initialization of variables
int a = 8, b = 25;
double c = 3.550093;
bool f = false;
char sym = 'A';

// Initialization of variables with expression
float x = 3.5;
float y = x + 2.8;
float z = x*x + y*y*y;
float zz = Math::Sqrt(z+5.0);

...

 

4. How is type conversion done in expressions?

If two operands of different types are found in the expression, then the following rules apply:

  • all operands are converted to the type of the largest operand. The process of this transformation is called integral promotion;
  • all types char and short int are converted to int type. The process of such a transformation is called an integer promotion;
  • if one of the operands is of type double, then any other operand is reduced to the type double. Even, in the case of type ‘char’, there is a cast to the type double;
  • after the conversion, both operands have the same type, which is the type of the result of the operation.

The following are examples of automatic type conversion.

Conversion between char and int types:

char c;
int d;

c = 'A';
d = c; // d = 65

d = 67;
c = d; // c = 'C'

Conversion between int and float types:

int d = 28;
float x;

x = d; // x = 28.0 - the 'float' type
d = 5.0 + 5; // d = 10 - the 'int' type

Conversion between float and double types:

float f;
double d;
int size;

f = 2.54f;
d = f; // d = 2.54 - 'double' type
d = 2.0f + 8.5; // the result of 'double' type

 

5. How are the conversions that are associated with the bool type?

If the expression contains an integer type, then the values of type bool are automatically converted to integers 0 and 1. The value 0 corresponds to the value false. The value 1 or non-zero value corresponds to true.

Example. A code snippet that demonstrates the conversion for the bool type

bool b;
int a;

a = 0;
b = a; // b = False

a = 1;
b = a; // b = True

a = 50;
b = a; // b = True

 

6. What is the general kind of type cast operation?

The general form of the type cast operation is:

(type) expression

where

type – the type that you want to cast the result of evaluating an expression.

Examples of using a type cast operation.

int a;
float x;

a = 5;
x = a/2; // x = 2.0
x = (float)(a/2); // x = 2.0
x = (float)a/2; // x = 2.5 - 'float' type
x = a/2.0; // x = 2.5 - 'float' type

x = (int) (8/3.0);   // x = 2
x = (float) (8/3.0); // x = 2.666667

Recommendation. In order to obtain the necessary accuracy when evaluating expressions containing integer and real values, it is always desirable to do an operation of casting the type to the type of the variable to which the value is assigned.

 

7. What are the features of the use of parentheses and “space” characters in expressions?

To improve readability, the programs use:

  • characters “space”;
  • tabs (Tab keys);
  • round brackets ( ). Round brackets also allow you to increase the priority of the operations that are placed in them. The number of round brackets does not affect the speed of evaluating the expression.

 


Related topics