Talk about those things about cycles

Everyone deserves at least one opportunity to stand up and applaud in their lives, because we have all beaten the world. —RJ Palacio "Miracle Boy"

Introduction: The Greek philosopher Zeno once said, "Motion is impossible. Since a moving object must reach a point halfway before reaching its destination, if the space is assumed to be infinitely separable, the finite distance includes infinite points, so moving objects Will pass infinitely many points in a limited time." This is the paradox of dichotomy proposed by Zeno. He also proposed that the arrow would never reach its target. He believes that the arrow must reach half of the target distance, then half of the remaining distance, and then half of the remaining distance in turn, so that there is endless. The time used is also endless. Of course, it seems that Zeno can only come to this conclusion if he is willing to be a target.

Then we can write a program to prove that, suppose that the first time the arrow completes half of the distance is 1 second, then half of the remaining distance is completed every 1/2 second, and then the remaining distance is completed in 1/4 second. Half... Then we can use the following formula to express the total time:

1+1/2+1/4+1/8+…

Are there many expressions added here, so how do we write these expressions into the program?

Here we need to ask three bosses to control the situation. Why are there three? Did we mention the boss in the array, there is only one boss, why there are three here? Although there is only one boss, there are many younger brothers, but here, there are only three polished commanders

Let me introduce these three polished commanders one by one (While, do while, for)

NO.1
is our while first, which is an English word that stands alone. While statement is also called While loop. While in the loop is the entry condition loop, the program must get the input data and check the value in the condition before entering the loop body.

1. The general form of the while loop is as follows:

while (expression) {

Loop body; }//loop body can also be an empty statement

Note: When the expression is true, execute the following loop body; after the loop body is executed, judge whether the expression is true, if it is true, execute the following statement again; then judge whether the expression is true... It keeps looping in this way, until the expression is false and out of the loop. This is the order of execution of while.

2. Terminate the while loop:

The while loop is very important: when constructing the while loop, the value of the test expression must be changed, and the expression must eventually be false, otherwise the loop is an infinite loop.

It is also clear that only when the test condition is evaluated, the decision is made to terminate or continue the loop.

3.while: entry condition loop (also for loop statement)

The while loop is a conditional loop that uses entry conditions. What is "conditional"? It actually means that the execution of the statement part depends on the condition described by the test expression. The expression is an entry condition, because the condition must be met to enter the loop body. If the conditions are not met, it will not enter the loop body.

When writing a while statement, the expression in parentheses is usually a judgment statement, such as:
status==1;//Judging whether the status is 1
status=1;//Assigning 1 to status

It should be noted that == is different from =. == is the equality operator of C. The above expression is to determine whether the status is 1. And = is to assign 1 to status.

Note:

When a beginner learns programming, no matter how many lines there are in the execution statement after for, while, do while, even if there is only one line, add "{}" to develop good programming habits.

We usually use pseudo-code when typing code. Pseudo-code is a way of expressing program ideas in simple sentences. It corresponds to the form of the computer. It helps us to clarify the logic of the design program. The advantage of using pseudo-code is that we can focus on the organization and logic of the program, and we don’t need to think about how to express our ideas in programming languages ​​for the time being.

Look at Wikipedia and say pseudo code:

Pseudocode (English: pseudocode), also known as virtual code, is a high-level method of describing algorithms. It is not a real programming language (a language similar to pseudo-code has appeared, see Nuva); it may use the syntax, reserved words of multiple programming languages, and even natural language.

It specifies the function of the algorithm in the written form of a programming language. Compared to programming languages ​​(such as Java, C++, C, Delphi, etc.) it is more similar to natural languages. It is a semi-formal, non-standard language. We can describe the structure of the entire algorithm operation process in a form close to natural language (here we can use any kind of text that the author is familiar with, such as Chinese and English, with the focus on expressing the meaning of the program). Using pseudo-code can help us better express the algorithm without being stuck on the specific implementation.

When people implement the same algorithm in different programming languages, they realize that the implementation (not the function) they make is very different. It may be very difficult for a programmer to understand a program written in a programming language he is not familiar with, because the form of the programming language limits the programmer's understanding of key parts of the program, and pseudo code can be used to help people understand the interpreted code The grammar, rules, connotation and structure of So pseudocode came into being.

When considering the function of an algorithm (rather than its language implementation), pseudo code is often applied. Pseudo-code is often used in computer science teaching to help learners understand quickly and thoroughly.

NO.2's
second boss is do while, who is also one of the three polished commanders. But he is different from While, do while loop is an exit conditional statement. What is an exit condition loop, that is, check the test conditions after each iteration of the loop to ensure that the content in the loop body can be executed at least once.

1. The general form of the do while loop is as follows:

do{

Loop body}while(); Note: the do while statement ends with a semicolon

Note: The execution flow of the do-while loop: first execute the loop body unconditionally, and then judge whether to continue executing the loop body according to the value of the loop control expression. If it is true, continue execution; if it is false, stop execution and exit the do-while loop. In other words, the do-while loop will execute the loop body at least once.

So how do we distinguish between While and do while. I wrote an article about while and do while, see below:

深探 while 与 do while

Note: Use relational operators and expressions to compare sizes

Loops often use test expressions for comparison. Such expressions are called relational expressions. Operators that appear in the middle of relational expressions are called relational operators. The related relational operators are as follows:
Insert picture description here
since it comes to relational operators, let's talk about the precedence of operators

The precedence of relational operators is lower than that of arithmetic operators and higher than assignment operators. The relevant operator priority list is not listed here. It is relatively long. If you are interested, you can go to Baidu to search it.
For as the last boss to play, it can be considered the most played of the three bosses. Why do you say it, because it is easy to use, right, because it is easy to use, it often appears in C language code.

1. The general form of the for loop is as follows:

for (expression 1; expression 2; expression 3) {

Loop body}

Note: There are three expressions in parentheses after the for loop, separated by two semicolons. The first expression is initialization and will only be executed once at the beginning of the loop; the second is the test condition, which is evaluated before the loop is executed; if it is false, the loop ends, otherwise the third statement is executed and continues cycle.

The first expression of the for loop assigns a value to the counter, the second expression represents the range of the counter, and the third expression increments or decrements the counter.

The reason why the for loop is easy to use is also because of its flexibility. And there are nine usages as follows:

  • You can use the decrement operator to decrement the counter

  • You can increment the counter by 2, 10, etc.

  • You can use characters instead of numbers to count

  • In addition to testing the number of iterations, other conditions can also be tested

  • Can increase the amount of increment geometrically, not arithmetic

  • The third expression can use any legal expression

  • One or more expressions can be omitted (but the semicolon cannot be omitted), as long as the loop includes a statement that can end the loop

  • The first expression is not necessarily to assign an initial value to the variable, you can also use printf()

  • The behavior in the loop body can change the expression in the loop header

How do we choose which statement to use when using loops?

First, we have to determine whether we need an entry condition loop or an exit condition loop. Generally, we use more times for the entry condition loop. This is because according to the general principle, the test conditions are better before the execution of the loop, and the test is placed at the beginning of the loop to make the program more readable. Also, in many applications, it is required to skip the entire loop when the test conditions are not met at the beginning.

We usually use the entry condition loop the most. So how do we choose to use the for statement or the while statement in the entry condition loop? This depends on personal preference, because the two can complement each other. In most cases, the for statement and the while statement are interchangeable.

I believe you have learned more or less about the cycle. If you want to understand the cycle better, I suggest you read it several times. These are also some of the most important knowledge points in the cycle that has been sorted out after reading books many times.

Guess you like

Origin blog.csdn.net/m0_46259251/article/details/105943083