break,continue,return的區別


1)break     直接跳出當前的循環,從當前循環外面開始執行,忽略循環體中任何其他語句和循環條件測試。他只能跳出一層循環,如果你的循環是嵌套循環,那么你需要按照你嵌套的層次,逐步使用break來跳出.

2)continue     也是終止當前的循環過程,但他並不跳出循環,而是繼續往下判斷循環條件執行語句.他只能結束循環中的一次過程,但不能終止循環繼續進行.    

3)return 語句可被用來使 正在執行分支程序返回到調用它方法。(費解)

 

詳說return

  return表示中止當前函數的運行,並將操作權返回給調用者。   

  如果是在main函數中,表示將操作權返回給操作系統。    

    return不是必須要返回一個值。   

      void   func(void)   {   

       //代碼塊...;   return;   

      }   

    當然,就算不寫return,在函數執行完畢后也會返回操作權給調用者。寫return是一種清晰的風格,可以防止一些意外的錯誤。所以書上只說應該寫,而不是必須寫。 

    如果符合某個條件要退出的話,可以用return返回,否則可以不寫這句代碼的,當程序執行到“}”時會自動返回,這個問題其實你自己到機器上試一下就知道了。不過作為一個良好的編程習慣,每個函數都有返回語句倒是對的,這樣更有可讀性,並利於程序的維護~~~~

   return   指令一個是返回一個指定數據給主調函數, 

   另外的一個作用就是   結束   所在函數的執行   ...

 

  子函數中的運算是不能改變main()中已經定義的變量

    void add(int a, int b,int c){

      c=a+b;

    }

  main() //主方法入口,此處省略...

    {

      int a=1,b=2, c=0;

      add(a,b,c);

      print("/nc=%d/n", c);

    }

    你看看 最后結果還是c=0,因為計算機執行一個程序的時候 他只看到main()。他十分忠誠的 從main()的第一句 一直執行到最后一句 ,中間的那個 add()調用,這時main()函數被掛起 ,也就是停止運行 系統會把main()放在一塊內存x中 ,這時 系統會把一片內存y划分給add()函數,也就是說 main()中的abc;和add()中的abc,並不是同一個東西,他們只是在數值上相等罷了。系統首先把a、b的值傳給add,然后運行,c=3。然后系統沒有發現add中有return,也就是 子函數做完了運算。並沒把內存y中的結果送給main(),這時候 add運行結束。系統喚醒內存x中的main(),開始運行下一句 這時候 ,add函數做的一切 都是白干了,他並沒有把他的結果上交給main()函數。所以c仍然是0;

    int add(int a, int b,int c){

      c=a+b;

      return c;

    }

    main(){

      int a=1, b=2, c=0;

      c=add(a,b);

      print("/nc=%d/n",c);

      /*還可以這樣寫*/

      print("/nc=%d/n",add(a,b));

    }

    想要通過子函數來改變main()中的東西,有兩個方法:1 return ; 2 通過指針。因為Java畢竟很少用指針的說法,常見於C、C++、C#。故此處不做多的辯解

    測試追加案例:

    void test(Integer[] a){

      a[0] = 1;

      a[1] = 2;

    }

    main(...){

      Integer a[] = new Integer[3];

      test(a);

      print("a[0]="+a[0]+",a[1]="+a[1]);結果:a[0]=1,a[1]=2

    }

    可能有些朋友就疑惑了,我函數里面並沒有返回,可是為什么值確改變了呢?與上面不同的是,我們知道在java中每一個對象都占用一個單獨的內存(參考堆棧說明),

    上面的計算等后,實際給c另外分配了一個內存地址,而新的內存地址的值並未重新指向原內存地址,顧值不會產生任何改變,

    而當以數組或者集合入參的時候,可以通過測試發現,其內存地址並未有任何改變,只是改變了對應棧的值。

    案例:

void test(List<Integer> acc,Integer a,Integer b,Integer[] c){
acc.add(1);
acc.add(2);
acc.add(3);
acc.add(4);
System.out.println("計算中:acc->hash:"+System.identityHashCode(acc));
System.out.println("計算中:a->hash:"+ System.identityHashCode(a));
System.out.println("計算中:b->hash:"+ System.identityHashCode(b));
a = a+b;
System.out.println("計算中:c->hash:"+ System.identityHashCode(a));
c[0] = 1;
c[1] = 2;
c[2] = 3;
System.out.println("計算中:c->hash:"+ System.identityHashCode(c));
}

main(...){
  
List<Integer> acc = new ArrayList<>();
Integer a = 1;
Integer b = 2;
Integer c[] = new Integer[3];
System.out.println("計算前:acc->hash:"+System.identityHashCode(acc));
System.out.println("計算前:a->hash:"+ System.identityHashCode(a));
System.out.println("計算前:b->hash:"+ System.identityHashCode(b));
System.out.println("計算前:c->hash:"+ System.identityHashCode(c));
System.out.println("---------------------------------");
test(acc,a,b,c);
System.out.println("---------------------------------");
System.out.println("計算后:acc->hash:"+System.identityHashCode(acc));
System.out.println("計算后:a->hash:"+ System.identityHashCode(a));
System.out.println("計算后:b->hash:"+ System.identityHashCode(b));
System.out.println("計算后:c->hash:"+ System.identityHashCode(c));

  
}

 打印結果:

計算前:acc->hash:868693306
計算前:a->hash:1746572565
計算前:b->hash:989110044
計算前:c->hash:424058530
---------------------------------
計算中:acc->hash:868693306
計算中:a->hash:1746572565
計算中:b->hash:989110044
計算中:c->hash:321001045
計算中:c->hash:424058530
---------------------------------
計算后:acc->hash:868693306
計算后:a->hash:1746572565
計算后:b->hash:989110044
計算后:c->hash:424058530

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

僅供參考,如有錯誤,還請指點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM