Java程序逻辑控制

程序逻辑主要分为三种逻辑结构:顺序结构、分支结构、循环结构。

顺序结构的所有的代码都是从前向后执行的。有些时候顺序是由“{}”为界限的。


一、分支结构

分支结构是一种判断结构,有两类语法支持:if、switch。

1、if分支语句:此类语句有多种定义形式。

if

if…esle…

if…esle…if…esle…

if(布尔表达式){

     程序语句

}

if(布尔表达式){

     //条件满足时执行

     程序语句

}else{

     //条件不满足时执行

     程序语句

}

if(布尔表达式1){

       //条件满足时执行

       程序语句

}else if(布尔表达式2){

       程序语句

}…else{

      //条件不满足时执行

     程序语句

}

范例:进行判断

public class TestDemo {
	public static void main(String args[]) {
		double score = 90.0;
		if (score > 60.0) {
			System.out.println("及格了!");
		}
	}
}

在此操作里面,如果条件不满足发现就没有执行了。

范例:出现不满足的判断

public class TestDemo {
	public static void main(String args[]) {
		double score = 10.0;
		if (score > 60.0) {// 条件判断满足
			System.out.println("及格了!");
		} else {// 条件判断不满足
			System.out.println("小白的成绩!");
		}
	}
}

范例:进行多条件判断

public class TestDemo {
	public static void main(String args[]) {
		double score = 10.0;
		if (score < 60.0) {// 条件判断满足
			System.out.println("不及格!");
		} else if (score >= 60.0 && score <= 90) {
			System.out.println("中等成绩!");
		} else if (score >= 90.0 && score <= 100) {
			System.out.println("优秀成绩!");
		} else {// 条件判断都不满足
			System.out.println("小白的成绩!");
		}
	}
}

整个if的判断都可以编写布尔表达式,但是switch的判断不能够使用布尔表达式,它最早的时候只能支持整数或者字符的判断,从JDK1.5开始支持了枚举判断,在JDK1.7支持了String的判断。


2、switch分支语句

语法:其中break语句表示停止case的执行

switch(整数|字符|枚举|String){

      case 内容:{

        内容满足时执行;

break;

}

      case 内容:{

        内容满足时执行;

break;

}

      case 内容:{

        内容满足时执行;

break;

}

……

default:{

  内容都不满足时执行;

break;

}

}

范例:使用switch判断

public class TestDemo {
	public static void main(String args[]) {
		int ch = 1;
		switch (ch) {
		case 2: {
			System.out.println("内容是2");
			break;
		}
		case 1: {
			System.out.println("内容是1");
			break;
		}
		case 3: {
			System.out.println("内容是3");
			break;
		}
		case 4: {
			System.out.println("内容是4");
			break;
		}
		default: {
			System.out.println("没有匹配");
			break;
		}
		}
	}
}

switch不能够判断布尔表达式,只能够判断内容。

范例:判断字符

public class TestDemo {
	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;
		}
		default: {
			System.out.println("没有匹配");
			break;
		}
		}
	}
}

程序是区分大小写关系的。


二、循环结构

当某段代码需要重复执行的时候,就可以使用循环结构来实现控制。

对于循环结构有两种循环:while循环、for循环。

1、while循环

两种形式语法:

while循环

do…while循环

while(循环判断){

循环语句;

修改循环结束的条件;

}

do{

循环语句;

修改循环结束的条件;

}while(循环判断);

所有的循环语句里面都必须有循环的初始化条件,每次循环的时候都要去修改这个条件,以判断循环是否结束。

范例:实现1 ~ 100的累加(使用while循环)

public class TestDemo {
	public static void main(String args[]) {
		int sum = 0;// 保存总和
		int current = 1;// 循环的初始化条件
		while (current <= 100) {// 循环结束条件
			sum += current;// 累加
			current++;
		}
		System.out.println(sum);
	}
}

while循环属于先判断条件而后执行。

范例:使用do…while循环

public class TestDemo {
	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循环指的是先执行一次,而后再进行判断,即:不管循环条件是否满足,一定会至少执行一次。

一般do…while循环不会用。


2、for循环

语法:

for(循环初始化条件;循环判断;循环条件变更){

        循环语句;

}

范例:使用for循环实现1 ~ 100累加

public class TestDemo {
	public static void main(String args[]) {
		int sum = 0;// 保存总和
		for (int current = 1; current <= 100; current++) {
			sum += current;
		}
		System.out.println(sum);
	}
}

有些时候for循环也会变成以下形式编写(不建议使用)

public class TestDemo {
	public static void main(String args[]) {
		int sum = 0;// 保存总和
		int current = 1;
		for (; current <= 100;) {
			sum += current;
			current++;
		}
		System.out.println(sum);
	}
}

疑问?现在给出了两种循环的语法,那么开发里面用哪种?

  • 如果不知道循环次数,但是知道循环结束条件的时候使用过while循环;
  • 如果已经明确知道循环次数,使用for循环。

对于循环本身也是可以进行嵌套的,循环里面继续嵌套其它的循环。

范例:模拟抽烟

public class TestDemo {
	public static void main(String args[]) {
		int max = 9;
		for (int x = 0; x < max; x++) {
			System.out.print("开始抽烟" + x + "根烟");
			for (int y = 1; y <= 20; y++) {
				System.out.print("嘬!");
			}
			System.out.println();
		}
	}
}

范例:打印乘法口诀表

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();
		}
	}
}

三、循环控制

循环控制一共有两种语句:

  • continue(退出本次循环);
  • break(退出整个循环)。

此类语句一定要与判断语句结合使用。

范例:观察continue

public class TestDemo {
	public static void main(String args[]) {
		for (int x = 1; 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 = 1; x < 10; x++) {
			if (x == 3) {
				break;//退出整个循环
			}
			System.out.println("x=" + x);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/L_zhai/article/details/81143939