Simple learning C -- the third day and the third day

Source: Internet
Author: User

Simple learning C -- the third day and the third day

Control Structure (2)

As we have learned before, the judgment structure in the control structure, of course, the switch statement is also the judgment statement. However, if... else... you will, I believe you will, too, switch statements. Their functions are similar, but the switch statement will make the program more concise in some cases. Please read this syntax by yourself.

Next, go to the topic.

2. Loop statements.

In C language, there are two types of loop statements: 1): while loop. 2): for Loop.

Basic syntax format:

1): while LOOP

// The first one is: while () // It is a judgment statement in parentheses. If it is false, the content in the braces is not executed {printf ("...... ");}
// Second: do // first execute the following statement {printf ("...... ");} while (); // note that there is a semicolon in the brackets, which is a judgment statement. If it is false, the content in the braces will not be executed.

What is the difference between the first and the second? Yes. First, remember that the first class will run the class content in braces at least 0 times, and the second class will run the class content in braces at least once. Which will be explained below.

2): for Loop

for(   1  ; 2   ;  3  )  {      4  }

Because the for loop is a little more complex than the while loop, it is necessary to explain it carefully. First, define the running sequence of the syntax in the program.

1-> 2-> 4-> 3-> 2-> 4-> 3-> 2 ...... if it does not end, it will continue to run cyclically. When will it end? When the statement in Statement 3 is false, the for loop ends. It may be a bit abstract now. I will use the actual example below to explain the usage of the loop structure.

Is there a computer with a compiler running next to you?

If so, let's get started.

A simple question: Enter the number of one hundred in the keyboard, and then output the sum of all numbers from 1 to 100.

Of course, if you have added two numbers (if you have carefully read the previous one), you can think of a stupid way. Its program is as follows:

# Include <stdio. h> int main () {int a; // enter 100 scanf ("% d", & a) from the keyboard; // can the omitted content be supplemented? I don't want to do this tired thing printf ("% d", 1 + 2 + 3 + 4 +... + 100 );}

If you write the program that I did not complete, I can only tell you that you are too silly and naive. So how can this operation be simplified? The advantage of the machine is that the machine is good at doing a lot of repetitive operations. Of course, the computer is also like this. Repetitive operations are both loops. Let's take a good look at the following code to see how it turns into a cyclical problem.

 

# Include <stdio. h> int main () {int a, I, sum = 0; // enter 100 scanf ("% d", & a) from the keyboard ); /* The initial value of I is 1. Each time I add one, I ++ differs from ++ I. I <= a is similar to if () the judgment in parentheses. If I <= a, the operation in braces will be executed, otherwise the loop will jump out */for (I = 1; I <= a; I ++) {sum = sum + I; // sum adds an I value each time, each time I add one //, the sum of the first I positive integers is realized.} printf ("% d \ n", sum); // "\ n" is a line break, not mentioned before}

If you understand the above program, continue with it.

For each for () loop, it can be converted to a while () loop. for each while () loop, it can be converted to a for () loop. the following uses the while () loop to implement the functions of the above program

# Include <stdio. h> int main () {int a, I, sum = 0; // enter 100 scanf ("% d", & a); I = 1; // I value from the beginning while (I <= a) // If I <= a, execute the operation {sum = sum + I; I ++; // I auto-increment one} printf ("% d \ n", sum); // "\ n" is a line break, not mentioned before}

Of course, there can be many ways to implement it in for () and while (), but the principle will never change. It is a process of implementing a "accumulators", for example:

// Content in the for () loop can be placed in appropriate content // to implement the same function I = 1for (;) {sum = sum + I; I ++; if (I = a + 1) break ;}
// The same while () loop can also be I = 1 while (1) // 1 represents the truth in the computer, that is, the while loop is always // true, that is, it will continue to loop, {sum = sum + I; I ++; // when the value of I is equal to a + 1, you need to break this cycle if (I = a + 1) break ;}

In the above program, the break statement appears, which means to jump out/break this loop. in C, there is a contiue; Statement, which means that the following statement is no longer executed and the next loop in this loop statement continues. the above two must be used in the loop. the goto statement is more practical.

 

Running result:

 

Of course, you can understand the difference between do... while; and while loop mentioned above. If you cannot understand it, try writing code by yourself.

If the above program can understand it, the cycle should not be too large for you. Although this is a basic foundation, it will not be a permanent change.

Well, let's calculate the first n positive integers. The sum of all the even numbers.

It is very simple, that is, let I start from 0 and add 2 each time when I is not greater than a. The result is the sum of all even numbers.

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.