線性結構之鏈隊
迴圈結構
while迴圈
while迴圈
while是最基本的迴圈,它的結構為:
while(布林表示式) {
迴圈內容
}
public class WileDemo01 {
public static void main(String[] args) {
//輸出1~100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
public class WileDemo02 { public static void main(String[] args) { //死迴圈 while (true){ //客戶端等待登入 } } }
只要布林表示式為true,迴圈就會一直執行下去。
我們大多數情況是會讓迴圈停止下來的,我們需要一個讓表示式失效的方式來結束迴圈。少部分情況需要迴圈一直執行,比如伺服器的請求響應監聽等。
迴圈條件一直為true就會造成無限迴圈【死迴圈】,我們正常的業務程式設計中應該儘量避免死迴圈。會影響程式效能或者造成程式卡死奔潰!
思考:計算1+2+3+..+100=?
public class WileDemo03 { public static void main(String[] args) { int i = 0; int sum = 0; while (i<=100){ sum = sum + i; i++; } System.out.println(sum); } }
do...while迴圈
do... while 迴圈
對於while語句而言,如果不滿足條件,則不能進入迴圈。但有時候我們需要即使不滿足條件.也至少執行一次。
do...while迴圈和while迴圈相似,不同的是,do...while迴圈至少會執行一次。
do {
//程式碼語句
}while(布林表示式);
While和do-While的區別:
while先判斷後執行。dowhile是先執行後判斷!
public class DoWileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while (i<=100); System.out.println(sum); } }
Do...while總是保證迴圈體會被至少執行一次!這是他們的主要差別。
public class DoWileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("==================================" );
do {
System.out.println(a);
a++;
}while (a<0);
}
}
for迴圈
雖然所有迴圈結構都可以用while或者do...while表示,但Java提供了另一種語句—— for迴圈,使一些迴圈結構變得更加簡單。
for迴圈語句是支援迭代的一種通用結構,是最有效、最靈活的迴圈結構。for迴圈執行的次數是在執行前就確定的。語法格式如下:
for(初始化;布林表示式;更新){
//程式碼語句
}
package com.jeger.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;
while (a<=100){
System.out.println(a);
a+=1;//迭代
}
System.out.println("while迴圈結束");
for (int i = 1;i<=100;i+=2){
System.out.println(i);
}
System.out.println("for迴圈結束");
/*
關於for迴圈有以下幾點說明:
最先執行初始化步驟。可以宣告一種型別,但可初始化一個或多個迴圈控制變數,也可以是空語句。
然後,檢測布林表示式的值。如果為true,迴圈體被執行。如果為false,迴圈終止,開始執行迴圈體後面的語句。
執行一次迴圈後,更新迴圈控制變數(迭代因子控制迴圈變數的增減)。
再次檢測布林表示式。迴圈執行上面的過程。
*/
for (;;){
//死迴圈
}
}
}
練習1:計算0到100之間的奇數和偶數的和
package com.jeger.struct;
public class ForDemo02 {
public static void main(String[] args) {
int a = 0;
int b = 0;
for (int i = 0; i <= 100; i++) {
if (i%2==0){
a+=i;
}else {
b+=i;
}
}
System.out.println("偶數和為"+a);
System.out.println("奇數和為"+b);
}
}
練習2:用while或for迴圈輸出1-1000之間能被5整除的數,並且每個
package com.jeger.struct;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
}
// print() 輸出完不會換行
// println() 輸出完會換行
}
int a= 1;
while (a<=1000){
if (a%5==0){
System.out.print(a+"\t");
}
if (a%(5*3)==0){
System.out.println();
}
a+=1;
}
}
}
練習3:列印九九乘法表
package com.jeger.struct;
public class ForDemo04 {
public static void main(String[] args) {
for (int i=1;i<=9;i++){
for (int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
在Java5中引入了一種主要用於陣列的增強型for迴圈。
這裡我們先只是見一面,做個瞭解,之後陣列我們重點使用Java5引入了一種主要用於陣列或集合的增強型for迴圈。Java增強for迴圈語法格式如下:
for(宣告語句︰表示式){
//程式碼句子}
package com.jeger.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i=0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("==========================");
for (int x:numbers){
if (x%20==0){
System.out.println(x);
}
}
}
}
宣告語句:宣告新的區域性變數,該變數的型別必須和陣列元素的型別匹配。其作用域限定在迴圈語句塊,其值與此時陣列元素的值相等。
◆表示式:表示式是要訪問的陣列名,或者是返回值為陣列的方法。