C Primer Plus (6)

Source: Internet
Author: User

6.1.1 program Annotation

Condition for loop determination: status = 1 =: equal operator.
Scanf () returns the number of successfully read projects. If no integer is read, 0 is returned;

Standard cycle format:
Get first value to be tested
While the test is successful
Process value
Get next value

6.1.2 C-style read Loop

Status = scanf ("% ld", & num); while (status = 1) You can use while (scanf ("% ld", & num) = 1).
The syntax feature of C allows you to use a simplified version.
While getting and testing the value succeeds
Process the value

The general form of the while loop:
While (expression)
Statement
Statement can be a simple statement with a semicolon or a compound statement in curly brackets.

6.2.1 terminate the while LOOP

A while LOOP is a conditional loop with entry conditions. Statement execution depends on the conditions in the judgment expression.
When you construct a while loop, the loop must contain values that can change the judgment Expression to make the expression value false.
It is determined whether to terminate the loop or continue execution only when the value of the judgment condition is calculated.

If you want to skip the input until the first character is not a space or number, you can use this loop:
While (scanf ("% d", & sum) = 1)
;
Place the null statement in the next line to remind the user to intentionally place it there.

Relational operators: <, <=, ==, >=,> ,! =.
Only <and> can be used in floating point comparison. The reason is that the rounding error may cause two logically equal numbers to be different.
The fabs () function declared in the math. h header file can be used to conveniently judge floating point numbers. She returns the absolute value of a floating point number.

6.3.1 problems with true values

For C, the value of a true expression is 1, and the value of a false expression is 0.
Generally, all non-zero values are considered true, and only 0 values are considered false.
For example, while (goats! =) Can be replaced by while (goats.
If one of the two sides of the comparison is a constant, you can place it on the left of the comparison expression to help you find errors.
For example, 5 = canoes;
The _ Bool type is added to C99. A Bool variable can only have a value of 1 or 0.

6.3.2 priority of Relational operators

Relational operators have a lower priority than Arithmetic Operators but higher than value assignment operators.

High-priority group: <=>>=
Low-priority group: =! = Link operators are combined from left to right.

 

The first expression is initialized and executed once at the beginning of the for loop. It does not have to initialize a variable, or it can be a certain type of printf () statement.
The second expression is a judgment condition, which must be evaluated before each execution of a loop.
The third expression changes and is calculated at the end of each loop.
X * = 3 * y + 12 equals x = x * (3 * y + 12)

 

First, it ensures that the separated expressions are calculated in the order from left to right.
Use a comma as the sequence point to ensure that the side effects of the expression on the Left take effect before the subexpression on the right is calculated.
Again, a comma can also be used as a separator, for example, in a declaration statement.

Do
Statement
While (expression );
The do while Loop itself is a statement, so it requires an end semicolon.
The loop must be executed at least once. The for or while loop can be executed at one time.

An array is a series of values of the same type stored linearly.
For execution speed consideration, C does not check whether you have used the correct subscript.
If the character array contains an empty character \ 0, the content of the character array constitutes a string.
The number used to identify an array element is called a subscript, index, or offset.
Elements in the array are stored sequentially in the memory.
C99 allows you to use a constant value to specify the array size, but C90 does not. You can use the # define command to create an obvious constant of the specified array size. # define can be used in C90C99.

Q1: Assuming that value is an int type value, what is the output of the following loop?
For (value = 36; value> 0; value/= 2)
Printf ("% 3d", value );
A1: 36 18 9 4 2 1
Q2: If the value is a double value instead of an int value, what is the problem?
A2: When the value is smaller than 1, the judgment condition will remain true. The loop continues until 0 is generated due to floating point numbers. The % 3d specifier is incorrect.

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.