C # loop statement for loop,

Source: Internet
Author: User

C # loop statement for loop,

Loop: execute a code segment repeatedly.

Four elements of a loop: initial condition, cyclic condition, cyclic body, and state change.
For (initial condition; cyclic condition; state change)
{
Loop body
}

Given the initial conditions, first determine whether the cycle conditions are met. If the conditions are not met, the for statement is skipped. If the conditions are met, the for statement is executed. After the code in the for statement is executed, changes the variable according to the state, and then determines whether or not the loop conditions are met. If yes, the code in the for statement is executed until variable I does not meet the loop conditions, the loop is terminated, or run the break command to directly jump out of the current for loop.

Break-interrupt loop, jump out of Loop
Continue -- stop this loop and enter the next loop

Loops (for) and branch statements (if else, etc.) can be nested with each other.

 

Endless loop:

For (;;)
{
Console. WriteLine ("Hello ");
}

Note: an endless loop is not allowed in the program!

Application

Cyclic language is cumbersome and requires more exercises.

1. Input an integer to calculate the result of adding from 1 to this number.
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Int sum = 0;
For (int I = 1; I <= a; I ++)
{
Sum + = I; // sum = sum + I;
}

Console. WriteLine (sum );
Console. ReadLine ();

2. Enter a positive integer to calculate the factorial.
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Int sum = 1;
For (int I = 1; I <= a; I ++)
{
Sum * = I; // sum = sum * I;
}

Console. WriteLine (sum );
Console. ReadLine ();

3. Enter a positive integer to calculate the factorial and 1! + 2! +... + N!
Console. Write ("enter a positive integer :");
Int a = int. Parse (Console. ReadLine ());
Int sum = 1;
Int sum1 = 0;
For (int I = 1; I <= a; I ++)
{
Sum * = I; // sum = sum * I;
If (I = 4) // do not want 4
{
Continue; // terminate this cycle and continue the next cycle
}
Sum1 + = sum;
}

Console. WriteLine (sum1 );
Console. ReadLine ();

 

4. In a game, the first 20 levels are the scores of each level,
From to 30, each mark is 10 points.
31-40 off, each off is 20 points
Mark 41-49, with each mark being 30 points
50 off, which is 100 points
Enter the number of levels you have reached.
Two Methods: if nested for nested if
(1) Console. Write ("Enter the number of levels you have reached :");

Int guan = int. Parse (Console. ReadLine ());
If (guan> = 1 & guan <= 50)
{
Int sum = 0;
If (guan <= 20)
{
For (int I = 1; I <= guan; I ++)
{
Sum + = I;
}
}
Else if (guan <= 30)
{
For (int I = 1; I <= 20; I ++)
{
Sum + = I;
}
For (int I = 21; I <= guan; I ++)
{
Sum + = 10;
}
}
Else if (guan <= 40)
{
For (int I = 1; I <= 20; I ++)
{
Sum + = I;
}
For (int I = 21; I <= 30; I ++)
{
Sum + = 10;
}
For (int I = 31; I <= guan; I ++)
{
Sum + = 20;
}
}
Else if (guan <= 49)
{
For (int I = 1; I <= 20; I ++)
{
Sum + = I;
}
For (int I = 21; I <= 30; I ++)
{
Sum + = 10;
}
For (int I = 31; I <= 40; I ++)
{
Sum + = 20;
}
For (int I = 41; I <= guan; I ++)
{
Sum + = 30;
}
}
Else // 50 off
{
For (int I = 1; I <= 20; I ++)
{
Sum + = I;
}
For (int I = 21; I <= 30; I ++)
{
Sum + = 10;
}
For (int I = 31; I <= 40; I ++)
{
Sum + = 20;
}
For (int I = 41; I <= 49; I ++)
{
Sum + = 30;
}
Sum ++ = 100;
}
Console. WriteLine (sum );
}
Else
{
Console. WriteLine ("incorrect input! ");
}
Console. ReadLine ();

 

 

(2) Console. Write ("Enter the number of levels you have reached :");
Int guan = int. Parse (Console. ReadLine ());
If (guan <= 50 & guan> = 1)
{
Int sum = 0;
For (int I = 1; I <= guan; I ++)
{
If (I <= 20)
{
Sum + = I;
}
Else if (I <= 30)
{
Sum + = 10;
}
Else if (I <= 40)
{
Sum + = 20;
}
Else if (I <= 49)
{
Sum + = 30;
}
Else
{
Sum ++ = 100;
}
}
Console. WriteLine (sum );
}
Else
{
Console. WriteLine ("incorrect input! ");
}
Console. ReadLine ();

 


5. Find the number related to 7 in 100 and print,
(1). Find 100 from 1
(2). Find the number related to 7
The single digit is 7 a % 10 = 7
10 digits are 7 a/10 = 7
Divisible by 7 a % 7 = 0
For (int I = 1; I <= 100; I ++)
{
If (I % 7 = 0 | I % 10 = 7 | I/10 = 7)
{
Console. Write (I + "\ t ");
}
}
Console. ReadLine ();

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.