2017.02.23. 学习C#第五天,循环语句for助我脑洞大开。。。

2017.02.23学习循环语句for,循环+1.

一,什么是循环语句

循环语句就是在满足条件的情况下,不断的在计算机中生成一个循环,直到不满足循环条件,终止循环。

  for语句循环四要素:       初始条件-----循环条件-----循环体-----状态改变

for(初始条件;循环条件;状态改变)

         {循环体}

 

 

 

一个简单的for循环

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 输出1_100的所有数字
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.WriteLine("1-100以内所有的整数:");
13             Int32 a;
14                 for(a=1;a<=100;a++)
15                 { 
16                     Console.Write(a + ".");
17                 }
18                 Console.ReadLine();
19         }
20     }
21 }

分析一下这段程序

看这段循环:

1,初始条件:a=1

2,循环条件:a<=100

3,循环体:a

4,循环状态:a++

当初始条件a=1时,满足循环条件a<=100,此时输出循环体a,a的值为1,跳转到状态改变a++,此时a+1,a的值变为2,跳转循环条件,满足循环条件a<=100,输出循环体a的值为2,再次跳转到状态改变a++,此时a+1,a的值变为3,一次循环直到a的值为100,输出循环体a的值为100,跳转状态改变a++,a变为101,跳转循环条件a<=100,不满足循环条件,此时循环终止。共输出a的值1,2,3,4,5,6,7,8,9,…………100.

 

 

 

二,switch…………case…………循环语句

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 输出1_100的所有数字
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12           
13             Int32 a = 1;
14             switch (a)
15             {
16                 case 1: Console.WriteLine("你好!"); break;
17                 case 2: Console.WriteLine("你说什么?"); break; 
18             }
19             Console.ReadLine();
20                 
21         }
22     }
23 }

a=1,switch意为转换的意思,可以这么理解,转换a,如果a=1,输出你好!,如果a=2,输出你说什么?

该程序中a的值为1,所以会输出你好!

switch(变量名)

{case 变量值 :输出结果;break;

case 变量值:输出结果;break;}

 

posted on 2017-02-23 22:48  张鑫4477  阅读(197)  评论(0编辑  收藏  举报