C Language Program Structure branch structure if switch

Source: Internet
Author: User

One, the structure of the program: Sequential structure: The program runs in accordance with the normal process of running (the program inside the implementation of linear structure: stack, Team

column, linked list, array of personal feelings)

Branching structure: Conditional statements if switch

Loop structure: While do and for

Two, if statement

1, Category 1) The first form of:

Composition: if (expression) {

statement block;

}

2) Form of If....else

Usage: If the value of an expression is true (1) the statement block 1 is executed

If the value of the expression is False (0) to execute the statement block 2

Format: if (expression)

{

If the value of an expression is true (1), this line of code blocks

Statement Block 1;

}

Else

{

If the expression value is False (0), the contents of the else brace are executed

Statement Block 2;

}

3) nested use of If...else

if (condition 1) {

Statement Block 1;

if (condition 2) {//condition 1 is True

}

}else{

}

4) If ... else if...else

if (condition 1) {

Statement Block 1

}else if (condition 2) {

}else if (condition 3) {

}

else{

}

2, note: After the IF statement whether or not the following is an empty statement or a sentence plus {} (to prevent errors)

1) if (); A ";" can be written directly after the IF statement. (“;” Empty statement), indicating that nothing is done

2) When the IF statement is true, you can omit the parentheses when you simply execute a sentence

3) if (1) or if (-1) are permanent, if (0) Permanent

4) The problem of the scope disorder

if (a>0) int num=-1; Num variable will error, can be added {}

printf ("%d\n", num);

5) If the problem of omitting curly braces

Method: From bottom to top find else according to else find can match if

6) return keyword within the function, the end of the program;

7) A leap year must be interpreted in years that are divisible by 4 and not divisible by 100, or that the year is divisible by 400;

Three, switch statement

1) switch statement a branch statement

Format: 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

are not satisfied with the execution of the statement;

}

2) Attention Issues

1>case: In order to prevent penetration, we use break;break, we can end the Swith statement prematurely

If the case ends without a break in each condition, the program continues execution until the break ends;

The return value type of the 2>switch (expression) expression cannot be float\double, can be integer, and character type

The case is also a constant expression;

3> If you define a variable, you need to enclose it in curly braces.

int a=3;

Switch (a+3) {

default:printf ("error\n");

Case 6:

printf ("xxx");

int num =-1;

Break

}

C Language Program Structure branch structure if switch

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.