C Primer Plus (7)

Source: Internet
Author: User

7.1 if statement

An if statement is called a branch statement or a selection statement because it provides a junction where the program needs to select one of the two branches to move forward. The general form is as follows:
If (expression)
Statement
If expression is true, execute statement. Otherwise, Skip.

The general form of the if else statement is:
If (expression)
Statement1
Else
Statement2
If expression is true, execute statement1; otherwise, execute statement2.
To have multiple statements between if and else, you must use curly brackets to create a code block.

7.1.1 geichar () and putchar ()

The getchar () function has no parameter. It returns the next character from the input device. Ch = getchar () has the same effect as scanf ("% c", & ch.
The putchar () function prints its parameters. putchar () and printf ("% c", ch) have the same effect.
Because these functions only process characters, they are faster and more concise than the more common scanf () and printf () functions. Similarly, they do not need format specifiers. They only apply to characters.

7.1.2 else

The rule is that if no curly brackets are specified, else matches the nearest if.

7.2 obtain Logic

! The operator has a high priority. It is higher than multiplication, and has the same priority as incremental operators, second only to parentheses. & The operator has a higher priority than |. The priority of both operators is lower than the relational operation but higher than the value assignment operation.

7.3 conditional operators?

Conditional operators are the only example of the ternary operators in C.
The following is a general expression of conditional expressions:
Expression1? Expression2: expression3;
If expression1 is true, the value of the entire condition expression is the same as that of expression2. Otherwise, the value of the entire condition expression is the same as that of expression3.

7.4 cycle auxiliary means: continue and break

7.4.1 continue statement

This statement can be used in three types of loops. When the sentence is run, it will cause the remaining iteration part to be ignored and start the next iteration.
Another use of continue is as a placeholder. For example
While (getchar ()! = '\ N ')
Continue;

7.4.2 break statement

The break statement in the loop causes the program to terminate the loop containing it and carry out the next stage of the program.
If the break statement is in a nested loop, it only affects the loop that contains its innermost layer.

7.5 multiple options: switch and break

The switch can use multiple case labels for a given statement.
The program control jumps to the corresponding case tag according to the expression value, and then the program flow continues through all the remaining statements until the break statement is redirected again.
Expression and case labels must both be integer values (including char type), and the label must be a constant or an expression completely composed of constants.
If there is no case tag that matches the expression value, control the statement that locates the tag as default.

7.6 goto statement

The goto statement consists of two parts: goto and a tag name.
Format:
Goto label;
.
.
Label: statement

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.