JAVA的流程控制

1.条件语句

if

if(布尔表达式)
{
   //如果布尔表达式为true将执行的语句
}
  public static void main(String[] args) {
	// write your code here
        int x = 10;

        if( x < 20 ){
            System.out.print("这是 if 语句");
        }
    }

if..else..

if(布尔表达式)
{
   //如果布尔表达式为true将执行的语句
}else{
   //如果布尔表达式为false将执行的语句
}

 

 public static void main(String[] args) {
	// write your code here
        int x = 10;

        if( x < 20 ){
            System.out.print("小于20");
        }else {
            System.out.print("不小于20");
        }
    }

if..else if..else

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}else {
   //如果以上布尔表达式都不为true执行代码
}
public static void main(String[] args) {
	// write your code here
        int x = 10;

        if( x < 20 ){
            System.out.print("小于20");
        }else if (x<30){
            System.out.print("小于30");
        }else if (x<40){
            System.out.print("小于40");
        }else {
            System.out.print("不小于40");
        }
    }

switch

switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}
public static void main(String[] args) {
	// write your code here
        int x = 10;
        switch (x){
            case 0:
                System.out.print("0");
                break;
            case 1:
            case 2:
                System.out.print("1或者2");
                break;
            case 10:
                System.out.print("10");
                break;
            default:
                break;
        }
    }

2.循环语句 

while 

while( 布尔表达式 ) {
  //循环内容
}

 

 public static void main(String[] args) {
	// write your code here
        int x = 10;
        while( x < 20 ) {
            System.out.print("value of x : " + x );
            x++;
            System.out.print("\n");
        }
    }

do..while(区别与while,do…while 循环至少会执行一次。) 

do {
       //代码语句
}while(布尔表达式);
public static void main(String[] args) {
	// write your code here
        int x = 10;
        do{
            System.out.print("value of x : " + x );
            x++;
            System.out.print("\n");
        }while (x<10);
    }

for

for(初始化; 布尔表达式; 更新) {
    //代码语句
}
public static void main(String[] args) {
	// write your code here
      for (int i=0;i<10;i++){
          System.out.println(""+i);
      }
    }

增强 for 循环

for(声明语句 : 表达式)
{
   //代码句子
}
 private static String[] strings = {"1", "2", "3"};

    public static void main(String[] args) {
        // write your code here

        for (String name : strings) {
            System.out.print(name);
            System.out.print(",");
        }
    }

3.跳转语句

break

private static int[] ints = {1,2,3};

    public static void main(String[] args) {
        // write your code here

        for (int num : ints) {
            if (num>2){
                break;
            }
            System.out.println("num:"+num);
        }
    }
continue(与break都是中断的意思,break中断整个循环,continue中断单词循环)
 private static int[] ints = {1,2,3};

    public static void main(String[] args) {
        // write your code here

        for (int num : ints) {
            if (num==2){
                continue;
            }
            System.out.println("num:"+num);
        }
    }

return 从当前方法返回

public void getName() {
    return name;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值