程序的执行一共有三类形式:顺序结构、分支结构、循环结构。顺序结构指所有的代码按照定义的先后执行,就好比在之前主方法中所编写的代码一样,按照定义顺序一行行执行。
分支结构
分支结构指的是进行逻辑判断,但满足某些条件的时候才会执行某些语句.而对于分支结构有两类:if分支结构、switch分支结构。if的分支结构的基础语法如下:
if(布尔表达式) {
条件满足时的代码
}
if(布尔表达式) {
条件满足时的代码
}
else {
条件不满足时的代码
}
if(布尔表达式) {
条件满足时的代码
}
else if{
条件满足时的代码
}
[else {
条件都不满足时的代码
}]
范例: 使用if语句
public class TestDemo {
public static void main(String[] args) {
int age = 18 ;
if(age >= 18) {
System.out.println("姑娘大了,可以嫁人了。") ;
}
}
}
范例: 使用if else语句
public class TestDemo {
public static void main(String[] args) {
int age = 16 ;
if(age >= 18) {
System.out.println("姑娘大了,可以嫁人了。") ;
}
else {
System.out.println("孩子岁数小,再长长。");
}
}
}
范例: 多条件判断
public class TestDemo {
public static void main(String[] args) {
int age = 16 ;
if(age <= 18) {
System.out.println("未成年呢,还年轻。") ;
}
else if(age > 18 && age < 22){
System.out.println("学习呢,不用想其它。");
}
else {
System.out.println("随便折腾吧!");
}
}
}
使用if…else这样的的语法可以实现条件的判断,但是如果要想进行多数值内容的判断,那么就可以使用switch完成,但是需要注意的是switch随着JDK版本的变化支持的数据类型也在变化。
- 最初的数据类型支持:int、char;
- 从JDK1.4开始支持了枚举(enu);
- 从JDK1.7开始支持了String。
switch(数字 | 枚举 | 字符 | 字符串) {
case 内容1:{
内容满足时的执行的语句;
[break;]
}
case 内容2:{
内容满足时的执行的语句;
[break;]
}
......
[default:{
都不满足时执行的语句;
[break;]
}]
}
switch这种开关语句有一个重要的特点:如果你在编写case的时候没有加上break,则会在满足的case之后一直执行到break或全部结束。
范例: 观察switch
public class TestDemo {
public static void main(String[] args) {
int ch = 1;
switch(ch) {
default:
System.out.println("没有条件时被满足");
break;
case 1:
System.out.println("内容为1。");
break;
case 2:
System.out.println("内容为2。");
break;
}
}
}
从JDK1.7开始switch支持了字符串的操作。
public class TestDemo {
public static void main(String[] args) {
String info = "hello";
switch(ch) {
case "hello":
System.out.println("你好");
break;
case "word":
System.out.println("我的");
break;
default:
System.out.println("其它");
break;
}
}
}
循环结构
循环结构指的就是某几行代码被重复执行的操作形式。循环一般有两类循环:while循环、for循环。
- while循环
循环的初始化内容
while(循环的结束条件判断) {
循环语句;
修改循环结束条件判断;
}
循环的初始化内容
do {
循环语句;
修改循环结束条件判断;
}while(循环的结束条件判断)
使用while循环的最大特点在于:如果判断条件不满足就一次都不执行,而使用do…while的特点是即使判断条件不满足也会执行一次
范例: 实现1 ~ 100的累加
public class TestDemo {
public static void main(String[] args) {
int num = 1;
int result = 0;
while (num <= 100) {
result += num;
num++;
}
System.out.println(result);
}
}
范例: 使用do…while实现累加处理
public class TestDemo {
public static void main(String[] args) {
int num = 101;
int result = 0;
do {
result += num;
num++;
} while (num <= 100);
System.out.println(result);
}
}
以后的开发中对于do…while基本不使用,基本上使用的就是while循环。
- for循环
for(循环初始化条件;循环结束判断;修改循环条件) {
循环体代码;
}
范例: 使用for循环实现1 ~ 100的累加
public class TestDemo {
public static void main(String[] args) {
int result = 0;
//1. 循环初始化:int i = 0;
//2. 判断循环条件:i <= 100;
//4. 循环条件变更:i++
//5. 判断循环条件
for(int i = 0; i <= 100; i++) {
result += i; //3. 循环体操作
}
System.out.println(result);
}
}
但是千万记住一点,对于以下的循环结构别用:
public class TestDemo {
public static void main(String[] args) {
int result = 0;
int i = 0;
for(; i <= 100;) {
result += i;
i++;
}
System.out.println(result);
}
}
循环使用原则:
-
对于不知道循环次数,但是知道循环条件的使用while循环;
-
如果已经明确知道循环次数,则使用for循环;
##循环控制
在进行循环处理的时候有两个关键字:continue、break。一般这样的语句都会结合if判断一起使用。
- continue:执行到此语句时将跳过循环体的剩余部分。
public class TestDemo {
public static void main(String[] args) {
for(int x = 0; x < 10; x++) {
if(x == 3) {
continue; //执行此语句之后循环体的后面代码不执行
}
System.out.println("x = " + x);
}
}
}
- break:跳过整个循环
public class TestDemo {
public static void main(String[] args) {
for(int x = 0; x < 10; x++) {
if(x == 3) {
break;
}
System.out.println("x = " + x);
}
}
}
其它的语言里面有一种goto的功能,这种功能一般不会出现在Java中,而且也没有这样的关键字,不过可以利用continue实现与之一样的功能。
public class TestDemo {
public static void main(String[] args) {
mp: for(int x = 0; x < 5; x++) {
for(int y = 0; y < 3; y++) {
if(x > 2) {
continue mp;
}
System.out.println("x = " + x + " y = " + y);
}
}
}
}
正常人不写此类操作,而且很少去使用break、continue。
循环嵌套
循环语句本身是可以进行嵌套使用的,但是从现在的开发来讲,这种嵌套的操作已经少了。
范例: 打印乘法口诀表
public class TestDemo {
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(); //换行
}
}
}
范例: 打印三角形
public class TestDemo {
public static void main(String[] args) {
int line = 5;
for(int x = 0; x < line; x++ ) { //控制行
for(int y = 0; y < line - x; y++) { //空格
System.out.print(" ");
}
for(int y = 0; y < x; y++) {
System.out.print("* ");
}
System.out.println();
}
}
}
这些程序逻辑训练不属于本系列课程。
总结
知道次数使用for,不知道次数知道结束条件使用while。