c++--Study notes 009

Source: Internet
Author: User

Logical operators are often used in conjunction with relational operators.

The order problem between operators

The correct result of an expression can be obtained only in the correct order of calculation

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/97/wKiom1XnWAbSuccKAAESZ3pIrsc271.jpg "title=" capture. PNG "alt=" Wkiom1xnwabsucckaaesz3pirsc271.jpg "/>

Operators with high precedence are evaluated first, and the peers are left-to-right.

Experience: Reduce the mix of multiple operators in an expression, keeping the expression short and short.

Use () to change the precedence of operations in an expression.

The statement emphasizes the completed function, and the expression concerns the operation and the final result that it describes. An expression with a semicolon can directly form a statement.

Variable definition statement;

Assignment statement;

Variable definition statements

int A,b,c

Assignment statements

a=20;

c=2* (A+B);

When multiple consecutive statements belong to the same control range, you can enclose this statement with a pair of curly braces {}:

{

int a=100;

BOOL b=a>20;

}

The content inside the curly braces, called the compound statement

Conditional selection Statement

If statement syntax format:

if (conditional expression)

{

Statement 1;

}

Else

{

Statement 2;

}

In a conditional selection statement, the value of the conditional expression is evaluated first, and then judged by the expression. If the expression value is true, statement 1 is executed, otherwise statement 2 is executed.

If statements we can omit the contents of the Else section without concern.

if (nscore>80)

{

cout<< "good!";

}

If the value of Nscore is greater than 80, the output good! , less than or equal to 80, we do not care, do not handle.

If conditional statements can be nested

cout<< "Please enter two integers:" <<endl;

int v1,v2;

cin>>v1>>v2;

if (V1!=V2)

{

if (V1>V2)

{

cout<< "V1>v2" <<endl;

}

Else

{

cout<< "V1<v2" <<endl;

}

}

Else

{

cout<< "V1=v2" <<endl;

}

The IF condition statement can be tied

Parallel if conditional statement syntax format:

if (conditional expression 1)

{

Statement 1;

}

else if (conditional expression 2)

{

Statement 2;

}

else if (conditional expression N)

{Statement n;}

Else

{Statement n+1;}

A side-by-side conditional selection statement executes only one of the scores, and if more than one condition expression is true, only the first branch is executed.

Use a side-by-side if condition statement to avoid repeated overrides of the condition range, and do not let multiple conditional expressions be true at the same time.

Parallel SELECT switch statement

Replacing complex parallel conditional selection statements with switch statements

The switch statement syntax format is:

switch (conditional expression)

{Case constant expression 1:

{Statement 1;}

Break

Case constant Expression 2:

{Statement 2;}

Break

case constant Expression N:

{Statement n;}

Break

Default

{statement;}

}

The switch statement first evaluates the value of the conditional expression, which can only be of integer or character type. Calculates the value of the play expression, starts to look for the same value in the case, and if it is equal to the constant value in the box, executes the statement in the box until the break keyword is encountered, completing the entire switch statement.

After the program enters a branch statement, it regrets the execution of all subsequent branch statements until the BREAK keyword is encountered.

In a switch statement, the values of each constant expression cannot be the same, i.e. two case branches of the same condition cannot appear.


Loop control Statements

There are 3 kinds

While loop

The syntax format is:

while (conditional expression)

{loop body statement;}

If the value of the conditional expression in the while loop is true, the loop resumes, and false ends the loop.

As long as the condition is established, the loop body statement is executed continuously. Ends the while loop as long as the condition is not satisfied.

int _tmain (int argc,_tchar* argv[])

{cout<< "================== monthly revenue Peer program ===========" <<endl;

cout<< "Please enter your income for this month (positive) and indicate (negative number), 0 means end of input. "<<endl;

int ninput=1;

int ntotal=0;

while (0!=ninput)

{cout<< "Please enter your income or expenditure:";

cint>>ninput;

Ntotal+=ninput;

}

cout<< "Your balance this month is:" <<nTotla<<endl;

return 0;

}

While the twin brother, do ... while

Do ... while syntax format:

Do

{Loop statement}

while (conditional expression);

The loop body statement is executed before the value of the conditional expression is judged.

The essential difference from a while loop statement is that Do...while executes a loop body statement first.

Execute at least once.

It is more appropriate to use the DO...WHILE loop structure in cases where the cycle is at least once done.

For loop more powerful

The syntax format for the For loop control structure is:

20150502

A For loop expresses a loop within a range

For loop syntax format:

for (initialization statement; conditional expression; Change statement)

{Loop statement;

}

Any cycle has an opportunity to end, otherwise it is a dead loop.

A conditional expression is a chance to give a loop an end.

When you use a conditional expression, make sure that the conditional expression has a value of false at the end of the loop

If the loop executes regardless of how the value of the conditional expression is true, a dead loop is formed.

Assigning a user-entered value to Ninput is a modification to the loop control variable. ---change statements

Workflow for A For loop:

Enter the For loop, execute the initialization statement first, complete the initialization, and then evaluate the value of the conditional expression, execute the Loop body statement, execute the change statement, and modify the loop control variable if the value of the conditional expression is true.

The value of the conditional expression is then evaluated, depending on its value, whether the next loop is required or not, and if the value of the conditional expression is true, the next loop is resumed, and the entire loop is ended instead.

int ntotal=0;

for (int i=1;i<=100;++i)

{ntotal+=1;}

cout<< "from 1 to 100 for all integers" <<nTotal<<endl;

Use a For loop when you need to describe a loop scene for a particular loop range.

The break keyword is used to jump out of a loop under certain conditions. The whole loop is jumping out.

The Continue keyword is used under certain conditions to determine whether the condition satisfies the next cycle after the end of the loop body. Jump out of this cycle, continue the next wash

Expressions and a variety of statements---words and sentences used in writing.

When the program is complex, you can set breakpoints and step through the analysis program.

Select a structure that determines the path that the program executes according to the criteria. Select the structure to make decisions based on different conditions, choose different execution paths, and realize different functions.

The loop structure is mainly used in statements that need to be executed repeatedly, and the functions completed by these statements need to be implemented repeatedly.

Program control mode: Sequential structure, selection structure, cyclic structure


c++--Study notes 009

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.