Chapter 5 loop and relational expressions

Source: Internet
Author: User
Chapter 5 Use of loops and relational expressions 5.1 for loops 5.1.1 For Loops

The components of the for loop are as follows:

1) set the initial value;

2) execute the test to check whether the loop should continue to run;

3) execution cycle body;

4) Update the value used for testing.

The form of a for loop is as follows:

    for (init-expresstion; test-expresstion; update-expresstion)        body-statement

Note:

1) C ++ regards the entire for as a statement, although the loop body can contain one or more statements.

2) The for loop only executes the initialization expression once. The initialization expression can be null. If it is null, it is better to write null or 0. This means that not the programmer forgets to write the initialization statement.

3) The test-expression can use any type of expression. c ++ forcibly converts the result to the bool type. The value 0 indicates false, and the value non-0 indicates true. Test-expression can be null. If it is null, it is equivalent to true. If it is null, it is better to write true or 1. This means that not the programmer forgets to write the initialization statement.

4) A for loop is an entry-condition loop. The value of the test expression is calculated before each loop. Update-expression is executed at the end of each loop. Similarly, update-expression can be null. If it is null, it is better to write null or 0, which means that not the programmer forgets to write the initialization statement.

5) The common method of C ++ is to add a space between for and parentheses, while is similar to if. Do not use spaces between functions and parentheses.

5.1.2 for loop with variable Declaration

C ++ adds a feature to for Based on C. You can declare the variable in init-expresstion of. In this case, the for loop can be expressed:

    for (init-statement test - expresstion; update - expresstion)        body - statement

5.1.3 side effects and Sequence

Side effect (side effect) indicates that some variables are modified during expression calculation. A sequence point can be considered as a semicolon. The order point means that before processing the next statement, all the modifications made by the value assignment operator, increment operator, and decrease operator must be completed. The end of any complete expression is a sequence point.

A complete expression is a subexpression that is not a larger expression.

For example:

    y = (4 + x++) + (6 + x++);

4 + x ++ is not a complete expression. c ++ does not guarantee that the value of X is 4 + x ++ followed by 1. This statement is a complete expression. c ++ only guarantees that X is incremented twice before the next statement.

5.1.4 prefix format and suffix format

Prefix formats include ++ X and suffix formats x ++. If the expression value is not used, only side effects exist. The prefix format or suffix format does not affect program behavior. However, the execution speed varies slightly.

For prefix format, add the variable value to 1 and then return the result.

For the suffix format, copy a copy of the variable, add the variable to 1, and then return the copy.

This does not seem to be a problem for built-in types and modern compilers, but for classes, prefix formats are more efficient than suffix formats. Therefore, you should develop a good habit and try to use the prefix format.

5.1.5 increment/decrease operators and pointers

When both * And ++ are used as pointers, the priority and tuberculosis of operators determine the calculation result.

Prefix increment and prefix decrease are combined from right to left with the same priority as removing the reference operator.

The priorities of increasing suffixes and decreasing suffixes are the same, but they are higher than those of increasing prefixes. They are combined from left to right. For example, the following expressions:

* ++ Pt; // first apply ++ to PT, and then * to PT after increment; (* PT) ++; // first apply * to PT, then, increment the retrieved value. * PT ++; // first, apply ++ to PT, and then * to the incremental PT;

5.1.6 compound statement (statement block) 

C ++ and C use two curly braces {} to construct a composite statement (statement block ). A code consisting of a bunch of curly braces is considered as a statement.

5.1.7 comma Operator

The comma operator allows two expression statements (which must be an expression statement but not a declaration statement) to be placed in the C ++ statement where only one expression is allowed. A comma is a sequence point. Expressions separated by commas (,) are executed from left to right. Such as-J, ++ I;

Commas are not all comma operators. In a declaration statement, a comma is used to separate adjacent variable names in the Variable list. Such as int I, J;

The comma operator is mostly used for init-expression and update-expression. This brings a lot of flexibility.

5.1.8 comparison Operators

The comparison operators of C ++ are as follows:

Operator

Description

<

Less

<=

Less than or equal

=

Equal

>

Greater

> =

Greater than or equal

! =

Not equal

Note: For C-style string comparison, use the strcmp () function. This function is included in the header file "cstring. The C ++-style string class can use comparison operators.

5.2 While Loop

A while loop can be seen as a for loop without initialization or update. While loop is also an entry condition loop. Only test conditions and body are available, in the following format:

    while (test-condition)        body-statement

First, calculate test-condition. If it is true, the statement in the loop body is executed.

Next, return and execute the test conditions.

In C ++, The for loop and the while loop are essentially the same. For example

    for (init-expresstion; test-expresstion; update-expresstion)        body-statement

Can be converted

    init-expresstion;    while (test-condition){        body-statement    update-expresstion;    }

5.3 do-while loop

The do-while loop is an exit condition loop. The format is as follows:

    do        body-statement    while (test-condition);

Do-while loop: Execute the loop body first and execute the test conditions. Note that the parentheses after while must be followed by a plus sign.

Generally, the entry condition loop is better than the exit condition loop. The do-while loop is used to obtain user input.

5.4 range-based for loop (C ++ 11)

A range-based for loop simplifies common loops for arrays (or container classes, such as vector and array ). See the following example:

// Use the range-based for loop Double V [4] = {1.0, 2.0} For the array; For (double D: V) {cout <v <Endl ;}

If you want to modify the array content, you need to use the reference. The reference will be explained later. Here we will look at the example:

// Modify the array content by referencing Double V [4] = {1.0, 2.0}; For (double & D: V) {D * = 0.8 ;}

The array here can also be the initialization list:

// Use the initialization list for (double D: {1.0, 2.0, 3.0}) {cout <D <Endl ;}

5.5 two-dimensional array

C ++ does not provide a two-dimensional array, but you can create an array for each element. For example, int matrix [4] [5]; a 4 × 5 matrix is created. A two-dimensional array is a continuous space in the content and is continuously stored in multiple one-dimensional arrays.

Two-dimensional array initialization:

Int matrix [2] [2] = {1, 2}, {2, 2}; // initialization list

Chapter 5 loop and relational expressions

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.