7、程序逻辑控制
2018-01-23 14:10
127 查看
1、本次的预计讲解的知识
1、判断语句的使用;2、循环语句与循环控制、
2、具体内容
程序逻辑主要分为三种逻辑结构:顺序结构、分支结构、循环结构。其中顺序结构最好理解,所有的到吗都是由前向后执行的、,但是需要提醒的时候,有些时候顺序是以所在的“{}”为界限的。2.1、分支结构
分支结构就是一种判断结构。对于分支结构有两类语法支持:if、seitch。1、if分支语句:此类语句有多种定义形式: | ||
if | if...else | If...else if..else if...else |
if (布尔表达式) { 程序语句 } | if (布尔表达式) {//条件满足时执行 程序语句 } else {//条件不满足时执行 程序语句 } | if (布尔表达式1) {//条件满足时执行 程序语句 }else if (布尔表达式2) { 程序语句 } ... else {//条件不满足时执行 程序语句 } |
public class TextDemo { public static void main(String[] args) { double score = 90.0 ; if (score > 60.0) { System.out.println(及格); } } } |
范例:出现不满足的判断
public class TextDemo { public static void main(String[] args) { double score = 10.0 ; if (score < 60.0) { //条件满足 System.out.println("不及格"); } else if (score >= 60 && score <= 90) { System.out.println("中等成绩"); } else if (score > 90 && score <= 100) { System.out.println("优秀成绩"); } else { //条件都不满足 System.out.println("你家的成绩很奇怪!!!!!"); } } } |
switch (整数 | 字符 | 枚举 | String) { case 内容 : { 内容满足时执行 ; [break ;] } case 内容 : { 内容满足时执行 ; [break ;] } case 内容 : { 内容满足时执行 ; [break ;] } [default : { 内容不满足时执行 ; [break ;] }] } |
范例:使用switch判断
public class TextDemo { public static void main(String[] args) { int ch = 2 ; switch (ch) { case 2: { System.out.println("内容是2"); break ; } case 1: { System.out.println("内容是1"); break ; } case 3: { System.out.println("内容是3"); break ; } default: { System.out.println("没有内容"); } } } } |
从JDK1.7开始switch终于在这么多年的呼唤下出下了字符串的判断。
public class TextDemo { public static void main(String[] args) { String str = "HELLO" ; switch (str) { case "HELLO": { System.out.println("HELLO"); break ; } case "hello": { System.out.println("hello"); break ; } case "mdl": { System.out.println("mdl"); break ; } default: { System.out.println("没有内容"); } } } } |
2.2、循环结构
当某代码需要一直重复执行的时候,就可以使用循环结构实现控制,对于循环结构有两种循环:while循环、do while循环。1、while循环:分为两种形式语法:
while循环 | do while循环 |
while (判断条件) { 循环语句 ; 修改循环结束条件 ; } | do { 循环语句 ; 修改循环结束条件 ; } while (循环判断) ; |
范例:实现1...100的累加——使用while循环
public class TextDemo { public static void main(String[] args) { int sum = 0 ; //保存总合 int current = 1 ; //循环的初始化条件 while (current <= 100) { //循环结束条件 sum += current ; //累加 current++ ; } System.out.println(sum); } } |
范例:使用do while循环
public class TextDemo { public static void main(String[] args) { int sum = 0 ; //保存总合 int current = 1 ; //循环的初始化条件 do { //循环结束条件 sum += current ; //累加 current++ ; } while (current <= 100); System.out.println(sum); } } |
以后的开发之中请彻底忘掉do while,根本就不会用。
2、for循环,语法:
for (循环初始化条件 ; 循环判断 ; 循环条件变更) { 循环语句 ; } |
public class TextDemo { public static void main(String[] args) { int sum = 0 ; //保存总合 for (int current = 0 ; current <= 100 ; current ++) { //循环结束条件 sum += current ; } System.out.println(sum); } } |
public class TextDemo { public static void main(String[] args) { int sum = 0 ; //保存总合 int current = 0 ; for ( ; current <= 100 ; ) { //循环结束条件 sum += current ; current ++ ; } System.out.println(sum); } } |
l 如果不知道循环次数,但是不知道循环结束条件的时候使用while循环
l 如果已经明确知道循环次数了,使用for循环。
对于虚循环而言,是属于初学者刚刚开始接触程序最麻烦的一个环节,但是对于循环本身也是可以进行嵌套的,循环继续嵌套其它的循环。
范例:模拟抽烟
public class TextDemo { public static void main(String[] args) { int max = 9 ; for ( int x = 1; x < max ; x++ ) { //循环结束条件 System.out.print("开始抽烟" + x +"根烟,抽了"); for (int y = 1 ; y <= 20 ; y++ ) { System.out.print("嘬、"); } System.out.println(); //换行 } System.out.println("呵呵"); } } |
public class TextDemo { public static void main(String[] args) { for ( int x = 1; x <= 9 ; x++ ) { for (int y = 1 ; y <= x ; y++ ) { System.out.print(x +"*"+ y +"="+ x*y +"\t"); } System.out.println(); //换行 } } } |
2.3、循环控制
循环控制一共有两种语句:continue(退出本次循环)、break(退出整个循环),此类语句要与判断语句结合使用。范例:观察continue
public class TextDemo { public static void main(String[] args) { for ( int x = 0; x < 10 ; x++ ) { if (x == 3) { continue ; //之后代码不执行,直接结束本次循环 } System.out.println("x = " + x) ; } } } |
public class TextDemo { public static void main(String[] args) { for ( int x = 0; x < 10 ; x++ ) { if (x == 3) { break ; //退出真个循环 } System.out.println("x = " + x) ; } } } |
3、总结
1、循环结构需要反复多写几次,因为你写的代码有可能出现死循环,指的是每次循环时的结束条件没有改变。2、while用于不确定次数但是知道结束条件的循环,而for适合与明确知道循环次数的循环;
3、continue、break都需要与if语句结合。
相关文章推荐
- 逻辑运算_三元运算符_程序流程控制
- 从程序的控制逻辑看线程的三种应用模式
- ABAP-程序处理逻辑控制(stop,exit,return,check,reject)
- java学习路线__基础入门向__程序逻辑控制__6
- ABAP-程序处理逻辑控制(stop,exit,return,check,reject)
- Java-06-程序逻辑控制
- Java程序逻辑控制
- 笔记5:java_程序逻辑控制
- Java程序逻辑控制,Java循环
- Java程序逻辑控制
- Thinking in java(Chapter3 控制程序流程)
- 微信小程序登录态控制深入分析
- scala 开发入门(2)-- 程序控制结构
- 如何在程序中部署界面和业务逻辑.
- C# AssemblyInfo.cs控制EXE程序版本信息
- 【编程札记】_程序的逻辑
- 【连载】Java学习系列(004)——程序逻辑-2(分支和循环)
- 用Windbg调试一个Windows自带扫雷程序的逻辑bug
- 角度舵机控制手爪开合测试程序
- CMDOW 一个CMD命令行下 隐藏、禁用窗口控制程序