流程控制语句知识点和思考练习,及书里发现的错误

知识点

条件语句:if...else,switch...case
循环语句:while,do,for,foreach
跳转语句:break,continue,goto,return,throw

 

思考练习

using System;
namespace FlowControl
{
    public class FlowControlClass
    {
        static double CalZC(int r)
        {
            double zc = 2 * Math.PI * r;
            return zc;
        }


        static void Main()
        {
            //条件语句 if...else
            int b;
            //            if(5<1)
            //            //int b=10;错误语句,嵌入的语句不能是声明或标记语句
            //            b=10;
            //            else
            //            b=7;
            if (6 > 7)
            {
                b = 10;
            }
            else//视情况可以不加大括号,下面这种简单情况就可以不用加大括号

                if (5 < 1)
                    b = 11;
                else
                    b = 12;

            Console.WriteLine(b);
            Console.ReadLine();
            //循环语句while continue break
            int i = 1, sum = 0;
            while (i <= 10)
            {
                sum += i;
                i++;
            }
            Console.WriteLine(sum);
            Console.ReadLine();
            i = 1;
            sum = 0;
            while (1 == 1)
            {
                sum += i;
                if (++i > 10) break;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

            i = 1;
            int oddnumsum = 0;
            while (i <= 100)
            {
                if (i % 2 == 0)
                {
                    i++;
                    continue;    //跳过当前循环开始下一循环                
                }
                oddnumsum += i;
                i++;
            }
            Console.WriteLine(oddnumsum);
            Console.ReadLine();

            //do...while循环 不管条件是否正确,至少运行一遍
            i = 1; sum = 0;
            do
            {
                sum += i;
                i++;
            } while (i <= 10);//此处的封号一定要有
            Console.WriteLine(sum);
            Console.ReadLine();

            //for循环
            for (i = 1, sum = 0; i <= 100; i++)//其实这样写并不合适,应该是局部变量一个新的变量,并且不能与上下文其他变量重名
            {
                if (i % 2 == 0)
                {
                    continue;
                }
                sum += i;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

            //foreach循环 用于遍历整个数组或集合,但不会改变数组或集合中的内容
            char[] strArr = new char[] { 'a', 'c', 'e', 'f', 'h', 'j' };
            foreach (char c in strArr)
            {
                Console.WriteLine(c);
            }
            Console.ReadLine();

            //switch...case和跳转语句goto return throw如下
            Console.Write("请输入数字");
            int x = int.Parse(Console.ReadLine());//int.Parse()方法为将字符串转换为整型
            switch (x)
            {
                case 1: sum += 10; break;//必须为每一个case块指定一个跳转语句(break或goto)
                case 5: sum = 50; break;
                case 7: sum = 70; break;
                case 17: sum = 60; goto case 1;
                default: goto case 5;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

            int p = 3, q = 4;
            int val = 0;
            string[,] strArr1 = new string[p, q];

            for (int j = 0; j < p; j++)//j作为局部变量,不能与其他非局部变量重名
            {
                for (int k = 0; k < q; k++)
                {
                    val++;
                    strArr1[j, k] = val.ToString();//ToString()为数字转换为字符串                    
                }
            }

            Console.Write("Please input number:");
            string value = Console.ReadLine();
            for (int j = 0; j < p; j++)
            {
                for (int k = 0; k < q; k++)
                {
                    //if(value==strArr1[j,k]) goto L1;//和下句意思应该相同

                    if (strArr1[j, k].Equals(value)) goto L1;
                }
            }
            Console.WriteLine("没有发现");
            goto L2;
        L1:
            Console.WriteLine("发现了!");
        L2:
            Console.WriteLine("查找结束");
            Console.ReadLine();

            int radius = 10;
            Console.WriteLine("周长为{0}", CalZC(radius));//静态方法才可直接引用,而不用引用对象
            Console.ReadLine();



            //
        }
    }
}

 

书内错误

1、P38页,中间示例的第九行if 语句内应为==而不是=

2、P38页,中间示例的第十行,应加 i++; 这条语句

3、P40页,第三行的c应该是val

转载于:https://www.cnblogs.com/365up/archive/2009/09/19/1570146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值