I remember when I first learned programming, I heard a saying: "It is easy for novices to learn and master a programming language, but if you learn a certain programming language and then learn a new language, it will be difficult to master it." Now I deeply feel the resistance in this area.
In fact, process control, looping, and object-oriented knowledge, are basically the same in all languages. The only difference is that the format and specifications are inconsistent when writing. The differences in these aspects are relatively subtle, so you can often rely on experience to understand them quickly when learning, but later you find that you cannot use your palm to grasp it.
do / while
The meaning of loop is actually to repeat the execution of the statement. Of course, there will be corresponding conditions to judge, otherwise it will become a dead cycle. The difference between a do loop and a while loop: the do loop will execute the loop body first before continuing to judge; while the while loop will judge first, and then decide whether to execute the loop body:
double benJ, liV, muB, liX;
int needY = 0;
("Please enter your deposit amount:");
benJ = (());
("Please enter the deposit rate:");
// +1, the calculated result is principal + interest. If only interest is needed, +1 will be removed. It's important to learn mathematics well
liX = (()) / 100.0;
liV = 1 + liX;
liX = benJ * liX;
("Please enter the expected return:");
do
{
muB = (());
if (muB <= benJ)
{
("The expected return amount is lower than the principal, please enter the return amount higher than the principal:");
}
} while (muB <= benJ);
while (benJ < muB)
{
benJ *= liV;
++needY;
}
("October {3}, deposit {0} year{4}, the sum of your principal and interest {1} can achieve the expected target {2}.", needY, benJ, muB, liX, needY > 1 ? "s" : "");
if (needY == 0) ("High starting point, low goal, in fact, you don't need to deposit money in the bank~");
();
Here we encounter a very good application scenario for do loops: use do loops to determine whether the content entered by the user meets the condition. If not, repeat the execution and exit the loop until the condition is met (the ; number needs to be added at the end of the do loop). The example also applies the ternary operator learned earlier? to determine the result based on the condition and format the string before the format.
for
The for loop is more suitable for applications with specified times. When using it, a variable value as a counter needs to be initialized (it can be declared in the for statement, but the counter variable cannot be accessed outside the loop):
for (int i = 1; i <= 10; i++)
{
("{0}", i);
}
After declaring the counter variable for, use ; sign to separate it, and then add the expression for conditional judgment (the counter variable should be involved) i <= 10, continue to use ; sign to separate it, and add the operation on the counter variable i++ (without the end of ; sign). It is also feasible to try to remove i++ and put it in the circulation body.
The book gives an example, using a for loop to print out the mandelbrot collection. Haha, although I understand the logical structure of the code, I can't understand the algorithm at all, so I won't post it. However, I still read more about the founder of the mandelbrot collection: Benhua Mandebo, and a mathematical structure: fractal. I spent some time reading some information, thanking my predecessors and paying tribute.
Of course, the importance of the example is self-evident. Try to recall the "Nine-Nine Multiplication Table" written by Basic that I simply made one in C#. The principle is the same, pay attention to the details:
int i, k;
for(i = 1; i < 10; i++)
{
("{0}: ", i);
for(k = 1;k <= i; k++)
{
("{0}x{1}={2} " ,k, i, i * k);
}
("\n");
}
();
This for-loop nesting method is very useful in implementing horizontal and vertical loops (output, control). It was often used when doing Excel VBA before.
The interruption of the loop
I haven't remembered before, whether to use break or continue, but the following example is vividly described:
int i = 1;
while (i <= 10)
{
if (i == 6)
break;
("{0}", i++);
}
for (i = 1; i <= 10; i++)
{
if ( i % 2 == 0)
continue;
("{0}",i);
}
();
In fact, both can be called interrupts, but: break is to interrupt the current circulation body (exit the circulation body), and continue is to interrupt the current cycle of the circulation body (not exiting the circulation body).