004-Process Control and type conversion, 004-process Conversion

Source: Internet
Author: User

004-Process Control and type conversion, 004-process Conversion

Process Control

Sequential structure, selection structure, and cyclic structure

1. Ordered Structure

The program is executed sequentially by default.

2. Select a structure

If SELECT statement

First case

If (condition) // if the condition is true, execute the following statement.

{

// Statement

}

Case 2

If (condition) // if the condition is true, execute the following statement. Otherwise, execute the statements in else.

{

// Statement

}

Else

{

// Statement

}

Case 3

If (condition 1) // if condition 1 is true, execute the following statement; otherwise, execute the statements in the following else if

{

// Statement 1

}

Else if (condition 2) // condition 2 is true. Execute the following statement. Otherwise, execute the statements in else if

{

// Statement 2

}

Else if (Condition 3) // Condition 3 is true. Execute the following statement. Otherwise, execute the statements in else.

{

// Statement 3

}

Else // if the preceding statement is not executed, this

{

// Statement 4

}

Switch Structure

Switch (expression) // when the expression execution result meets the following conditions, the default statement is executed.

{

Case value 1: Statement 1

Break;

Case value 2: Statement 2

Break;

Case value 3: Statement 3

Break;

Default: Statement 4

Break;

}

 

3. Loop Structure

While Loop

Do while LOOP

For Loop

  1. while Loop

While (condition) // judgment condition. If the condition is true, the statement is executed. If the condition is true, the statement is executed. If the condition is true, the loop ends.

{

Statement;

}

 

Difference between continue and break

Continue ends this cycle and enters the next cycle.

Break ends the entire loop.

2. do while LOOP

Do

{

Statement;

} While (condition); // first execute the statement to determine the condition, at least once

3. for Loop

For (Statement 1; condition; Statement 2)

{

Loop body;

}

Statement 1: Execute once and initialize the statement

Statement 2: Incremental statement. Execute the loop body after the condition is determined, then execute Statement 2 to determine the condition again.

Note: loops can be nested.

For (;;)

{

For (;;)

{

While ()

{}

}

}

 

 

 

Use of static local variables

Static local variables share the global data zone with global variables, but static local variables are only visible in the functions that define them. Static local variables and local variables are stored in different locations, so that they have different time limits, resulting in different running results for these two operations.

 

Note:
(1) Static local variables are allocated to storage units in the static storage area. The program is not released during the entire running period. Automatic variables (that is, dynamic local variables) belong to the dynamic storage class. They are stored in the dynamic storage space (rather than the static storage space) and are released after function calls.
(2) assigning an initial value to a static local variable is performed at compilation, that is, assigning the initial value only once and having the initial value when the program is running. In the future, the initial values will not be re-assigned for each function call, but the values at the end of the last function call will be retained. However, assigning an initial value for an automatic variable is not performed during compilation, but during function calling. Every time the distinct function is called, the initial value is re-assigned, which is equivalent to executing a value assignment statement.

(3) If initial values are not assigned when defining local variables, the initial values 0 (For numeric variables) or null characters (For numeric variables) are automatically assigned to static local variables during compilation ). For automatic variables, if the initial value is not assigned, its value is an uncertain value. This is because the storage unit has been released after each function call, and the storage unit is re-allocated in the next call. The value in the allocated unit is uncertain.
(4) Although static local variables still exist after the function call, other functions cannot reference them, that is, they are "invisible" in other functions.

 

 

About type conversion (as mentioned earlier)

Int and float/double

When you convert a floating point (single-and double-precision) to an integer, the fractional part of the floating point is discarded and only the integer part is retained.

Assign the integer value to the floating point variable. The value remains unchanged. Only the floating point type is changed to the floating point type. That is, the decimal point is followed by several zeros.

Float and double

Float Type data only adds 0 at the end to be extended to doub1e type data for calculation, and then directly assigns a value.

When data of doub1e type is converted to float type, it is achieved by the number of truncated tails. Rounding is required before truncation.

Char and int

When an int value is assigned to a char variable, only the minimum 8 bits are retained.

Convert the char type to the int type and directly obtain the internal values of the char.

Char c = 'a'; // the ascii value of character a is 97.

Int n = c; // Therefore, n is 97.

 

Automatic type conversion

Int a = 10.55 + 10; // The 10.55 type is automatically converted here

Forced type conversion

Forced type conversion is implemented through the type conversion operation. The general form is (type specifier) (expression). Its function is to forcibly convert the operation result of an expression to the type represented by the type specifier.

Int a = (int) 10.55 + 10; // here is the forced type conversion

To be continued ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.