zoukankan      html  css  js  c++  java
  • C#中foreach,for,while,DoWhile循环对比

    foreach循环

    // An array of integers
    int[] array1 = {0, 1, 2, 3, 4, 5};

    foreach (int n in array1)
    {
        System.Console.WriteLine(n.ToString());
    }


    // An array of strings
    string[] array2 = {"hello", "world"};

    foreach (string s in array2)
    {
        System.Console.WriteLine(s);
    }

    for循环

    // An array of integers
    int[] array1 = {0, 1, 2, 3, 4, 5};

    for (int i=0; i<6; i++)
    {
        System.Console.WriteLine(array1[i].ToString());
    }


    // An array of strings
    string[] array2 = {"hello", "world"};

    for (int i=0; i<2; i++)
    {
        System.Console.WriteLine(array2[i]);
    }

    while循环

    // An array of integers
    int[] array1 = {0, 1, 2, 3, 4, 5};
    int x = 0;

    while (x < 6)
    {
        System.Console.WriteLine(array1[x].ToString());
        x++;
    }


    // An array of strings
    string[] array2 = {"hello", "world"};
    int y = 0;

    while (y < 2)
    {
        System.Console.WriteLine(array2[y]);
        y++;
    }

    Do while循环

    // An array of integers
    int[] array1 = {0, 1, 2, 3, 4, 5};
    int x = 0;

    do
    {
        System.Console.WriteLine(array1[x].ToString());
        x++;
    } while(x < 6);


    // An array of strings
    string[] array2 = {"hello", "world"};
    int y = 0;

    do
    {
        System.Console.WriteLine(array2[y]);
        y++;
    } while(y < 2);

  • 相关阅读:
    跨页传值另一种方法
    运行nodejs的blog程序遇见问题
    nodejs和mongodb实践
    mongodb数据库实践笔记
    两次分页显示内容——先少后多显示
    Java进阶4表达式中的陷阱
    Java进阶3. 内存回收机制
    Java进阶1. Synchronized 关键字
    Java复习9网路编程
    Java复习8.多线程
  • 原文地址:https://www.cnblogs.com/Luouy/p/1328896.html
Copyright © 2011-2022 走看看