C#学习笔记02:程序结构

1)if选择

 if(条件表达式1)

{

    语句序列1;

}

     [ else if(条件表达式2)

{

       语句序列2;

      }

      …

      else{

         语句序列n;

      }

     ]

 eg:   static void Main(string[] args)

        {

            int score = int.Parse(Console.ReadLine());

            if (score >= 90)

                Console.WriteLine("优秀");

            else if(score>=80)

                Console.WriteLine("良");

            else if(score>=70)

                Console.WriteLine("中等");

            else if (score >= 60)

                Console.WriteLine("及格");

            else

                Console.WriteLine("不及格");

            Console.ReadLine();

        }

 2)switch

   switch(表达式){

       case 值1:

      语句序列1;

       case 值2:

      语句序列2;

       …

       case 值n:

      语句序列n;

       [default:

     语句序列n+1;

       ]

   }

eg:

       static void Main(string[] args)

        {

            int score = int.Parse(Console.ReadLine());

            switch (score / 10)

            {

                case 10:

                case 9:

                    Console.WriteLine("优秀");break;

                case 8:

                    Console.WriteLine("良"); break;

                case 7:

                    Console.WriteLine("中等"); break;

                case 6:

                    Console.WriteLine("及格");break;

                default:

                    Console.WriteLine("不及格"); break;

            }

            Console.ReadLine();

        }

3)while,do while循环

   while(条件表达式){

       循环体;

   }

  do{

     循环体;

}while(条件表达式);

    static void Main(string[] args)

        {

            int n = int.Parse(Console.ReadLine());

            int s1 = 0, s2 = 0;

            int n1 = 1, n2 =1;

            while (n1 <= n)

            {

                s1 = s1 + n1;

                n1++;

            }

            do

            {

                s2 = s2 + n2;

                n2++;

            } while (n2 <= n);

            Console.WriteLine("{0} {1} {2} {3}",n1,s1,n2,s2);

            Console.ReadLine();

        }

  4)for循环

   for(变量赋初值;条件表达式判断条件;变量增量){

     循环体;

   }

eg:

   for(int x=0①;x<=10②;x++④){

      s=s+x;③

   }

①-->②-->③-->④

     <------------  

eg:

     int x=0;

   for(;x<=10②;x++④){

      s=s+x;③

   }

   for(;;){

       …

   }

        static void Main(string[] args)

        {

            int n = 100;

            int x = 1,s=0;

            for (; ; )

            {

                s += x;//s=s+x;

                x++;

                if (x > n) break;

            }

            Console.WriteLine("{0} {1}",x,s);

            Console.ReadLine();

        }

5)foreach:遍历集合或数组中元素

      static void Main(string[] args)

        {

            int[] x = { 1, 2, 3, 4, 5 };

            foreach(int a in x)

                 Console.Write(a + " ");

            Console.ReadLine();

       }

本章练习题下载地址:点此下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值