博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(原创)c#学习笔记04--流程控制04--循环03--for循环
阅读量:4329 次
发布时间:2019-06-06

本文共 2561 字,大约阅读时间需要 8 分钟。

4.4.3 for循环

  这类循环可以执行指定的次数,并维护它自己的计数器。

  要定义for循环,需要下列信息:

    初始化计数器变量的一个起始值。

    继续循环的条件,它应涉及到计数器变量。

    在每次循环的最后,对计数器变量执行一个操作。

  例如,如果要在循环中,使计数器从1递增到10,递增量为1,则起始值为1,条件是计数器小于或等于10,在每次循环的最后,要执行的操作是给计数器加1。

  这些信息必须放在for循环的结构中,如下所示:

for (
;
;
) {
}

  它的工作方式与下述while循环完全相同:

while (
) {
}

  循环输出从1~10的数字。下面看看如何使用for循环完成这个任务:

int i; for (i = 1; i <= 10; ++i) {     Console.WriteLine("{0}", i); }

  最后要注意的是,可以把计数器变量声明为for语句的一部分,重新编写上述代码,如下所示:

for (int i = 1; i <= 10; ++i) {     Console.WriteLine("{0}", i); }

  但如果这么做,就不能在循环外部使用变量i(参见第6章中的“变量作用域”一节)。

  下面看一个示例,他将:显示一个Mandelbrot集合(使用纯文本字符,看起来不会那么吸引人)!,代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Ch04Ex06{    class Program    {        static void Main(string[] args)        {            double realCoord, imagCoord;            double realTemp, imagTemp, realTemp2, arg;            int iterations;            for( imagCoord =1.2; imagCoord >= -1.2; imagCoord -= 0.05 ) {                for( realCoord = -0.6; realCoord <= 1.77; realCoord += 0.03 ) {                    iterations = 0;                    realTemp = realCoord;                    imagTemp = imagCoord;                    arg = ( realCoord * realCoord ) + ( imagCoord * imagCoord );                    while( ( arg < 4 ) && ( iterations < 40 ) ) {                        realTemp2 = ( realTemp * realTemp ) - ( imagTemp * imagTemp ) - realCoord;                        imagTemp = (2 * realTemp * imagTemp) - imagCoord;                        realTemp = realTemp2;                        arg = ( realTemp * realTemp ) + ( imagTemp * imagTemp );                        iterations += 1;                    }                            switch( iterations % 4 ) {                        case 0:                            Console.Write( "." );                            break;                        case 1:                            Console.Write( "o" );                            break;                        case 2:                            Console.Write( "O" );                            break;                        case 3:                            Console.Write( "@" );                            break;                    }                }                Console.Write( "\n" );  // 换行             }            Console.ReadKey();        }    }}

  运行结果如下:

 

转载于:https://www.cnblogs.com/wodehao0808/p/4899161.html

你可能感兴趣的文章
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>