Example analysis for a For loop in C #

Source: Internet
Author: User
This article mainly introduces the classic case for the For Loop, which has a good reference value. Let's take a look at the little series.

Since the for loop can change the traversing interval by controlling the initial value of the loop variable and the loop ending condition, it is relatively simple to use for loop when sorting or traversing, and the following is some summary cases I get after studying.

1. Application of sorting

1) Exchange sorting: By comparing the number of points taken and the remainder of the number, the largest or smallest number is placed at the top of a set of numbers, then the second largest number is placed in the second position, and then all the numbers are sorted out sequentially.

for (int i = 0; i < (num.length-1), I + +) {for  (int j = i + 1; j < Num.length; J + +)  {     if (Num[i] > Nu M[J])      {        int temp = num[j];        Num[i] = num[j];        NUM[J] = temp;}}  }

The above code is the implementation of the minimum value in the array num from I-num.length, and there is the first position, where Num is a large array of data storage.

2) Bubble sort: By constantly comparing the size of the adjacent two numbers, the large number is exchanged continuously toward the back position, and the small number is floating at the top of the array.

for (int i = Nums. Length-1; i > 0; i--) {  //within the 0-i range, sink the largest number in the range to I for  (int j = 0; J < i; j + +)  {    if (Nums[j] > nums[j+1])    {      // Change      int temp = nums[j];      NUMS[J] = nums[j+1];      NUMS[J+1] = temp;}}  }

3) Select Sort: The minimum number in a range is referred to as the first digit within the range by exchanging sort.

for (int i = 0; i < Nums. Length-1; i++) {  int index = i;//first assume that the subscript of the minimum number is I for  (int j = i + 1; j < Nums. Length; J + +)  {    if (Nums[j] < Nums[index])    {      index = j;    }  }  int temp = Nums[i];  Nums[i] = Nums[index];  Nums[index] = temp;}

2. Determination of prime numbers

BOOL isfinnd = false;for (int i = 2; i < num; i++) {  if (num% i = = 0)  {    isfinnd = true;    break;//When a number i is found to divide num, it means that the current num is a composite, ending the current for loop  }}if (!isfinnd)//If Num is a prime, the error hint {  //determines that the current num is a prime number}

Num of the current code is a specific integer variable.

In addition to the above cases, of course, there are a lot of application scenarios, you need to use the time to constantly summarize their own.

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.