c entry for the loop statement using the detailed of for loop do and while

  • 2020-06-19 11:36:45
  • OfStack

I remember when I first learned programming, I heard a sentence: "It is easy for beginners to learn and master a programming language, but if you learn a programming language, then learn a new language, it will be difficult to master". Now I feel the resistance in this aspect deeply.

In fact, process control, circulation, including object-oriented knowledge, basically all languages are the same, the only difference lies in the format and specification of writing. The differences in these aspects are more subtle, so often rely on experience to understand quickly when learning, but later found that they can not use (the hand) (the hand).

do / while

The meaning of the loop is to execute the statement repeatedly, of course, there will be corresponding conditions, otherwise it will become a loop. The difference between the do loop and the while loop is that the do loop executes the loop body once before proceeding. The while loop will judge before deciding whether to execute the loop body:


double benJ, liV, muB, liX;
int needY = 0;
Console.WriteLine(" Please enter your deposit amount: ");
benJ = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(" Please enter the deposit interest rate: ");
// +1 , the calculated result is the principal + Interest, if interest is all that is needed, is taken out +1 . It's important to learn math well 
liX = Convert.ToDouble(Console.ReadLine()) / 100.0;
liV = 1 + liX;
liX = benJ * liX;
Console.WriteLine(" Please enter expected revenue: ");
do
{
    muB = Convert.ToDouble(Console.ReadLine());
    if (muB <= benJ)
    {
        Console.WriteLine(" The expected income is lower than the principal, please enter the income higher than the principal: ");
    }
} while (muB <= benJ);
while (benJ < muB)
{
    benJ *= liV;
    ++needY;
}
Console.WriteLine(" Every year the interest  {3} And deposit  {0} year{4} , your principal and interest  {1}  Be able to achieve the desired goal  {2} . ", needY, benJ, muB, liX, needY > 1 ? "s" : "");
if (needY == 0) Console.WriteLine(" High starting point, low goal, in fact, you do not have to deposit money in the bank ~");
Console.ReadKey();

Here's a nice do loop scenario: Use the do loop to determine if the user's input meets the criteria. If not, repeat until the criteria are met. No.). The example also applies the three - element operator you learned earlier? , judge the result according to the condition, format the preceding string.

for

The for loop is more suitable for the specified number of times, when used, you need to initialize 1 variable value as the counter (can be declared in the for statement, but the counter variable cannot be accessed outside the loop) :


for (int i = 1; i <= 10; i++)
{
    Console.WriteLine("{0}", i);
}

After declaring the counter variable of for, use i, followed by an expression for the conditional judgment (which should involve the counter variable) < = 10, continue to use; Add the operation on the counter variable i++ (no; At the end of the sign. It is also possible to remove i++ and put it in the circulation.

The book gives an example of using for loop to print out mandelbrot set. Although I can understand the logic structure of the code, I can't understand the algorithm at all, so I won't post it. Nevertheless, I have extended the reading for the founder of mandelbrot, Benwa Mandelbaum, and a mathematical construct, fractal. Took some time to read some materials, thank the predecessors, salute.

Of course, the importance of examples is self-evident, try to recall the time you learned basic to write the 99 Multiplication Table, and simply do one in C#. The principle is the same, the main attention to detail:


int i, k;
for(i = 1; i < 10; i++)
{
    Console.Write("{0}: ", i);
    for(k = 1;k <= i; k++)
    {
        Console.Write("{0}x{1}={2} " ,k, i, i * k);
    }
    Console.WriteLine("\n");
}
Console.ReadKey();

This nesting of for loops is very useful for horizontal and vertical loops (output, control), which are often used when doing excel VBA.

Interruption of a loop
I didn't remember break or continue, but here is an example of how to use it.


int i = 1;
while (i <= 10)
{
    if (i == 6)
        break;
    Console.WriteLine("{0}", i++);
}
for (i = 1; i <= 10; i++)
{
    if ( i % 2 == 0)
        continue;
    Console.WriteLine("{0}",i);
}
Console.ReadKey();

Both can be called interrupts, except that break is the body that interrupts the current loop (exiting the loop body) and continue is the body that interrupts the current loop (which does not exit the loop body).


Related articles: