当前位置: 首页 > news >正文

C# 流程控制(学习笔记3)

1.条件语句

public void Test3(){int a = 1; int b = 2;int c = a + b;if (c > 0){MessageBox.Show(c.ToString());}else if (c == 0){MessageBox.Show("c=0");}else{MessageBox.Show("c<0");}          }

2.选择语句和循环语句

        public void Test4(){int[] a = { 1,2,3,0,-10};for (int i = 0; i < a.Length; i++){switch (a[i]){case 1:MessageBox.Show("1");break;case 2:MessageBox.Show("2");break;case 3:MessageBox.Show("3");break;default:MessageBox.Show("other");break;}}}

3. while

        public void Test5(){int[] a = { 1, 2, 3, 0, -10 };for (int i = 0; i < a.Length; i++){while (a[i] < 0){MessageBox.Show("a<0");}}}

4. do...while

        public void Test6(){int[] a = { 1, 2, 3, 0, -10 };for (int i = 0; i < a.Length; i++){do{MessageBox.Show("a<0");} while (a[i] < 0);}}

5. for...each

遍历一个集合

        public void Test7(){int[] a = { 1, 2, 3, 0, -10 };foreach (var i in a){MessageBox.Show(i.ToString());}}

6. break

跳出循环体

        public void Test8(){int[] a = { 1, 2, 3, 0, -10 };foreach (var i in a){if (i < 1){break; //跳出循环}}}

7. continue

跳过后面的代码,从循环体开始处重新执行

        public void Test9(){int[] a = { 1, 2, 3, 0, -10 };foreach (var i in a){if (i < 1){continue; // 后面的代码不再执行,从循环开始处重新执行。MessageBox.Show(i.ToString()); }}}

7. return

退出函数或方法,有返回值就返回。

        public void Test10(){int[] a = { 1, 2, 3, 0, -10 };foreach (var i in a){if (i < 1){return; // 无返回值}}}
        public int Test11(){int[] a = { 1, 2, 3, 0, -10 };foreach (var i in a){if (i < 1){return i; // 有返回值}}return 0;}
http://www.rkmt.cn/news/49903.html

相关文章:

  • 【提示词工程】PPT做得越用心,听众越想睡觉?
  • win10 右击菜单-子项目过多导致 慢
  • 让PlantUML的布线更加简洁的一个方法
  • java实现Word转Pdf(Windows、Linux通用) - IT
  • docker: ubuntu 24.04安装docker
  • 探索“AI元人文”构想:致学者、技术专家与爱好者的一篇导言
  • 实用指南:计算机视觉(opencv)——基于 MediaPipe 人体姿态检测
  • 《团队协作:如何高效进行代码审查》
  • 英国留学中介避坑指南:新通/厚仁领跑,G5录取率93%+的靠谱留学机构清单
  • C# 数据类型与运算符(学习笔记2)
  • 《移动开发:Flutter与React Native的对比》‌
  • 超参数调优:Grid Search 和 Random Search 的实战对比
  • 实用指南:26秋招三分钟快速背诵一个八股-Spring中Bean的生命周期
  • FreeSWITCH使用RNNoise进行实时通话降噪
  • Spring AI Alibaba 项目源码学习(六)-Agent Framework 工作原理与使用
  • 采用 OpenCASCADE 提取布尔运算后平面图形的外轮廓
  • 题解:AtCoder ARC209D A_A_i
  • Kotlin Coroutines
  • 我的标题
  • Java Benchmark使用
  • Go-秘籍-全-
  • Kotlin中的flow、stateflow、shareflow之间的区别和各自的功能 - 教程
  • 非离散网络流——P3347 [ZJOI2015] 醉熏熏的幻想乡
  • Dark Side of the Moon
  • 图片合集
  • 升幂引理(LTE)
  • OpenWrt路由的端口映射问题
  • 解码IPC-管道与信号
  • How-to-extract-text-from-PDF-Image-files-OCR-CarlZeng
  • 升鲜宝供应链管理系统、各端的访问地址及nginx 真实的配置方法