自学三.控制流

6 篇文章 0 订阅

§5.1 控制流概念

在编程中,程序的运行过程分为编译、链接和执行三大阶段。然而控制流则指定了程序中的语句在执行阶段中被系统由主函数开始从前向后或由上到下正常情况下执行某种功能的执行顺序。

§5.1.1 C语言中的三种控制结构

顺序结构:

按照语句编写的顺序自上到下逐句执行。

选择结构:

if选择结构——条件语句为真时,执行动作;条件语句为假,跳过不执行。

if/else选择结构——条件语句为真时,执行if分支的动作;条件语句为假,执行else分支。

if…else if … else if … else选择结构——条件语句为真时,执行if分支的动作;条件语句为假,执行elseif分 支;所有条件 语句为假时,最后执行else分之。

switch选择结构——根据条件表达式的值,选择执行多个动作当中的匹配的那个动作。

循环结构:

while循环结构——前置测试循环,先测试条件是否满足,若满足,再进入循环体执行。

do/while循环结构——前置测试循环,执行顺序与for循环不同。

for循环结构——后置测试循环:先循环一次,再测试条件。

任何一个C#语言程序,都可以由这7种控制结构完成。

§5.2 语句 & 程序块

什么是语句?

表达式+分号 = 语句

注:分号“;”是语句结束的标志。

什么是程序块?

被一对“{ ” 和 “ }”包含起来的语句,这个整体称为程序块。

例如:

int x;
​
{
​
    x = 0;   //赋值表达式语句
​
    x++;     //自增表达式语句
​
    Console.WriteLine("{0}", x); //函数调用语句
​
    int y = x;  //变量定义语句
​
    Console.WriteLine("{0}", y); //函数调用语句
​
}
​
y++;//ERROR
​
x--;

§5.3 if选择结构

if语句中,首先判断表达式的值,然后根据该值的情况控制程序流程。if语句有if、if … else 和else if 3种形式,下面介绍每种形式的具体使用方式。

§5.3.1 单路选择结构(if)

if 用在单向选择语句中,

语法表示如下:

if (expression) 
         语句;

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int score = 100;
​
            if (score >= 90)
            {
                Console.WriteLine("Perfect!\n");
            }
        }
    }
}
​

§5.3.2 双路选择结构(if-else)

if-else 用在双向选择分支语句中, 

语法表示如下:

if (表达式)  //表达式为true,即非0,执行语句1(单一或复合语句)
​
语句1;//{}
​
else         //表达式为false,即0,执行语句2(单一或复合语句)
​
语句2;//{}

如果if括号后面的表达式的值为真,就会执行if下面的语句,否则执行else下的语句;两个语句必有一个执行。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int score = 100;
​
            if (score >= 90)
            {
                Console.WriteLine("Perfect!\n");
            }
            else
            {
                Console.WriteLine("Bad\n");
            }
        }
    }
}

§5.3 .3多路选择结构(else-if)

嵌套结构,测试多个表达式。一旦满足某个表达式,则执行其后代码后, 终止。(只有一个分支下的语句被执行)

语法表示如下:

if (expression)
​
statement1;
​
else if (expression)
​
statement2;
​
else if (expression)
​
statement3;
​
else if (expression)
​
statement4;
​
else  
​
statement5;//处理一些意外情况,错误检验,可以省略不写。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int score = 100;
​
            if (score >= 90)
            {
                Console.WriteLine("Perfect!\n");
            }
            else if (score > 70)
            {
                Console.WriteLine("Good\n");
            }
            else
            {
                Console.WriteLine("Bad\n");
            }
        }
    }
}

§5.4 Switch选择结构

多路选择结构,类似于else-if选择语句,区别在于选择条件表达的取值不同。

语法表示如下:

switch (expression)  //表达式必须返回整数值(包括字符型)
​
{
​
case const-expr1: 
​
statements; //break;
​
//每个case后面跟一个标签值,必须是常数(int类型常量,字符常量)。
​
case const-expr2: statements; break;
​
default: statements; break;
​
//default:默认标签。
​
}

exprission可以是一个表达式或者变量,case后面是表达式或变量的值,让表达式的值与case后面的标签值进行比较和匹配,如果二者相等,就执行case后面的语句。加上break;就是跳出当前的switch语句不在执行下面的语句。如果匹配语句后不写break语句,程序将继续执行后面的语句,即使后面的case标签不匹配,直到遇到一个break才停止。

注意:

  • 表达式只能针对基本数据类型使用switch,这些类型包括int和char等。对于其他类型,则必须使用if语句。
  • case标签必须是常量表达式,如4+2或者"42"。如果需要在运行时计算case标签的值,必须使用if语句。
  • case标签必须是惟一性的表达式;也就是说,不允许两个case具有相同的值。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            char ch = 'A';
​
            switch (ch)
            {
                case 'A':
                    Console.WriteLine("Perfect!\n");
                    break;
​
                case 'B':
                    Console.WriteLine("Good\n");
                    break;
​
                default:
                    Console.WriteLine("Bad\n");
                    break;
            }
        }
    }
}

§5.5 Loops - While and For 循环语句

§5.5.1 for

语法表示如下:

for (expr1; expr2; expr3)
{
​
statements循环体语句
​
}

for循环语句中的表达式详解:

expr1:对循环控制变量,进行有效初始化。一般为int型;

expr2:循环条件表达式。为真,则执行循环体内语句;为假,则跳过循环体;

expr3:修改循环控制变量。为下一次执行相同的动作而改变,递增、或递减。

for循环语句执行原理:

学习for循环时一定要弄清楚三条语句执行的顺序(先执行表达式语句1;然后执行表达式语句2,如果表达式语句2的值为真就去执行循环体中的语句块{}内的语句,执行结束后才执行表达式语句3;然后再执行语句2,如果表达式语句2为真就去执行循环体中的语句块{}内的语句执行结束后才执行表达式语句3;依次这样循环判断后执行,直到表达式语句2为假时,for循环结束)

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("i = {0}\n", i);
            }
        }
    }
}
​
/*输出结果*/
/*
  i = 0
​
  i = 1
​
  i = 2
​
  i = 3
​
  i = 4
*/
​
/*特殊for循环--死循环*/
for (; ; )
{
    Console.WriteLine("Dead-Loop!\n");
}

§5.6 循环嵌套

语法表示如下:

while (boolean-expression)    //条件表达式
​
{
​
statements循环体语句
​
change condition inside loop;循环控制变量修改语句
​
}

while**循环语句中的表达式详解:**

boolean-expression:循环条件表达式。为真,则执行循环体内语句;为假,则跳过循环体;

change condition inside loop: 修改循环控制变量。为下一次执行相同的动作而改变,递增、或递减。

while循环语句执行原理:

如果条件表达式的值为真就执行循环体语句,然后执行循环变量的修改;否则就跳过循环体语句,循环终止。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
​
            int iCount = 1;//循环控制变量
            while (iCount <= 5)
            {
                Console.WriteLine("iCount = {0}\n", iCount);
                iCount++;//修改循环控制变量
            }
        }
    }
}
​
/*输出结果*/
/*
iCount = 1
iCount = 2
iCount = 3
iCount = 4
iCount = 5
*/

注意:

使用while循环时,必须在循环体内部修改循环变量的值,否则造成死循环。

§5.5.3 do…while

语法表示如下:

do 
​
{
​
body of the loop;//循环体
​
change condition inside loop;//修改循环控制变量语句
​
} while (check-expression);

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
​
            int iCount = 1;
            do
            {
                Console.WriteLine("iCount = {0}\n", iCount);
                iCount++;
            } while (iCount <= 10);
​
        }
    }
}
​
/*输出结果*/
/*
iCount = 1
iCount = 2
iCount = 3
iCount = 4
iCount = 5
iCount = 6
iCount = 7
iCount = 8
iCount = 9
iCount = 10
*/

注意:

循环体至少会执行一次。

必须在循环体内部修改循环控制变量的值,否则造成死循环。

循环体内的控制变量不需要初始化,必须在do之前,否则每次的执行结果均与第一次相同。

§5.7 break and continue, return

§5.7 .1 continue语句

特点:

用于循环体内部,作用是结束本次循环,开始执行新的循环。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
​
            for (int i = 0; i < 5; i++)
            {
                if (i == 3)
                {
                    Console.WriteLine("Continue Continue!\n");
                    continue;
                }
​
                Console.WriteLine("i = {0}\n", i);
            }
​
        }
    }
}
​

§5.7 .2 break语句

特点:

用于switch结构,作用是跳出switch结构。

用于循环结构,跳出循环,不再进行新的循环,执行循环体后面的语句。

如果有多层循环,只跳出break所在这一层循环(当前循环体)。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        public static void Main(string[] args)
        {
​
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (i * j >= 6)
                    {
                        Console.WriteLine("Break, Break, You are free!\n");
                        break;
                    }
​
                    Console.WriteLine("Inner Loop-for: {0} * {1}  = {2}\n", i, j, i * j);
                }
​
                Console.WriteLine("OutSide Loop-for\n");
            }
​
            Console.WriteLine("After Loop-for!\n");
​
        }
    }
}

§5.7 .3 return语句

特点:

用于函数返回值;

终止程序执行,返回函数根部。

eg:

// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。 
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// // 
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
​
namespace MSN
{
    class MainClass
    {
        static void returnMethod()
        {
            for (int i = 0; i < 5; i++)
            {
                if (i == 3)
                {
                    Console.WriteLine("return return!\n");
                    return;
                }
​
                Console.WriteLine("i = %d\n", i);
            }
        }
​
        public static void Main(string[] args)
        {
            MainClass.returnMethod();
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值