• C#流程控制语句


    1. 选择语句

    (1)    if语句

    (2)if…else 语句

     (3)if…else if…else if ………else

      class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Console.Write("请输入一个字符");
    
                char c = (char)Console.Read();
    
                if (char.IsUpper(c))
    
                {
    
                    Console.WriteLine("大写字母");
    
                }
    
                else if (char.IsLower(c))
    
                {
    
                    Console.WriteLine("小写字母");
    
                }
    
                else if (char.IsDigit(c))
    
                {
    
                    Console.WriteLine("数字");
    
                }
    
                else
    
                {
    
                    Console.WriteLine("其他字符");
    
                }
    
            }
    
        }

     (4)if 嵌套

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Console.WriteLine("请输入一个字符");
    
                char c = (char)Console.Read();
    
                if (Char.Isletter(c))
    
                {
    
                    if (char.IsUpper(c))
    
                    {
    
                        Console.WriteLine("大写字母");
    
                    }
    
                    else
    
                    {
    
                        Console.WriteLine("小写字母");
    
                    }
    
                }
    
                else
    
                {
    
                    Console.WriteLine("输入的字符不是数字");
    
                }
    
            }
    
    }
    1. switch语句
    switch(n)
    
    {
    
       case(n1):
    
           ….
    
       break;
    
       case(n2):
    
           …
    
       break;
    
       ….
    
       default:
    
    ….
    
    break;
    
    }

    Eg:屏幕输数字

    1. while循环

    while(表达式)

    {

       循环体

    }

    Eg:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Console.WriteLine("循环结果为:");
    
                int n = 1;
    
                while (n < 6)
    
                {
    
                    Console.WriteLine("循环第{0}次", n);
    
                    n++;
    
                }
    
            }
    }
    1. do…while循环(用得较少)

    先做,再看while如果while里面的东东对,继续返回去接着做,否则退出循环

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Console.WriteLine("循环结果为:");
    
                int n = 1;
    
                do
    
                {
    
                    Console.WriteLine("循环第{0}次", n);
    
                    n++;
    
                }
    
                while (n < 0);
    
            }
    
    }
    1. for循环

    eg:

      class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Console.WriteLine("循环结果为:");
    
                for (int i = 0; i < 10; i++)
    
                {
    
                    Console.WriteLine(i);
    
                }
    
            }
    
    }
    1. for的嵌套
    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Console.WriteLine("循环结果为:");
    
                for (int j = 0; j < 5;j++ )
    
                {
    
                    for (int i = 0; i < j; i++)
    
                    {
    
                        Console.WriteLine(j);
    
                      
    
                    }
    
                }
    
            }
    
    }
    1. foreach
    foreach(类型 变量名 in 集合对象)
    
    {
    
    语句体
    
    }

     数组作为例子

    1. 跳转语句

    break

    eg:

    for(int i=1;i<=10;i++)
    
    {
    
        If(i>4)
    
        {
    
           break;
    
    }
    
    Console.WriteLine(i);
    
    }

    结果为:1 2 3 4

    continue

    eg:

       for(int i=1;i<=10;i++)
    
       {
    
           If(i<9)
    
           {
    
              continue;
    
    }
    
    Cosonle.WriteLine(i);
    
    }

    结果为 9 10

    1. return语句

    将控制返回给调用方法,还可以返回一个可选值,如果方法为void类型,则省去return

    class Program
    
       {
    
       static  double Aa(int r)
    
       {
    
           Double area=r*r* Math.PI;
    
           return area;
    
    }
    
    static void Main()
    
    {
    
        int radius=5;
    
        Console.WriteLine("输出结果为:
     The area is {0:0.00}",Aa(radius));
    
        Console.Read();
    
    }
    
    }
  • 相关阅读:
    C语言复习---杨辉三角打印
    C语言复习---获取矩阵的对角和
    C语言复习---选择法排序
    C语言复习---用筛选法求100之内的素数
    C语言复习---比赛问题
    C语言复习---打印菱形
    建立一个node.js服务器(使用express搭建第一个Web环境)
    nodejs小问题:express不是内部或外部命令
    使用express搭建第一个Web应用【Node.js初学】
    Node.js 相关资料网站汇总
  • 原文地址:https://www.cnblogs.com/yinsheng/p/5708356.html
Copyright © 2020-2023  润新知