return,break,continue三者区别

continue语句并没有真的退出循环,而是只结束本次循环的执行,所以使用continue时要注意这一点。

情景一
package com.spring_boot.guava;

import com.google.common.collect.Maps;
import java.util.Map;

/**
 * Created by wanggs on 2017/9/28.
 */
public class BreakTest {
    public static void main(String[] args) {
        Map map = Maps.newHashMap();
        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 3);
        map.put("4", 4);
        map.put("5", 5);
        for (String key : map.keySet()){
            if (key.equals("3"))
                continue;
            System.out.println("key: " + key + "value: " + map.get(key));
        }
        System.out.println("结束...");
        /**
         * key: 1value: 1
           key: 2value: 2
           key: 4value: 4
           key: 5value: 5
         结束...

         */
    }
}


情景二
for (String key : map.keySet()) {
            if (key.equals("3"))
                System.out.println("key: " + key + "value: " + map.get(key));
            continue;
        }
        System.out.println("结束...");
        /**
         * key: 3value: 3
           结束...
         */
      

break语句使用场合主要是switch语句和循环结构。break语句,那么直接退出循环,执行循环结构下面的第一条语句。如果在多重嵌套循环中使用break语句,当执行break语句时,退出的是它所在的循环结构,对外层循环没有影响。

情景一
for (String key : map.keySet()) {
            if (key.equals("3"))
                break;
                System.out.println("key: " + key + "value: " + map.get(key));
        }
        System.out.println("结束...");
        /**
         * key: 1value: 1
           key: 2value: 2
           结束...
         */
情景二
for (String key : map.keySet()) {
            if (key.equals("3"))
                System.out.println("key: " + key + "value: " + map.get(key));
            break;
        }
        System.out.println("结束...");
        /**
           结束...
         */
    }
```---
> 如果在程序中遇到return语句,那么戴拿就退出该函数的执行,退回到函数的调用处,如果是main()函数,那么就结束整个程序的运行
###### 情景一
  for (String key : map.keySet()) {
        if (key.equals("3"))
            return;
            System.out.println("key: " + key + "value: " + map.get(key));
    }
    System.out.println("结束...");
    /**
     key: 1value: 1
     key: 2value: 2
     */
}
###### 情景二
  for (String key : map.keySet()) {
        if (key.equals("3"))
            System.out.println("key: " + key + "value: " + map.get(key));
        return;
    }
    System.out.println("结束...");
    /**
     直接退出
     */
}
###### 情景三无返回参数

package com.spring_boot.guava;

import com.google.common.collect.Maps;

import java.util.Map;

/**

  • Created by wanggs on 2017/9/28.
    */
    public class BreakTest {
    public static void main(String[] args) {

     console("3");
     System.out.println("结束...");
     /**
      结束...
      */
    

    }

    private static void console(String key) {
    if ("3".equals(key)) {
    String result = System.currentTimeMillis() + "--->>" + key;
    return;
    }
    System.out.println("测试");
    }
    }

---
> exit()函数和return语句的最大区别在于,调用exit函数将会结束当前进程,同时删除子进程所占用的内存空间,把返回信息传给父进程。当exit()中的参数为0时,表示正常退出,其他返回值表示非正常退出,执行exit()函数意味着进程结束

你可能感兴趣的:(return,break,continue三者区别)