C Language Basics 3

Source: Internet
Author: User
Tags case statement

Design of selection structure

1.if statement:

The IF statement is the simplest and most intuitive way to implement a selection statement. His simple form is to determine if a condition value is true and, if true, to execute a piece of code.

if (expression) {

Statement 1

}

Function: Evaluates the value of the expression and, if true, executes statement 1; otherwise skips statement 1 executes the next statement of the IF statement.

Description: The expression in parentheses controls the condition in which the value of the expression is nonzero to true, and 0 is false.

2.if_else statement:

if (expression) {

Statement 1

}else{

Statement 2

}

Evaluates the value of the expression, if the value of the expression is true, executes statement 1, and skips statement 2, continuing to execute the next statement of the IF_ELSE statement;

If the value of the expression is false, skip statement 1, execute Statement 2, and proceed to the next statement of the If_else statement.

3. Nested IF statements:

A.If (expression 1) {

if (expression 2) {

Statement 1

}else{Statement 2}

}else{Statement 3}

b.if (expression 1) {

if (expression 2) {

Statement 1

}

}else{Statement 2}

C.if (expression 1) {

Statement 1

}else if (expression 2) {

}else{

}

4.switch statement:

switch (expression) {

case constant Expression 1: statement 1

Case constant Expression 2: statement 2

Case constant Expression 3: statement 3

....

case constant Expression N: statement n

Default: Statement n+1

}

The value of the expression is evaluated first, followed by the constant expression, and, if equal to the constant expression I, executes from the expression I until the switch statement ends.

If all constant expressions are not equal, execution starts from default.

Description

1). Switch is followed by a constant or constant expression or an expression that evaluates to a result, with its integer part compared to each constant expression.

2). A constant expression cannot have a variable, and the type must be an integer character or an enumerated type, and each constant expression is different.

3). The default statement usually appears after the case statement, or it can be between.

5.break statement:

Break

Terminates execution of the switch statement or Loop statement in which it resides.

6.goto statement: (strongly discouraged, he destroys the logical order of the program)

C Language Basics 3

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.