1、foreach每执行一次内含的代码时,循环变量就会一次读取集合中的一个元素,不需要个数。循环变量只是一个只读的局部变量,这个值是不能修改的。char后的word是 foreach语句的迭代变量,它是只读的,不允许修改。

源程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一串文字:");
String str = Console.ReadLine();
foreach (char item in str)
{
if (char.IsWhiteSpace(item))
{
Console.WriteLine();
}else{
Console.Write(item);
}
}
Console.ReadKey();
}
}
}

2、break跳转语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 表达式
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 11; i++)
{
if (i % 4 == 0)
{
break;
}

Console.Write(i);
}
Console.ReadLine();
}
}
}

3、continue主要用于停止当前的迭代语句,结束本次循环,只能用于迭代语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("50以内的奇数:");
for (int i = 1; i <=50; i++)
{
if (i % 2 == 0) {
continue;
}
Console.Write(i+"\t");
}
Console.ReadKey();
}
}
}

4、return语句使用时有两种格式

(1)return;

(2)return 表达式;

return语句只能出现在方法中,当调用方法,回到主函数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入三个整数按回车键确认每个数的输入:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
Console.WriteLine("平均数为{0}", average(a, b, c));
}
Console.ReadLine();

}
static double average(int A,int B,int C) {
return (A + B + C) / 3;
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入三个整数按回车键确认每个数的输入:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
average(a,b,c);
}
Console.ReadLine();
}
static void average(int A,int B,int C) {
Console.WriteLine("平均数为{0}", (A+B+C)/3);
return;
}
}
}

5、goto语句使用格式

goto+标识符标识符标志程序位置的方法

作用:当程序执行到goto语句时,程序会跳转到标识符所标识符所标志的位置

goto语句使用会使代码的易读性下降,在编写程序的时候尽量少用goto语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 表达式
{
class Program
{
static void Main(string[] args)
{
//5的阶层等于?
Console.WriteLine("5的阶乘等于?根据以下选项选择正确答案,回车键确认!");
Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60");
//用户的判定
//标识符标志内容
error:
{
Console.WriteLine("您回答错了!");

}
int option=int.Parse(Console.ReadLine());

switch(option){
case 1:
case 2:
case 3: goto error;
case 4: goto right;
default :
Console.WriteLine("选项不存在,请重新选择");
//break;
goto end;
}
right:
{
Console.WriteLine("恭喜答对了");
}
end: ;
//end: { }
Console.ReadLine();
}
}
}

C#迭代语句、跳转语句--C#基础的更多相关文章

  1. 第一天:javascript实现界面运算及循环语句跳转语句

    文档位置:untitled3(c:\user\dell\WebstormProjects\untitled3\testjstry0.html) 知识点1: 1.新创建html文件,编辑文档如下: &l ...

  2. 【2-24】for循环嵌套,跳转语句,异常语句,穷举法、迭代法

    For循环嵌套与if嵌套相似,是在for中再套for,其结构如下: For(;;) { For(;;){} }经典题型为打印星星例: Console.Write("请输入一个奇数:" ...

  3. 【2017-02-24】循环嵌套、跳转语句、异常语句、迭代穷举、while

    一.循环嵌套 1.格式 for() { for() { } } 2.执行顺序 先执行外边循环进到循环体发现里面的循环,开始执行里面的循环.等到里面的循环执行完毕,再执行外边的循环. 在外面循环第一次, ...

  4. for循环的表达规则,for循环的嵌套,跳转语句;穷举;迭代;异常处理

    for循环的基本格式 for(表达式1:表达式2:表达式3) { 循环体: } for循环的四要素 表达式1就是变量初始化:表达式2就是循环条件:表达式3是状态改变 static void Main( ...

  5. 【2017-2-24】C#循环嵌套,跳转语句,迭代穷举,异常语句,while循环

    循环嵌套 在一个循环体语句中包含另一个循环语句: 99乘法表 ; i <= ; i++) { ; j <= i; j++) { Console.Write(i+"x"+ ...

  6. c#循环语句 for 循环嵌套的练习。还有跳转语句,异常语句,迭代穷举介绍

    先说一下循环嵌套:循环嵌套就是再一个循环里面再放一个循环,也就是说如果没一个循环都循环10次,那么第一个循环是1的时候,嵌套的循环会循环十次.也就是10*10的效果. for 循环语句 主要还是逻辑思 ...

  7. javascript语句——条件语句、循环语句和跳转语句

    × 目录 [1]条件语句 [2]循环语句 [3]跳转语句 前面的话 默认情况下,javascript解释器依照语句的编写顺序依次执行.而javascript中的很多语句可以改变语句的默认执行顺序.本文 ...

  8. C#语句2——循环语句(for穷举、迭代和while循环)

    一.for循环拥有两类: (一).穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元. ...

  9. 房上的猫:for循环,跳转语句与循环结构,跳转语句进阶

    一.for循环 1.定义:  for循环语句的主要作用是反复执行一段代码,直到满足一定条件为止 2.组成部分:  (1)初始部分:设置循环的初始状态  (2)循环体:重复执行的代码  (3)迭代部分: ...

随机推荐

  1. Selenium_Java版本安装及初试

    [环境] ①JDK版本:jdk1.8.0_73 ②Eclipse:jee-mars-4.5.2 ③Selenium:selenium-java-3.5.3 ④GoogleChrome:60 ⑤chro ...

  2. Dagger2 使用全解析

    Dagger2 使用全解析 Dagger是一个注入工具,何为注入,我们要生产一批机器人,每个机器人都有一个控制器,我们可以在机器人内部 new 出一个控制器: class Robot { val co ...

  3. 解决nginx [error] open() "usr/local/nginx/logs/nginx.pid" failed错误

    重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之 后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/us ...

  4. How to delete a VM with snapshots

    A note about error: "cannot delete inactive domain with snapshots" You cannot delete a VM ...

  5. mysql中的coalesce用法

    在mysql中,其实有不少方法和函数是很有用的,这次介绍一个叫coalesce的,拼写十分麻烦,但其实作用是将返回传入的参数中第一个非null的值,比如    SELECT COALESCE(NULL ...

  6. .NET常用第三方库(包)总结

    文章会不定期更新,以下内容均为个人总结,欢迎各位拍砖指正 序列化与反序列化 JSON.NET应该是.NET平台上使用最为广泛的序列化/反序列化包了,ASP.NET和ASP.NET Core中默认序列化 ...

  7. tensorflow import 没找到cudnn库问题解决

    ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory 将cuda下lib64中的l ...

  8. hihoCoder 1513 : 小Hi的烦恼 位运算好题

    思路:考虑第i个同学,第一门课排名比他靠前的同学的集合是S1,第二门课是S2...第五门课是S5,很明显比这个同学每门课程都优秀的同学就是S1&S2&S3&S4&S5, ...

  9. float 与 display:inline-block

    float: 1.会导致高度塌陷 <style type="text/css"> li{ float:left; height:200px; width:200px; ...

  10. Hbase Scan的方法

    public static void main(String[] args) throws IOException { //Scan类常用方法说明 //指定需要的family或column ,如果没有 ...