分支结构
- if …… else ……
- if 后边的小括号中, 要传递一个 boolean 类型的值(boolean 类型值的产出有 : 方法、逻辑运算符、比较运算符 )
语法结构
- if 后边的小括号中, 要传递一个 boolean 类型的值(boolean 类型值的产出有 : 方法、逻辑运算符、比较运算符 )
if(布尔表达式){
//如果布尔表达式的值为true,执行这里代码
}else{
//如果布尔表达式的值为false,执行这里代码
}
示例
public class Branch {
public static void main(String args[]){
int x = 30;
if( x < 20 ){
System.out.print("满足if条件执行这里代码!");
}else{
System.out.print("不满足执行这里代码!");
}
}
}
- if…else if…else…
- if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
- if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
- 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。
语法结构
if(布尔表达式 1){
//如果布尔表达式 1的值为true 执行这里代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true 执行这里代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true 执行这里代码
}else {
//如果以上布尔表达式都不为true 执行这里代码
}
示例
public class Branch {
public static void main(String args[]){
int x = 30;
if( x == 10 ){
System.out.print("满足x==10 执行这里代码!");
}else if( x == 20 ){
System.out.print("满足x==20 执行这里代码!");
}else if( x == 30 ){
System.out.print("满足x==30 执行这里代码!");
}else{
System.out.print("都不满足执行这里代码!");
}
}
}
- switch …… case
- 建议 : 每一个 case 后边都加上一个 break , 表示 终止 当前 switch 中的case 分支 。
- 如果每个case 后边都不加上 break ,结果就是 匹配到的case 之后所有的代码都会执行
- switch 中 可以有 default , 作用是 如果没有匹配的内容,将执行 default 中的内容 ; 建议必须写!
- switch 中的 default 一般是在 整个switch 的最后边
- switch 括号中可以传入的类型是 : byte、short、char 、int 、枚举、String 、以及前边四个基本数据类型的包装类(Byte、Short 、Integer、Character)
- case 语句中的 值 一定是 确定的值 (常量),不能是 变量或 表达式 ; 值互不相同(基本常识).
- 分支嵌套
- if 后边执行的代码,放入另一个 if 或 switch
- 一般可以用 switch 声明的 分支,都可以使用 if 来进行改造 。
- JDK17 新增了 switch 的 preview 版本 。(switch 增强)
语法结构
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
示例
public class Branch {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
//switch增强
switch (i) {
case 0 -> System.out.println("0");
case 1 -> System.out.println("1");
case 2 -> System.out.println("2");
default -> System.out.println("default");
}
}
}
循环结构
循环结构 : 在满足一定条件的情况下, 反复 执行某些东西(代码)
循环四要素
- 初始条件
- 判定条件
- 迭代部分 (步进) step : 有增加或减少 ( 有增量 )
- 循环体 : 被 反复执行的代码
Java中有三种主要的循环结构
- while 循环
- do…while 循环
- for 循环
while循环
语法结构
1初始化部分
while( 2循环条件部分 ){
3循环体部分;
4迭代部分; // 3、4 的位置可以调换
}
具体的执行顺序:1-2-3-4-2-3-4-2-3-4-...-2
示例
public class Loop {
public static void main(String[] args) {
int x = 10;//初始化
while( x < 20 ) {//循环条件
System.out.print("value of x : " + x );//循环体
x++;//迭代
System.out.print("\n");//循环体
}
}
}
do…while循环
语法结构
1初始化部分;
do{
3循环体部分;
4迭代部分;
}while( 2循环条件部分 );
具体的执行顺序:1-3-4-2-3-4-2-3-4-....-2
示例
public class Loop {
public static void main(String[] args){
int x = 10;//初始化
do{
System.out.print("value of x : " + x );//循环体
x++;//迭代
System.out.print("\n");//循环体
}while( x < 20 );//循环条件
}
}
for循环
最常用的循环; 能用while 循环书写的循环,也可以被 for 循环所书写。
语法结构
for ( 1初始化部分 ; 2循环条件部分 ; 4迭代部分 ){
3循环体部分;
}
具体的执行顺序:1-2-3-4-2-3-4-2-3-4-.....-2
示例
public class Loop {
public static void main(String[] args) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Java增强for循环
主要用于数组的增强型 for 循环。
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
语法结构
for(声明语句 : 表达式)
{
//代码句子
}
示例
public class Loop {
public static void main(String[] args){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
}
}
break和continue关键字
- break : 表示终止的意思 , 如果用在哪个循环中,就表示终止哪个包含break 的循环
- continue : 只能使用在循环结构中;用于跳过其所在循环语句块的一次执行,继续下一次循环
说明: - break只能用于switch语句和循环语句中。
- continue 只能用于循环语句中。
- 二者功能类似,但continue是终止本次循环,break是终止本层循环。
- break、continue之后不能有其他的语句,因为程序永远不会执行其后的语句。
- 标号语句必须紧接在循环的头部。标号语句不能用在非循环语句的前面。基本没人用!
- 很多语言都有goto语句,goto语句可以随意将控制转移到程序中的任意一条 语句上,然后执行它。但使程序容易出错。Java中的break和continue是不同 于goto的。
示例1:break使用
public class BreakUse {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// x 等于 30 时跳出循环,30之后的都不会执行了
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
//输出:10,20
示例2:continue使用
public class ContinueUse {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {//跳过等于30的时候,继续执行30后面的
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
//输出:10,20,40,50