C # Summary of loop implementation methods,

Source: Internet
Author: User

C # Summary of loop implementation methods,

  • C # loop statements: while, for, foreach

1. while Loop

        static void Main(string[] args)        {            int[] hs = { 1,2,3,4,5,6,7,8,9};            int ligh = hs.Length;            while (ligh > 0)            {                Console.WriteLine(hs[ligh - 1]);                ligh -= 1;            }            Console.ReadKey();        }    

2. for Loop (nested for loop can be used, for example, during Bubble Sorting)

Static void Main (string [] args) {int [] hs = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; // for reverse printing, you only need to modify the judgment condition for (int I = 0; I 

3. foreach cyclically traverses elements in the Set (this method seems to be unique to. NET)

Static void Main (string [] args) {int [] hs = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // The var keyword is used here, anonymous type (automatically inferred by the compiler). You can replace it with int foreach (var item in hs) {Console. writeLine (item. toString ();} Console. readKey ();}

 

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.