流程控制

分支语句
(1) If…else语句
格式: if(布尔表达式)
语句1;
else
语句2;
If-else-if形式
If(布尔表达式1)
语句1;
else if(布尔表达式2)
语句2;
else if(布尔表达式3)
语句3;
……
else{语句n};
public class Test { public static void main(String args[]){
int x = 30;
if( x == 10 ){
System.out.print(“Value of X is 10”); }
else if( x == 20 ){
System.out.print(“Value of X is 20”);
}else if( x == 30 ){
System.out.print(“Value of X is 30”);
}else{ System.out.print(“这是 else 语句”); } } }
每个else总是与离它最近的if配对使用
(2) 多选一switch语句
格式:
Switch(表达式)
case valuel:
处理语句1;
Break;
case valuel:
处理语句2;
Break;
case valuel:
处理语句3;
Break;
default:
处理语句n;
public class Test {
public static void main(String args[]){
int i = 1;
switch(i){
case 0: System.out.println(“0”);
case 1: System.out.println(“1”);
case2: System.out.println(“2”);
default: System.out.println(“default”);
}
}
}
Break可写可不写
表达式的结果类型只能为byte/short/int/char型
循环结构
(1) for循环:
格式:
for(初始化;循环条件;迭代部分){
循环体;
}
for的嵌套:
for(表达式1; 表达式2; 表达式3){
for(表达式1;表达式2;表达式3){
语句;
public class fortest {
public static void main(String args[]){
int sum=0;
for(int i=0;i<=100;i++){
if(i%2==0){
System.out.println(i);
sum+=i;
}
}
System.out.println(“总和为:”+sum);

}

}
}
}
(2) while循环:
格式:while(循环条件){
循环体;

public static void main(String[] args) {
int i = 1;
int n = 1;
while(i <= 10) {
n=n*i;
i++;
System.out.println(“10的阶乘结果为:”+n);

  }

(3) do…while循环:
格式:do{
循环体
}while(循环条件);

  1. public static void main(String[] args) {
  2. int i = 1;
  3. int n = 1;
  4. while(i <= 10) {
  5. n=n*i;
  6. i++;
  7. }
  8. System.out.println(“10的阶乘结果为:”+n);

先执行循环体,执行完后才判断循环条件
Break和continue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值