In the C language, the sentence sequence is selected.

Source: Internet
Author: User

C language Five, the sentence sequence loop selection. A concise understanding of the statement

We know that the code is executed sequentially when writing a C language program.

Execute from the top down.

But we can control the process.

Before we control, we need to be familiar with what statements are.

Compared to people who have studied Chinese knowledge, a sentence is known to end with a period.

So is the high-level code. It's just not the end of the sentence but the semicolon.

For example:

  

int main () {    int a = ten;  A statement    return 0;}

  

II. selection Structure

We said that C is a sequential structure to execute a language, so we can control the statements it executes.

c keyword If, if is the meaning.

such as pseudo-code:

  

if (true) {   print 1  }if (false) {   print two}

The contents of the brackets we give to the true and false, corresponding to the high-level language is real (true) and False (false)

Of course in C, the non-0 is true, meaning that if it is not 0, then it is true. If 0 is false.

There is also else, and else is that if you do not do it, then execute mine.

The int a = 3;if (a-3)   result is 0, and if 0 is false, then the Else branch is executed. {   printf (1);  } else{  printf (2):}

Of course, there are else if otherwise if the meaning

if (a-3) {   printf (1);} else if (a-4)  If the above does not execute, then judge me the result below. {   printf (2);  } else{   printf (3);}
Three, cycle structure

Sometimes, it is possible for our program to operate on one data multiple times. So we're going to use the loop structure.

Loop so sure to give a number of times. or a condition. To quit.

For example, the 0-100 and below.

int main () {    int count = 0;    int i = +;    while (i)          condition, give true or False     {        count + = i;   Add 100 for the first time and then decrement        i = i-1;    Each decrement and then add next    }    printf ("%d\r\n", count);        System ("pause");    return 0;}

Where the while condition, given is I, that is, each time-1, until 0, then the while is not executed.

For Loop loop structure

for (initialize variable; condition; control) {    .... EXECUTE statement}

Use for to find the value of 0-100.

for (int i = +; i > 0; i--)    {        count = count + i;    }

Where our I variable is defined internally, we judge the condition is i > 0 to execute the statement, then each time I--, that is 1, the result is finally 0.

Do While statement.

The Do While statement is the same as while, except that the do while executes the row side first. Don't care about the condition.

For example:

  

Do    {        int a = ten;        printf ("%d\r\n", a);    } while (0);

As you can see, the condition itself is false, then in the program, it will output 101 times.

In the C language, the sentence sequence is selected.

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.