C ++ Primer Plus Chapter V

5.1 for loop

This much said, and nothing more difficult

The only different is that C ++ allows follows

for (int i = 0;i > 5;i++)

Can declare variables in the loop initialization section, but after the release of the loop variables

The while loop is like Python with the

do while loop is executed before the contents do while loop is then determined, as follows

{
    using namespace std;
    int n;
    
    cout << "Enter numbers in the range 1-10 to find";
    cout << "my favorite number \n";
    do
    {
        con >> n;
    }while(n!=7);
    cout << "Yes, 7 is mt favorite. \n";
    return 0;
}

5.4 based on the range for the cycle c ++ 11

c ++ 11 based on the range for the new cycle, as follows

    double prices[5] = {4.99,10.99,6.87,7.99,8.49};
    for (double x : prices)
        cour << x << std::endl;
    
    for (double &x : prices)
    x = x * 0.80; //这种利用&来声明,可以对内容进行修改,上边的就不可以


for (int x : {3,4,5,6,7})
    cour << x << std::endl; //当然,也是可以在初始化语句中初始化x

to sum up:

C ++ provides three loop: for loops, while loops, do while loop, if the loop test condition is true or non-zero, then the loop repeats a set of instructions, if the test condition is false or 0, the end loops, for loops and all the while loop circulation inlet conditions, which means that the program will be executed before the loop body statement to check the test conditions, do while loop is a loop exit conditions, which means that it will check the condition after executing the loop body statement.

Each syntactic cycle requires a loop statements made, however, this statement may be a compound statement, it may be a block of statements (statement played by the plurality of curly brackets)

Relationship between the expression of two values, often used as cycle test conditions, by using the relational expressions is one of the six relational operators consisting of: <, <=, ==,> =,> or =,! results relational expression type bool value of true or false

Many programs are read byte by byte text file or text input, the istream class provides several ways to do this job, the next character is read if a char variable ch, the following statement will be input into the ch in:

cin >> ch;

However, it ignores spaces, line breaks and tabs, the following member function call reads the input of the next character (regardless of what the character is) and stores it in ch:

cin.get(ch);

Member function calls cin.get () Returns the next input characters - including spaces, line breaks and tabs, so you can use it like this:

ch = cin.get();

cin.get (char) member function calls has been reached EOF, while cin.get () member function calls by returning value to indicate EOF has been reached EOF, EOF is defined in the file iostream returned by converting to bool value false to indicate of.

Loop is nested loop cycles, suitable for processing two-dimensional array.

Guess you like

Origin blog.csdn.net/u013693952/article/details/90726898