Some details about C ++ [4: cycle, branch statement, relationship expression, relational operator]

xiaoxiao2021-03-05  22

5. Circulation and relationship expression

5.1 C syntax While circulating the entire FOR cycle as a statement.

5.2 The loop is only initialized once.

5.3 Perform test conditions: It can be any expression.

C will convert the calculation result to the BOOL type: non-zero converted to true, 0 converted to false. This change does not cause incompatible issues - because C will convert True / Flase to 1 / 0; convert 0 to false, non-zero conversion to TRUE where the BOOL value is required.

5.4 for loops is the entry condition cycle - the value of the test expression is calculated before entering each cycle.

5.5 Coding habits: for and '(') plus a space; and between the function names and the brackets are not vacuum. From the visual enhancement of the difference between the control statement and function calls.

5.6 Expression: C expression is a value or combination with the operator, each C expression value.

EG: A = 3 expression value is 3; thus allow this: A = B = c = 0; // From right to left: C = 0, it is 0, so it becomes: a = b = 0; .....

The value of the x

You can use cout.setf (iOS_BASE :: BOOLALPHA); // Older version: iOS :: BOOLALPHA Set a tag, COUT will display true or false.

Expression plus semicolon ';' Both statement, EG: A B; is a legitimate statement, but useless results.

The statement removes the semicolon is not necessarily an expression, EG: INT A; is a statement, it is not an expression.

5.7 The partial declaration variable can be initialized in the for loop. For local variables, the variable will disappear after leaving the cycle.

5.8 Recommendation: Use the const value when declaring arrays and reference arrays (such as: for loops) to avoid values ​​modified.

5.9 omitting the test expression in the For loop will be considered true. For example, for (;;) {} test expression is not terminated for the TRUE program.

5.10 statement block {}. You can enclose a piece of code {} in the code. The scope of the variable declared inside is just its statement block.

5.11 Promoting Operations You can use the multiple expressions in ',' split in a FOR cycle expression

i = 20, J = 2 * I // Teasing Operators ensure that the first expression is calculated first, and then calculate one by one; the value of the overall expression is the value of the last expression. Also (17, 40) Expression The value of the formula is 40.

5.12 Highlights:

** for (CIN >> X; X == 0; CIN >> X) {} // Accept input until the value is input: 0.

*** The '==' if the write is the '=' relationship operation will change to assignment. If the value on the right side of the operator is non-zero, the value of the test expression is TRUE, and the loop will not terminate.

5.13 While cycle

*** Delay example:

#include describes clock () Function, Clock_t Type

Clock_t delay = secs * clocks_per_sec; // defined in CTIME

Clock_t start = clock (); // Get System Clock.

While (clock () - start

*** Type Alias: Clock_t As the alias of the CLOCK () return type, the compiler will convert it to long, ynsigned int, or other types of suitable systems.

Establishment method:

I: Using the pre-processor. #define clock_t unsigned int

II: Create an alias with keyword TypeDef: Become alias declare the type of type, then add TypedEf in front. Difference: TypeDef does not create a new type, just build a new name for existing types. Preprocessing uses replacement Method, there is the following shortcomings:

EG: #define f_p float *

F_P PA, PB;

The compiler replaces it: float * pa, pb; // Pb is not as Float * if you wish, but just a float type.

5.14 Do While cycle, cycle for the export condition.

5.15 Text input.

*** cin. The input to the CIN is buffered, and only the contents of its input are sent to the program after pressing the Enter key. And CIN will ignore the blank character.

*** cin.get (CH) input is still buffered, but receives each character. The input character will be assigned to CH. CH must be a char type, not Int

CIN.GET (CH) parameters are referenced: Char &, so you can modify CH, otherwise the parameters need to be incomed to & ch.

# * # cin.get (CH) Returns the iStream class object when character input is used, and converted to Bool Type True; when the end of the file, the function returns the ISTREAM class object and converts it to the Bool type false.

# * # ion ch = cin.get () Returns a character encoding of the int type when the character input is used; the function returns an EOF when the file tail is reached, and most of the system is defined as '-1'.

EG: while ((ch = cin.get ())! = EOF) {} whie (cin.get (ch)) {}

CIN.PUT (CH); // CH If the int type needs to be converted to a CHAR type.

6. Branch statements and logical operators

6.1 tidbits

* if (3 == Num) ... // If the error is 3 = NUM, the compiler will report an error, and some smart compilers recognize the errors of IF (Num = 3).

* (17

* Bool A, B; A && B, if A is False, the expression A && B will not judge the value of B.

* and, or, not in C for reserved words; but in the C language, non-reserved words, you want to add ahead file:

6.2 CCTYPE Character Library Universal for all character encodes.

Some functions: isalpha9char) Non-char Returns 0. ISPUNCT (char) Enter the punctuation to return TRUE.

Isspace (), ISDIGITS () ......

6.3 Switch statement tags are integer constant expressions.

6.4 Digital input.

EG: int Apple [10]; char CH;

While (ch! = 'q' && cin >> Apple [i] Returns the ISTREAM class object if you enter a non-number, and convert to BOOL FALSE.

While (! (cin >> apple [i]))))

{

Cin.clear (); // If you enter non-numbers, CIN returns false and set the outage, the input is blocked. Enter the loop, recover input with cin.clear ()

While (cin.get ()! = '/ n') Continue; // Because the input non-figures remain in the input queue, they are cleared, they end with '/ n'

}

You are welcome to make criticism and recommendations.

转载请注明原文地址:https://www.9cbs.com/read-34715.html

New Post(0)