流程控制
顺序结构
从上行下依次执行,中间没有任何判断和跳转
分支结构
根据条件,选择执行某段代码
- if-else
if(条件表达式){
执行代码块1;
}else{
执行代码块2;
}
class Test6{
public static void main (String[] arges){
int age = 22;
if(age<0){
System.out.println("数据错误");
}else if(age<18){
System.out.println("青年时期");
}else if(age<40){
System.out.println("壮年时期");
}else if(age<60){
System.out.println("中年时期");
}else{
System.out.println("老年时期");
}
}
}
//壮年时期
- switch-case
- 语法
switch(表达式){
case 常量1:
语句1;
[break;]
case 常量2:
语句2;
[break;]
default:
语句;
[break;]
}
- 示例
class Test7{
public static void main (String[] args){
int num=2;
switch(num){
case 0:
System.out.println("zero");
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}
}
}
//two
//three
class Test7{
public static void main (String[] args){
int num=2;
switch(num){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
}
}
}
//two
- 注意点
- switch在判定成功后,会继续执行剩下的代码,除非遇到break或代码执行完毕才会停止
- switch中表达式只能是:byte short char int 枚举类型 String类型
- case 之后只能声明常量
循环结构
根据循环结构,重复执行某段代码
- for循环
- 语法
for(初始化,循环条件,迭代){
循环体
}
- 示例
class forTest{
public static void main (String[] arges){
for(int i=0;i<4;i++){
System.out.println("hello word");
}
}
}
//hello word
//hello word
//hello word
//hello word
- while循环
- 语法
初始化;
while(循环条件){
循环体;
迭代;
}
- 示例
class whileTest{
public static void main (String[] agres){
int sum=0;
int i=0;
while(i<=100){
if(i % 2 == 0){
sum +=i;
}
i++;
}
System.out.println(sum);
}
}
//2550
- do-while循环
- 语法
初始化
do{
循环体;
循环条件;
}while(迭代)
//也就是do-while至少执行一次
- 示例
class doWhileTest{
public static void main (String[] args){
int sum=0;
int i=0;
do{
if(i % 2 ==0){
sum+=i;
}
i ++;
}while(i<= 100);
System.out.println(sum);
}
}
//2550
- 循环4个组成部分
- 初始化部分
- 循环条件部分
- 循环体部分
- 迭代部分
- 示例:做一个统计输入的正负数个数
import java.util.Scanner;
//引入模块
class Test1{
public static void main (String[] aegs){
Scanner scan =new Scanner(System.in);
//实例化
int positiveNumber = 0;
int negativeNumber = 0;
while(true){
int number =scan.nextInt();
if(number > 0){
positiveNumber ++;
}else if(number <0){
negativeNumber ++;
}else{
break;
//跳出循环
}
}
System.out.println(positiveNumber);
//输出统计正数出现个数
System.out.println(negativeNumber);
//输出统计负数出现个数
}
}
- 跳出循环
- 在循环条件返回false
- 循环体中执行break
- 不在循环条件部分限制次数结构
- for( ; ; )
- while(true)
- break和continue
关键字 | 使用范围 | 含义 |
---|---|---|
break | switch-case 循环结构中 | 结束当前循环 |
continue | 循环结构中 | 结束本次循环 |
示例: |
class Test2{
public static void main (String[] arge){
for(int i=1;i <=4;i ++){
if( i % 2 == 0 ){
break;//输出为1
// continue; //输出为1 3
}
System.out.println(i);
}
}
}
- label结束指定循环
class Test3{
public static void main (String[] arge){
label: for(int i=0;i <5; i ++){
for(int j=0;j <4; j ++){
if(j % 2 ==1){
System.out.print("helloword ");
}
}
System.out.println("hello word");
}
}
}
/*
helloword helloword hello word
helloword helloword hello word
helloword helloword hello word
helloword helloword hello word
helloword helloword hello word
*/
class Test3{
public static void main (String[] arge){
label: for(int i=0;i <5; i ++){
for(int j=0;j <4; j ++){
if(j % 2 ==1){
System.out.print("helloword ");
break label;
}
}
System.out.println("hello word");
}
}
}
/*helloword*/