"C # Learning Note" "6" to find out that all of the 0-100 can be divisible by 3 or be divisible by 5 by 2 (two) (flow control)

Source: Internet
Author: User

Remember the flowchart we drew in the previous section? We will know the judgment part, and it is almost a loop. Below I will introduce three commonly used loops.

Loop structure while loop (when loop, at least my C language teacher called it)
Let's introduce the syntax first, as follows

while (conditional expression)
{
    // Here is the loop body. When the above conditional expression is true, execute the loop body, otherwise exit
}
while is followed by a conditional expression, and the loop is executed if it is true. After the execution of the loop body, the conditional expression is evaluated, and the loop is pushed out when it is false.

For example, we output a number from 1 to 100. The routine is as follows:

int i = 1;
while (i <= 100)
{
    Console.WriteLine (i);
    i ++;
}
The output ends as follows:

// The result is as follows
1
2
3
...
99
100
Do you understand this example? Let me explain. First, when i = 1 is executed to while, it is judged that i <= 100, which is true, so in the loop, i is output, and i is incremented by 1, that is, i is now equal to 2, and then back. At the head of the loop, it is judged that i <= 100, it is true, and then it enters the loop body. When i = 101, the condition i <= 100 is found to be false. As a result, the loop is not executed, and it ends. .

OK, I believe you already understand, then we can solve the problem in the title of the article:

int i = 0;
while (i <= 100)
{
    if (i% 3 == 0 || i% 5 == 2)
    {
        Console.WriteLine (i);
    }
    i ++;
}
It's so easy, right? I didn't use else here, because I only need to find the numbers that meet the intent of the question, and the question did not say to deal with the numbers that do not match. By the way, i here is actually a counter that controls the number of loops. Below we introduce the variants of while.

do ... while (until the loop, of course my teacher told me to read like this)
Of course, start with the syntax:

do
{
    // here is the loop body
) while (conditional expression);
It's almost the same as while, but with one more do, the loop body runs in front of the while. Let me explain the execution sequence:

The first step is to execute the loop body before determining whether the condition is true. If it is true, perform the second loop body execution until the condition is false and exit.

It can be seen that this difference between a loop and a current loop is different, until the loop will execute the loop body at least once, and when the loop is not necessarily. Of course, in actual work, we use more loops.

No examples are given here. The most commonly used one is called the counting cycle.

for loop (counting loop)
Look at the title and explain that this counts itself. Come on

for (int i = 0; i <length; i ++)
{
    // loop body
}
Explain the process.

For is followed by three statements. The first sentence actually declares a counter (what a counter was just introduced), the second is the length of the counter, which is a conditional expression, and the third sentence, the counter is incremented.

The execution process is to first execute the first sentence, initialize the counter, then determine whether the second sentence is true, and then enter the loop body to execute. After the loop body is executed for the first time, the third sentence after the for is executed, which is the counter. Auto-increment, then judge the condition, enter the loop body again until it exits the loop. It's a bit abstract, we use a for loop to do 1 + 2 + 3 + 4 + ... + 100 once, the code is as follows:

int sum = 0; // This is actually called an accumulator
for (int i = 1; i <= 100; i ++)
{
    sum + = i;
}
Console.WriteLine (sum);

// Run result 5050
Is it easier than using while?

This is the most commonly used loop. Of course, if you write all the external adjustments as true, that is an endless loop, and it will never come out again. Below I introduce two keywords that can jump out of the loop when appropriate. code show as below:

int i = 0;
while (true)
{
    if (i <10)
    {
        Console.WriteLine (i);
        i ++;
    }
    else
    {
        break;
    }
}


//operation result
0,
1
2
3
...
9
I wrote an endless loop, but the real loop was repeated 10 times, because when i> 10, I used break, the word can jump out of the entire loop.

Look at the following example again:

int i = 0;
while (true)
{
    if (i <10)
    {
        i ++;
        continue;
    }
    if (i <20)
    {
        Console.WriteLine (i);
        i ++;
    }
    else
    {
        break;
    }
                
}

//result
10
11
...
19
Found nothing, continue removed the mask behind the loop.

Although the second if is true for the first 10 loops, it is not executed. In other words, once the continue statement is encountered in the loop, the loop body is no longer executed, but it will enter the next loop of the loop, and will not stop all loops like break.

to sum up
Three kinds of loop statements, focusing on while loop and for loop.
The difference between break and continue.
Exercise
Find the sum of 1 / (1 * 3) + 2 / (3 * 5) + 3 / (5 * 7) + ... + n / ((2 * n-1) * (2 * n + 1)), It is required to terminate when the absolute value of the difference between the previous N-1 term and the previous N term is less than 0.00001. And output n at this time.
Exercise answers from the previous article
1.

int a = 5;
int b = 4;
int c = 7;
if (a + b> c && Math.Abs (a-b) <c) // The absolute value is taken to prevent the small side from being subtracted from the large side
{
    Console.WriteLine ("Three sides can form a triangle");
}
else
{
    Console.WriteLine ("Three sides cannot form a triangle");
}
2.3.4 slightly (if in doubt, please email me, [email protected])

[C # study notes] [6] Find all numbers within 0-100 that can be divisible by 3 or 5 that are divisible by 2 (two) (flow control)

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.