java程序逻辑控制
顺序结构、选择(分支)结构、循环结构
一个入口一个出口
顺序结构
顺序结构的代码就是程序由上到下依次执行
流程图演示:
/**
* 顺序结构演示
* 顺序结构,程序运行后,由上到下开始执行,执行完最后一条语句,程序结束
* @author hgw
* @date 2022年01月16日13:02
**/
public class Demo {
public static void main(String[] args) {
System.out.println("第一条语句");
System.out.println("第二条语句");
System.out.println("第三条语句");
System.out.println("...");
System.out.println("第n条语句");
}
}
- 选择(分支)结构
分支结构可以理解中文为
如果…否则…
/**
* 形式一:
* if(布尔表达式) {
* 布尔表达式为true
* 条件满足时执行;
* }
* 选择(分支)结构
* @author hgw
* @date 2022年01月16日13:52
**/
public class Demo2 {
public static void main(String[] args) {
int count = 2;
System.out.println("程序开始");
if (count > 0) { //布尔表达式
System.out.println("符合条件,布尔表达式为true");
}
}
}
/**
* 形式二:
* if(布尔表达式) {
* 布尔表达式为true
* 条件满足时执行;
* } else {
* 不满足上面if判断
* 条件不满足时执行;
* }
* 练习介绍:
* 使用if...else结构进行条件判断,符合条件则 变量sum进行累加,
* 不符合条件直接输出count
* 选择(分支)结构
* @author hgw
* @date 2022年01月16日14:39
**/
public class Demo3 {
public static void main(String[] args) {
int count = 1;
int sum = 0;
if (count > 0) {
sum += count;
} else {
System.out.println(count);
}
System.out.println("结果为:" + sum);
}
}
/**
* 形式三:
* if(布尔表达式) {
* 布尔表达式为true时执行
* 条件满足时执行;
* } else if(布尔表达式){
* 布尔表达式为true时执行
* } else if(布尔表达式){
* 布尔表达式为true时执行
* } else {
* 不满足上面所有if判断时执行
* 条件不满足时执行;
* }
* 练习介绍:
* 使用if(){...}else if(){...}else{}结构进行,根据变量星期判断今天是星期几
* week = 1则输出今天是星期:1
* week = 2则输出今天是星期:2
* .
* .
* .
* week = 7则输出今天是星期:2
* week为其他值,以上if判断没有为true不符合条件直接输出日期有误今天是星期8
* 选择(分支)结构
* @author hgw
* @date 2022年01月16日17:27
**/
public class Demo4 {
public static void main(String[] args) {
int week = 2;
if (week == 1){
System.out.println("今天是星期:" + week);
} else if (week == 2){
System.out.println("今天是星期:" + week);
} else if (week == 3){
System.out.println("今天是星期:" + week);
} else if (week == 4){
System.out.println("今天是星期:" + week);
} else if (week == 5){
System.out.println("今天是星期:" + week);
} else if (week == 6){
System.out.println("今天是星期:" + week);
} else if (week == 7){
System.out.println("今天是星期:日");
}else {
System.out.println("日期有误");
System.out.println("星期8");
}
}
}
switch开关
/**
* switch (表达式){
* case 内容:
* 满足内容时执行的语句;
* break;
* case 内容:
* 满足内容时执行的语句;
* break;
* default:
* 以上条件都不满足时执行
* }
* 表达式中存放的值:
* byte,short,int,char jdk5以后可以使枚举、jdk7以后可以使字符串。
* case可以有多个
* 内容为常量
* @author hgw
* @date 2022年01月16日17:54
**/
public class Demo5 {
public static void main(String[] args) {
int number = 1;
switch (number){
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
default:
System.out.println("其他数!");
}
}
}
Switch语句的注意事项:
- case后面只能是常量,不能是变量,而且,多个case后面的 值不能相同
- default可以省略吗?
可以省略但不建议,因为他的作用是对不正确的情况给出提示。
特殊情况: case可以把值固定 - break可以省略吗?
可以省略,但结果不会是我们想要的
会出现一个现象:case穿透 所以不建议省略break - default一定要在最后吗?
不一定,可以在任意位置,但建议在最后 - switch语句的结束条件
a)、遇到break
b)、执行到末端
面试题:
byte可以做switch的表达式吗?
可以。
long可以做switch的表达式吗?
不可以。
string可以做switch的表达式吗?
jdk7以后可以使字符串结构
- 循环结构
循环包括while循环、do while循环、for循环三种
while循环和for循环的区别
使用区别:如果你想在循环结束后,继续使用控制条件的那个变量,
用while循环,否则用for循环。
不知道用for循环因为变量及早的内存中消失,可以提高内存的使用效率。 其实还有一种场景的理解:
如果是一个范围的,用for循环非常明确。
如果是不明确要做多少次,用while循环较为合适
循环语句的区别:
do…while循环至少执行一次循环体。
而for,while循环必须判断条件是否成立,然后决定是否执行循环体语句。
在使用循环语句的过程当中,
优先考虑for,其次while,最后do…while
在使用循环的过程中注意死循环:
一定要注意控制条件语句控制的那个变量的问题,不要弄丢了,否则就容易死循环
案例使用三种循环结构求1 + 2 + 3 + … + 100
/**
* while(循环条件){
* 循环体
* 设置循环条件
* }
* @author hgw
* @date 2022年01月16日18:26
**/
public class Demo6 {
public static void main(String[] args) {
int number = 0 ;
int sum = 0 ;
while (number <= 100) {
sum += number;
number++;
}
System.out.println(sum);
}
}
/**
* do{
* 循环体
* 循环步长
* }while(循环条件)
* @author hgw
* @date 2022年01月16日18:43
**/
public class Demo7 {
public static void main(String[] args) {
int number = 0 ;
int sum = 0 ;
do {
sum += number;
number++;
}while (number <= 100);
System.out.println(sum);
}
}
/**
* 语法结构:
* for (初始值;循环条件;步长) {
* 循环体
* }
* @author hgw
* @date 2022年01月16日18:44
**/
public class Demo8 {
public static void main(String[] args) {
int sum = 0 ;
for (int i = 0; i < 101; i++) {
sum += i ;
}
System.out.println(sum);
}
}
循环嵌套
一个循环的循环体是另一个循环。比如for循环里面还有一个for循环
比如九九乘法表:
/** * @author hgw * @date 2022年01月16日19:04 **/ public class Demo9 { public static void main(String[] args) { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + " = " + i*j + "\t" ); } System.out.println(); } } }
控制循环结构
控制跳转语句:
break:中断(结束循环)
/** * @author hgw * @date 2022年01月16日19:12 **/ public class Demo10 { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(i); //如果i == 5则结束循环 if (i == 5 ) { break; } } } }
continue:继续(跳过本次循环,进入下次循环)
/** * @author hgw * @date 2022年01月16日19:14 **/ public class Demo11 { public static void main(String[] args) { for (int i = 0; i < 10; i++) { //打印0-9的数,遇到5则跳过循环 System.out.println(i); if (i == 5 ) { continue; } } } }
return:返回
break:中断的意思,提前退,java里的退出语句
使用场景:
A:switch语句中
B:循环语句中 (循环语句中加入了if判断的情况) 注意:离开上面两个场景无意义 如何使用呢?
A:跳出单层循环
B:跳出多层玄幻
要想实现这个效果,就必须知道一个东西,带标签的语句 格式: 标签名:语句
给语句起名
如下代码:
/** * @author hgw * @date 2022年01月16日19:16 **/ public class Demo12 { public static void main(String[] args) { c:for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + " = " + i*j + "\t" ); if (j == 4){ break c; } } System.out.println(); } } }
输出结果
11 = 1
12 = 2 22 = 4
13 = 3 23 = 6 33 = 9
14 = 4 24 = 8 34 = 12 44 = 16
Process finished with exit code 0