C#初级(分支语句 循环 (例子))

1.单分支语句if  

            if语句的表达式:if(条件){语句块1;语句块2}
           //如果我们if语句块只有一个语句,那么可以省略{}
            if(0==0)
            {

                Console.WriteLine ("111");
            }

            //练习一比较俩个数的大小(单分支)
            int a=int.Parse(Console.ReadLine());
            int b=int.Parse(Console.ReadLine());

            if(a>b)
            {
                Console.WriteLine (a);
            }

2.双分支语句 if.....else

                     if(0==0)
            {

                Console.WriteLine ("成立");
            }
            else
            {
                Console.WriteLine ("不成立");
            }

            练习二比较三个数的大小

            #region
            int max;
            int a = int.Parse (Console.ReadLine ());
            int b = int.Parse (Console.ReadLine ());
            int c = int.Parse (Console.ReadLine ());
            if (a > b) {
                max=a;
            } 
            else
            {
                max=b;
            }
            if (max > c) {
                Console.WriteLine (max);
            } else {
            
                Console.WriteLine (c);
            }

3. 多重分支语句if...else if....else if... -->switch

                         int inttemp=10;
            if(inttemp>10)
            {
                Console.WriteLine ("111");
            }
            else if(inttemp==10)
            {
                Console.WriteLine ("222");

            }
            else
            {
                Console.WriteLine ("333");
            }

            练习三 战斗积分排行榜,从键盘上获取玩家的分数,如果改分数>10000分显示为金牌,如果该分数8000《分数《10000显 

            示银牌,分数<5000显示铜牌
            int a=0;
             a=int.Parse(Console.ReadLine());
        
            if (a >= 10000) {
                Console.WriteLine ("a: 金牌");
            } else if (8000 <= a &&a< 10000)  { 
                Console.WriteLine ("a :银牌");
            } else if(5000<=a&&a<8000) {
                Console.WriteLine ("a :铜牌");
            }
            else
            {
                Console.WriteLine ("a:未上榜");
            }
            课堂练习4 输入一个年份 判断该年份是否闰年?如果是闰年,则输出改年是闰年,否则输出改年不是闰年。
            闰年:能被400整除或者(能被4整除,但是不能被100整除)

            int a=0;
                     a=int.Parse(Console.ReadLine());
            if (a %400==0||(a%4==0&&a%100!=0)) {
                                Console.WriteLine ("闰年");
                            } 

                    else
                            {
                
                                Console.WriteLine ("不是闰年");
                            }
                
            #endregion
            #region //switch语句块
            char charTemp='A';

            switch (charTemp)
            {
            case 'A':
                Console.WriteLine ("用户输出的是A");
                break;
            case 'B':
                Console.WriteLine ("用户输出的是B");
                break;
            default:
                Console.WriteLine ("default分支");
            break;
            }


            for(int i=0;i<7;i++)
            {
            int a=int.Parse(Console.ReadLine());
            
            switch (a)
            { 
            case 1: 
                Console.WriteLine ("星期一");
                break;
            case 2:
                Console.WriteLine ("星期二");
                break;
            case 3:
                Console.WriteLine ("星期三");
                break;
            case 4:
                Console.WriteLine ("星期四");
                break;
            case 5:
                Console.WriteLine ("星期五");
                break;
            case 6:
                Console.WriteLine ("星期六");
                break;
            case 7:
                Console.WriteLine ("星期日");
                break;
            default:
                Console.WriteLine ("输入超出范围");
            break;
            }
            }

4. 循环语句

                /*
             *1.while循环 
             * 2.do..while循环
             * 3.for循环
             * 4.foreach循环
             */
            while(表达式)
            {
                语句块

                        }
            int i=1;
            while(i==1)
            {
                Console.WriteLine ("...");
            }

            练习1:输出十句helloworld
            int a=1;

            while (a<=10) {
                Console.WriteLine ("helloworld");
                a++;
            }

            练习2:累加1-100的和;
            int a=1;
            int sum=0; 
            while (a<=100) {
                
                sum+=a;
                a++;
            }
            Console.WriteLine (sum);
            //练习3 输出1-100之间所以的偶数和
            int a=1;
            int sum=0;

            while (a<=100) {
                if(a%2==0){
                    sum+=a;
                    Console.WriteLine (a);
                }
                a++;
            }

            int a=2;
            int sum=0;
            while (a%2==0&&a<=100) {
                sum+=a;
                a+=2;
                Console.WriteLine (a);
        }

            练习4 输出1-100之间的所有数字

            int i=1;
            while (i<=100) {
                
                Console.WriteLine (i);
                i++;
            }
            练习5 输出1-100之间所有能整除4的数
                    int a=1;

                while (a<=100) {
                if(a%4==0){
                    Console.WriteLine (a);
                }
                            a++;
                }
            练习6 输出1-100之间所有的奇数和

            int a=1;
            int sum=0;
            while (a<=100) {
                if(a%2!=0)
                {
                    sum+=a;
                    Console.WriteLine (sum);
                }
                a++;
            } 




            #endregion

            #region //循环语句do....while循环

            int a=0;
            do{
                Console.WriteLine ("该语句只会被输出一次");
            }
            while (a<0); 
            练习7:输出1-100之间能被3整除但不能被4整除的所有奇数
            int a=0;
            do{
                if(a%3==0)
                {
                    if(a%4!=0)
                    {
                        Console.WriteLine (a);

                    }
                }
                a++;
            }
            while(a<=100);







            #endregion
            #region //循环语句for
            while(true)
            {
                Console.WriteLine ("死循环");
            }
            for(;;)
            {
                Console.WriteLine ("死循环");
            }
            //for循环语法格式
            for(初始化变量;判断条件;自增或自减)
            {
                语句块
            }

            for循环执行顺序
            1.初始化条件
            2.执行条件判断
            3.语句块1
            4.自增或自减其他条件
            5.回到第二步
            6.条件不满足跳出整个for循环

            练习9:求和1-100使用for循环
            int a=1;
            int sum=0;
            for(a=1;a<=100;a++)
            {
                sum+=a;
                Console.WriteLine (sum);
            }

            练习10:输出1-100之间的所有奇数
            int a=1;
            for(a=1;a<=100;a++)
            {
                if(a%2!=0)
                {
                    Console.WriteLine (a);
                }

            }

            练习11:打印000-999之间的所有数

        
            for(int a=0;a<10;a++)

            {
            for(int j=0;j<10;j++)
            { 
                for(int k=0;k<10;k++)
                {
                    Console.WriteLine ("{0}{1}{2}",a,j,k);
                }
            }
            }
            练习12:打印9*9乘法表
            for(int i=1;i<=9;i++)
            {
                for(int j=1;j<=i;j++)
                {
                    Console.Write (i+"*"+j+"={0}",j*i+" ");
                }
                Console.WriteLine ();
            }
            练习13:打印任意三角形*

            for(int i=1;i<=4;i++)
                            {
                                for(int j=1;j<=i;j++)
                                {
                                    Console.Write ("*");
                                }
                                Console.WriteLine ();
                            }


            练习14:从键盘上输入行数打印数字三角形
            int n=int.Parse(Console.ReadLine());

            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=i;j++)
                {
                    Console.Write (j);
                }
                Console.WriteLine ();
            }

5. break, continue 关键词用法

                     break:作用就是跳出当前循环
            演示:break的用法
            int index=0;
            int sum=0;
            for (int i = 1; i <=100; i++) {
                sum+=i;
                if(sum>1000)
                {
                    index=i;
                    break;
                }
            }
            Console.WriteLine (sum-index);

            //练习15 
            for(int i=0;i<9;i++)
            {
                for (int j = 0; j < 9; j++) { 
                    break;
                    Console.WriteLine ("****");

                }
                Console.WriteLine ("------");
            }
            练习16
            for(int i=2;i<100;i++)
            {
                if(i%2==0){ 
                    break;

                }
                Console.WriteLine ("------");
            }

            #endregion

            #region //continue关键字作用
            //continue:作用跳出剩下语句快,进入下次循环
            //演示1:
            for (int i = 1; i < 100; i++) {
                if(i%2!=0)
                {
                    continue;
                    Console.WriteLine ();
                }
                
            }

            练习17:求整数1-100的累加值,排除个位为3的
            

                
            int sum=0;
            for (int i = 1; i <= 100; i++) {
                if(i%10==3)
                {
                    continue;
                }
                sum+=i;

            }
            Console.WriteLine (sum);
            
                
            }
            练习18:输出1-100之间的所有整数和。当和值超过1500时停止,并输出该值
            int sum1=0;
            for (int i = 1; i < 101; i++) {
                
                if(sum1>1500)
                {
                    break;
                }
                sum1+=i;
            }
            Console.WriteLine (sum1);         

            练习19:计算1-100的奇数累加并且小于4位数的累加和
            int sum=0;
            for (int i =1; i <101; i++) {
                if (i%2!=0)
                {
                    sum+=i;
                    if (sum>=1000)
                    {
                        
                        break;
                    }
                }
                Console.WriteLine (sum);

            }

            练习20:打印输出平方值小于20000的最大整数
            int n=1;
            for(n=1;n<20000;n++)
            {    
                if(n*n>20000)
                {
                    Console.WriteLine (n-1);
                    break;
                }

            }

推荐阅读更多精彩内容