1、简介
控制流程(也称为流程控制)意指在程序运行时,个别的指令(或是陈述、子程序)运行或求值的顺序。即:程序运行时,对代码执行的控制。
2、分类
- 复合语句
- 顺序语句
- 条件语句
- 循环语句
3、实现
3.1、复合语句
Java语言的复合语句是以整个块区为单位的语句,又称块语句。复合语句由“{”开始,闭括号“}”结束。如:类或方法定义时的类体或方法体
复合语句为局部变量创建了一个作用域,该作用域为程序的一部分,在该作用域中某个变量被创建并能够被使用,如果在某个变量的作用域外使用该变量,则会发生错误。并且复合语句中可以嵌套复合语句。
3.2、顺序语句
按照代码的写作顺序,从上到下依次执行
public class Hello {
public static void main(String[] args) {
String s = "hello world,你好,世界!";
String x = "1+2=";
String str1 = "Hello ";
String str2 = "World";
System.out.println(s);
System.out.println(x + (1 + 2));
System.out.println(str1);
System.out.println(str2);
}
}
3.3、条件语句
(1)if
语法:if(条件){语句 }
public class Contiue {
public static void main(String[] args) {
int i = 10;
int m = 0;
if (i < m) {
System.out.print("m大于i!!");
}
if (m < i) {
System.out.print("i大于m!!");
}
}
}
(2)if…else…
语法:if(表达式){
若干语句
} else{若干语句
}
public class Contiue {
public static void main(String[] args) {
int a = 11;
if (a > 0) {
System.out.println("a的为正整数");
}else {
System.out.println("a的为不为正整数");
}
}
}
(3)if…else if …else if…
语法:if(表达式1){
语句序列1
}else if(表达式2){
语句序列2}
,,,
else if(表达式n){
语句序列n
}
public class Contiue1 {
public static void main(String[] args) {
int x = 59;
if (x > 60) {
System.out.print("你考试及格了");
} else if (x > 90) {
System.out.print("你真棒!!");
} else {
System.out.print("继续努力!!");
}
}
}
(4)switch多分支语句
语法:switch(表达式)
{ case 常量1:
语句块1
[break;]
…
case 常量n:
语句块n
[break;]
default:
语句块 n+1;
[break;]
}
public class Contiue4 {
public static void main(String[] args) {
String a = "天晴天阴下雪下雨";
switch ("天") {
case "天晴":
System.out.println("今天天气不错!心情也舒畅了不少");
break;
case "天阴":
System.out.println("今天天阴了!心情还好吧");
break;
case "下雪":
System.out.println("今天下雪了!心也有点冷");
break;
case "下雨":
System.out.println("今天下雨了!心情不好啊");
break;
default:
System.out.println("心情糟糕透了");
break;
}
}
}
3.4、循环语句
(1)while循环语句
语法:while(条件表达式)
{执行语句
}
//1+2+3+....+10=
public class Contiue2 {
public static void main(String[] args) {
int i = 10;
int m = 0;
while (i > 0) {
m = m + i;
i--;
}
System.out.print("m=" + m);
}
}
(2)do…while 循环语句(“{}”中程序至少被执行一次)
语法:do{
执行语句
}
while(条件表达式)
public class Contiue2 {
public static void main(String[] args) {
int x = 60;
do {
System.out.println("x=" + x);
x--;
} while (x == 40);
}
}
(3)for语句
语法:for(表达式1;表达式2;表达式3)
{
语句序列
}
public class Contiue2 {
public static void main(String[] args) {
// for循环
int sum = 0;
for (int i = 1; i <= 100; i += 2) {
sum = sum + i;
}
System.out.print("输出1到100之间奇数之和:" + sum + "\n");
}
}
(4)foreach 语句(for语句的特殊简化版本,不是关键字,常用于遍历数组)
语法:for(元素变量x:遍历对象 obj){
引用x的语句
}
public class Contiue2 {
public static void main(String[] args) {
// foreach循环
int a[] = {
1, 2, 3, 4, 45, 5, 6, 6, 7 };
System.out.println("一维数组元素为:");
for (int x : a) {
// foreach中x指引用的变量,a要循环的数组,最后输出x
System.out.println(x);
}
}
}
(5)循环控制break和continue
-
break语句
作用:跳出当前循环体,中断当前循环。
-
continue语句
作用:跳过本次循环继续执行下次循环。
public class Contiue3 {
public static void main(String[] args) {
// break
int a = 1;
while (a > 0) {
a = a + 2;
System.out.println(a);
if (a == 79) {
break;
}
}
System.out.println("---end----" + "\n");
// continue
for (int a1 = 2; a1 < 100; a1 += 2) {
if (a1 % 5 == 0) {
continue;
}
System.out.println(a1);
}
System.out.println("----over-----");
}
}