C#迭代语句、跳转语句--C#基础
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#基础的更多相关文章
- 第一天:javascript实现界面运算及循环语句跳转语句
文档位置:untitled3(c:\user\dell\WebstormProjects\untitled3\testjstry0.html) 知识点1: 1.新创建html文件,编辑文档如下: &l ...
- 【2-24】for循环嵌套,跳转语句,异常语句,穷举法、迭代法
For循环嵌套与if嵌套相似,是在for中再套for,其结构如下: For(;;) { For(;;){} }经典题型为打印星星例: Console.Write("请输入一个奇数:" ...
- 【2017-02-24】循环嵌套、跳转语句、异常语句、迭代穷举、while
一.循环嵌套 1.格式 for() { for() { } } 2.执行顺序 先执行外边循环进到循环体发现里面的循环,开始执行里面的循环.等到里面的循环执行完毕,再执行外边的循环. 在外面循环第一次, ...
- for循环的表达规则,for循环的嵌套,跳转语句;穷举;迭代;异常处理
for循环的基本格式 for(表达式1:表达式2:表达式3) { 循环体: } for循环的四要素 表达式1就是变量初始化:表达式2就是循环条件:表达式3是状态改变 static void Main( ...
- 【2017-2-24】C#循环嵌套,跳转语句,迭代穷举,异常语句,while循环
循环嵌套 在一个循环体语句中包含另一个循环语句: 99乘法表 ; i <= ; i++) { ; j <= i; j++) { Console.Write(i+"x"+ ...
- c#循环语句 for 循环嵌套的练习。还有跳转语句,异常语句,迭代穷举介绍
先说一下循环嵌套:循环嵌套就是再一个循环里面再放一个循环,也就是说如果没一个循环都循环10次,那么第一个循环是1的时候,嵌套的循环会循环十次.也就是10*10的效果. for 循环语句 主要还是逻辑思 ...
- javascript语句——条件语句、循环语句和跳转语句
× 目录 [1]条件语句 [2]循环语句 [3]跳转语句 前面的话 默认情况下,javascript解释器依照语句的编写顺序依次执行.而javascript中的很多语句可以改变语句的默认执行顺序.本文 ...
- C#语句2——循环语句(for穷举、迭代和while循环)
一.for循环拥有两类: (一).穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元. ...
- 房上的猫:for循环,跳转语句与循环结构,跳转语句进阶
一.for循环 1.定义: for循环语句的主要作用是反复执行一段代码,直到满足一定条件为止 2.组成部分: (1)初始部分:设置循环的初始状态 (2)循环体:重复执行的代码 (3)迭代部分: ...
随机推荐
- 网络编程基础+UDP的实现
网络地址分类(32位网络地址) A类地址:第一个字节为网络地址,其他3个字节主机地址,第一字节的最高位固定为0 从1.0.0.1------126.255.255.255 B类地址:第一字节和第二字节 ...
- python数据分析工具包(3)——matplotlib(一)
前两篇文章简单介绍了科学计算Numpy的一些常用方法,还有一些其他内容,会在后面的实例中学习.下面介绍另一个模块--Matplotlib. Matplotlib是一个Python 2D绘图库,试图让复 ...
- PHP系统左侧菜单栏的管理与实现
在日常的开发工作中,面对后台的日益增长的业务,以及后期业务的迭代开发,通常会选择添加菜单栏的形式来扩充业务功能,同样日益增长的后台菜单选项也为我们后期的维护,产生了一定的困难性.为此我总结出自己关于左 ...
- [APIO2015]雅加达的摩天楼
Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 " ...
- laypage 使用
最近发现一个特别好用的前端分页插件,分享一下 <!doctype html> <html> <head> <meta charset="utf-8& ...
- MySQL数据库基础(四)(子查询与链接)
1.子查询简介 其中,所谓的"外层查询"并不是指"查找",指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称 ...
- .net core 2.0学习笔记(二):部署到Windows和Liunx系统
.Net Core最大的亮点就是跨平台了,下面介绍下在Windows下和Liunx下的部署. 首先发布项目文件,点击网站项目右键 发布: 从下图发布的文件图片可以看出,不像以前bin目录下有很多dll ...
- Centos下_MysqL5.7在使用mysqldump命令备份数据库报错:mysqldump: Got error: 1449: The user specified as a definer ('fk_system'@'localhost') does not exist when using LOCK TABLES
在阿里云服务器增加一个shell脚本定时备份数据库脚本执行任务时,测试性的执行了备份命令,如下 [root@iZ2ze503xw2q1fftv5rhboZ mysql_bak]# /usr/local ...
- 关于C/S框架网单表绑定,查询
这种绑定暂时支持单表,并且不支持主键自增长!保存,删除,查看,修改用框架现成的. 1.先生成tb.bll.dal三个类.框架有生成工具,在debug文件里面有个叫CSFramework.Tools.C ...
- Create小程序
我有时候喜欢直接用命令行创建.编译.执行java文件, 每次创建一个文件都要新建一个.java文件,然后再编辑.java文件加入类名,主函数…… 这些流程我有点厌倦,于是就编写了一个超级简单的自动创建 ...