曾新洲
摘 要: 循環(huán)語(yǔ)句是面向?qū)ο蟪绦蛟O(shè)計(jì)的基礎(chǔ),Java程序設(shè)計(jì)語(yǔ)言提供了三種基本的循環(huán)語(yǔ)句:while循環(huán)語(yǔ)句、for循環(huán)語(yǔ)句和do…while循環(huán)語(yǔ)句[1],通常情況下,這三種循環(huán)語(yǔ)句可互換[2],但又略有不同,for循環(huán)語(yǔ)句與另外兩種循環(huán)語(yǔ)句相比,結(jié)構(gòu)更緊湊,編寫(xiě)更方便,使用更頻繁。本文針對(duì)高職學(xué)生學(xué)習(xí)循環(huán)語(yǔ)句難度較大的問(wèn)題,提出了一種引導(dǎo)式教學(xué)方法,通過(guò)分析一個(gè)典型案例---“求前10個(gè)自然數(shù)的和”的執(zhí)行過(guò)程,提煉出循環(huán)體、循環(huán)執(zhí)行條件等,引導(dǎo)學(xué)生自己拼湊出循環(huán)語(yǔ)句,同時(shí)也分析了Java語(yǔ)言中三種循環(huán)語(yǔ)句的結(jié)構(gòu),并對(duì)三種循環(huán)語(yǔ)句的相同點(diǎn)和不同點(diǎn)進(jìn)行了詳細(xì)探討。
關(guān)鍵詞: Java語(yǔ)言; for循環(huán)語(yǔ)句; while循環(huán)語(yǔ)句; do…while循環(huán)語(yǔ)句
中圖分類號(hào):TP312 文獻(xiàn)標(biāo)志碼:A 文章編號(hào):1006-8228(2018)09-51-04
Abstract: Loop statements are the basis of object oriented programming, and the Java programming language provides three basic loop statements: while loop statement, for loop statement, and do... While loop statement, usually, these three loop statements are interchangeable, but are slightly different, for loop statement is more compact in structure, more convenient to write, and more frequently used than the other two loop statements. In order to solve the problem of Higher Vocational Students' difficulty in learning loop sentences, a guiding teaching method is put forward. By analyzing a typical case, the execution process of "the sum of the 10 natural numbers in the first place", the body of the loop and the execution condition of the loop are extracted to guide the students to piece together the loop sentences. The structure of three loop statements in Java language is discussed, and the similarities and differences between the three loop statements are discussed in detail.
Key words: Java language; for loop statement; while loop statement; do...while loop statement
0 引言
Java語(yǔ)言作為目前使用最為廣泛的一種程序設(shè)計(jì)語(yǔ)言,有三種基本的循環(huán)語(yǔ)句:while循環(huán)語(yǔ)句、for循環(huán)語(yǔ)句和do…while循環(huán)語(yǔ)句。高職有不少學(xué)生英語(yǔ)和數(shù)學(xué)基礎(chǔ)較差,邏輯思維能力較弱,在學(xué)習(xí)循環(huán)語(yǔ)句時(shí)有點(diǎn)力不從心。為了讓學(xué)生盡快掌握循環(huán)語(yǔ)句,在教學(xué)過(guò)程中盡可能聯(lián)系實(shí)際,例如采用引導(dǎo)式教學(xué)法,引導(dǎo)學(xué)生使用循環(huán)語(yǔ)句實(shí)現(xiàn)“求前10個(gè)自然數(shù)的和”;然后要求學(xué)生模仿教師的做法,使用循環(huán)語(yǔ)句實(shí)現(xiàn)“求前100個(gè)自然數(shù)的和”;并進(jìn)行擴(kuò)展練習(xí),如使用循環(huán)語(yǔ)句實(shí)現(xiàn)“求2+4+6+…+18+20的和”。下面我們對(duì)Java語(yǔ)言中的三種循環(huán)語(yǔ)句進(jìn)行詳細(xì)分析。
1 while循環(huán)語(yǔ)句
while循環(huán)語(yǔ)句的通用格式如下:
①
while(②)
{
③
}
④
其中①表示while循環(huán)語(yǔ)句之前的語(yǔ)句,②表示循環(huán)執(zhí)行的條件,③表示循環(huán)體[3](滿足循環(huán)條件時(shí),執(zhí)行循環(huán)體,循環(huán)體為重復(fù)執(zhí)行的代碼),④表示while循環(huán)語(yǔ)句之后的語(yǔ)句,while循環(huán)語(yǔ)句的執(zhí)行流程如圖1所示。
在教學(xué)中采用先分析執(zhí)行過(guò)程,然后再拼湊循環(huán)語(yǔ)句的方式進(jìn)行講解,例如,采用while循環(huán)語(yǔ)句實(shí)現(xiàn)“求前10個(gè)自然數(shù)的和”,教學(xué)過(guò)程設(shè)計(jì)如下。
⑴ 求和運(yùn)算,定義一個(gè)初始值為0的整形變量保存最終結(jié)果,如下所示:
int sum=0;
⑵ 執(zhí)行求和運(yùn)算,這里采用小學(xué)生的做法,首先將自然數(shù)1加入到最終的結(jié)果中:
sum=sum+1; //語(yǔ)句⑴
⑶ 然后將自然數(shù)2加入到最終的結(jié)果中:
sum=sum+2; //語(yǔ)句⑵
依此類推:
sum=sum+3; //語(yǔ)句⑶
…
sum=sum+10; //語(yǔ)句⑽
語(yǔ)句⑴、⑵、⑶….⑽中的數(shù)字不相同,且相鄰語(yǔ)句之間的數(shù)字相差1,若采用一個(gè)整形變量i(初始值為1)來(lái)替換語(yǔ)句⑴中的數(shù)字1,語(yǔ)句⑴執(zhí)行完以后,變量i的值加1,使用變量i替換語(yǔ)句⑵中的數(shù)字2,語(yǔ)句⑵執(zhí)行完后,變量i的值再加1,使用變量i替換語(yǔ)句⑶中的數(shù)字3,依此類推,執(zhí)行過(guò)程如下:
int i=1;
sum=sum+i; //等同于sum=sum+1
i++;
sum=sum+i; //等同于sum=sum+2
i++;
…
sum=sum+i; //等同于sum=sum+10
i++;
上述執(zhí)行過(guò)程中,有兩條語(yǔ)句重復(fù)執(zhí)行:
sum=sum+i;
i++;
這部分代碼可作為while循環(huán)語(yǔ)句通用格式中的③,變量i的初始值為1,每執(zhí)行一次循環(huán)體③,i的值加1,最終i的值變成11,程序結(jié)束,因此,可將i<11作為while循環(huán)語(yǔ)句執(zhí)行的條件②。而以下兩行語(yǔ)句在while循環(huán)之前執(zhí)行。
int sum=0;
int i=1;
因此,可將其作為while循環(huán)之前的第①部分,引導(dǎo)學(xué)生拼湊出如下while循環(huán)語(yǔ)句。
int sum=0;
int i=1;
while(i<11)
{
sum=sum+i;
i++;
}
在編寫(xiě)服務(wù)器程序時(shí),經(jīng)常使用無(wú)限循環(huán),while循環(huán)語(yǔ)句表示無(wú)限循環(huán)的一般格式如下:
while(true)
{
循環(huán)體;
}
上述“求前10個(gè)自然數(shù)的和”的while循環(huán)語(yǔ)句,可使用無(wú)限循環(huán)方式實(shí)現(xiàn),實(shí)現(xiàn)過(guò)程如下:
int sum=0;
int i=1;
while(true)
{
if(i<11)
{
sum=sum+i;
i++;
}
else
break;
}
在上述語(yǔ)句中,將循環(huán)執(zhí)行的條件"i<11"放置在循環(huán)體內(nèi),一旦循環(huán)變量i的值等于11,便結(jié)束while循環(huán)語(yǔ)句。
2 for循環(huán)語(yǔ)句
for循環(huán)語(yǔ)句的通用格式如下:
①
for(②; ③; ④)
{
⑤
}
⑥
其中①表示for循環(huán)語(yǔ)句之前的語(yǔ)句,②一般用來(lái)做循環(huán)變量的初始化操作,③表示循環(huán)執(zhí)行的條件,④表示循環(huán)變量的變化,⑤表示循環(huán)體[4],⑥表示for循環(huán)語(yǔ)句之后的語(yǔ)句,for循環(huán)語(yǔ)句的執(zhí)行流程[5]如圖2所示。
根據(jù)while循環(huán)語(yǔ)句部分的分析,可通過(guò)for循環(huán)語(yǔ)句實(shí)現(xiàn)“求前10個(gè)自然數(shù)的和”。
int sum=0;
int i=1;
這兩條語(yǔ)句是while循環(huán)語(yǔ)句之前執(zhí)行的語(yǔ)句,其中變量i為循環(huán)變量,因此,可將int i=1;放在for循環(huán)語(yǔ)句的第②個(gè)部分(循環(huán)變量的初始化部分),int sum=0;放在for循環(huán)語(yǔ)句的第①個(gè)部分(for循環(huán)語(yǔ)句之前執(zhí)行的語(yǔ)句),while循環(huán)語(yǔ)句中的循環(huán)體包括以下兩條語(yǔ)句:sum=sum+i; i++;其中sum=sum+i;作為for循環(huán)語(yǔ)句第⑤個(gè)部分(循環(huán)體),而i++作為for循環(huán)語(yǔ)句的第④個(gè)部分(循環(huán)變量的變化),i<11為循環(huán)的執(zhí)行條件,作為for循環(huán)語(yǔ)句中的第③部分,用for循環(huán)語(yǔ)句實(shí)現(xiàn)“求前10個(gè)自然數(shù)的和”如下所示:
int sum=0;
for(int i=1; i<11; i++)
{
sum=sum+i;
}
實(shí)際上,for循環(huán)語(yǔ)句的通用格式中的第②、③、④部分都可以省略,但它們之間的分號(hào)必須保留,以“求前10個(gè)自然數(shù)的和”為例,代碼如下:
int sum=0;
int i=1
for(; ;)
{
if(i<11)
{
sum=sum+i;
i++;
}
else
break;
}
此時(shí),for循環(huán)語(yǔ)句變成了一個(gè)無(wú)限循環(huán),類似于while無(wú)限循環(huán),將循環(huán)變量初始化操作放在for循環(huán)語(yǔ)句之前,循環(huán)執(zhí)行的條件和循環(huán)變量的變化放置在for循環(huán)體內(nèi)。
3 do…while循環(huán)語(yǔ)句
do…while循環(huán)語(yǔ)句的通用格式如下:
①
do
{
②
} while(③);
④
其中①表示do…while循環(huán)語(yǔ)句之前的語(yǔ)句,②表示循環(huán)體,③表示循環(huán)執(zhí)行的條件,④表示do…while循環(huán)語(yǔ)句之后的語(yǔ)句,do..while循環(huán)語(yǔ)句的執(zhí)行流程如圖3所示。
在do…while循環(huán)語(yǔ)句中,先執(zhí)行循環(huán)體,然后再判斷循環(huán)執(zhí)行的條件,如果條件為真,則繼續(xù)執(zhí)行循環(huán)體,如果條件為假,則結(jié)束整個(gè)do…while循環(huán)語(yǔ)句,采用do…while循環(huán)語(yǔ)句實(shí)現(xiàn)“求前10個(gè)自然數(shù)的和”代碼如下:
int sum=0;
int i=1;
do
{
sum=sum+i;
i++;
} while(i<11);
這種方式跟while循環(huán)語(yǔ)句非常類似,只是將while循環(huán)語(yǔ)句中的while換成do,然后將while(i<11)放在了循環(huán)體后,并以分號(hào)結(jié)束。但do…while循環(huán)語(yǔ)句與while循環(huán)語(yǔ)句并不完全相同,因?yàn)閐o…while循環(huán)語(yǔ)句不管循環(huán)執(zhí)行的條件是否為真,都會(huì)執(zhí)行一次循環(huán)體,因此,初次判斷循環(huán)執(zhí)行的條件為真時(shí),兩者完全相等,而初次判斷循環(huán)執(zhí)行的條件為假時(shí),do…while循環(huán)語(yǔ)句與while循環(huán)語(yǔ)句不同,以下的do…while循環(huán)語(yǔ)句和while循環(huán)語(yǔ)句就不相同。
while循環(huán)語(yǔ)句如下:
int i=20;
while(i<10)
{
System.out.println(i);
i--;
}
do…while循環(huán)語(yǔ)句如下:
int i=20;
do
{
System.out.println(i);
i--;
} while(i<10);
其中while循環(huán)語(yǔ)句沒(méi)有輸出,而do…while循環(huán)語(yǔ)句輸出20。
4 結(jié)束語(yǔ)
do…while循環(huán)語(yǔ)句不管條件如何,至少會(huì)執(zhí)行一次,while循環(huán)語(yǔ)句和for循環(huán)語(yǔ)句可互換,for循環(huán)語(yǔ)句與while循環(huán)語(yǔ)句相比,結(jié)構(gòu)更緊湊,在實(shí)際開(kāi)發(fā)中,更傾向于使用for循環(huán)語(yǔ)句,特別是在多重循環(huán)中均采用for循環(huán)語(yǔ)句來(lái)實(shí)現(xiàn)。在編寫(xiě)循環(huán)語(yǔ)句時(shí),必須弄清循環(huán)語(yǔ)句的四個(gè)組成部分:循環(huán)變量的初始化、循環(huán)執(zhí)行的條件、循環(huán)體和循環(huán)變量的變化。其中循環(huán)執(zhí)行的條件和循環(huán)體最為關(guān)鍵,可采用歸納法,提取出重復(fù)執(zhí)行的部分——循環(huán)體[6],通過(guò)分析,確定什么情況下執(zhí)行循環(huán)——循環(huán)語(yǔ)句的執(zhí)行條件,確定了上述兩部分內(nèi)容后,循環(huán)語(yǔ)句也就基本確定了。
參考文獻(xiàn)(References):
[1] [美]Bruce Eckel著,陳昊鵬譯.Java編程思想(第四版)[M].機(jī)械工業(yè)出版社,2007.
[2] 李英.C語(yǔ)言中for循環(huán)語(yǔ)句教學(xué)設(shè)計(jì)[J].網(wǎng)絡(luò)與信息工程,2017.8:60-61
[3] 凱 S.霍斯特曼(Cay S.Horstmann).Java核心技術(shù) 卷I:基礎(chǔ)知識(shí)(原書(shū)第十版)[M].機(jī)械工業(yè)出版社,2016.
[4] 張淑敏,王元芬.C語(yǔ)言中三種循環(huán)語(yǔ)句辨析[J].電腦與信息技術(shù),2017.25(1):26-27
[5] 明日科技.Java從入門(mén)到精通(第四版)[M].清華大學(xué)出版社,2016.
[6] 宋靈香.C語(yǔ)言中for循環(huán)語(yǔ)句教學(xué)研究[J].軟件導(dǎo)刊,2013.12(3):184-186