JavaScript基本的條件判斷語(yǔ)句如下:
1、if 、if else if
2、for
3、while , do while
一 、 if語(yǔ)句
if語(yǔ)法 :
? ? ? ? if (條件) {
? ? ? ? ? ? //執(zhí)行語(yǔ)句
? ? }
if 和 && 轉(zhuǎn)換
if ( 1 > 2 ) {
? ? ? ? document.write( ' a ' );
? ? }
? ? 1 > 2 && document.write( 'a' );
if else if 語(yǔ)法:
if (條件){
? ? ? ? //執(zhí)行語(yǔ)句1
? ? } else if {
? ? ? ? //執(zhí)行語(yǔ)句2
? ? } else {
? ? ? ? //執(zhí)行最后的語(yǔ)句
? ? }
二、 for 循環(huán)
for循環(huán)語(yǔ)法 :
for ( var i = 0; i <= 10; i ++) {
? ? ? ? document.write( 'i' );
? ? }
沒(méi)有固定的邏輯!
vari=0;
? ? for(;i;) {
? ? ? ? document.write('a');
? ? ? ? count++;
? ? ? ? if(count==10) {
? ? ? ? ? ? ? ? i=0;
? ? ? ? }
? ? }
while 循環(huán)語(yǔ)法 :
while(條件) {
? ? ? ? ? ? //執(zhí)行語(yǔ)句
? ? }
while案例:
? ? 找出100以?xún)?nèi)能被7整除,或帶有7的數(shù);
? ? vari=0;
? ? while(i<=100){
? ? ? ? if(i%7==0||i%10==7){
? ? ? ? ? ? document.write('i');
? ? ? ? }
? ? ? ? i++;
? ? }
do while 循環(huán)語(yǔ)法
var i = 0;
?
? ? do{
?
? ? ? ? document.write( ' a ' );
?
? ? ? ? i ++;
?
} while ( i < 0 );
作業(yè) :
1、計(jì)算 2 的 n 次冪,n 可輸入,n 為自然數(shù)。
2、計(jì)算 n 的階乘,n 可輸入。
3、1 1 2 3 5 8 輸出第 n 項(xiàng)。
4、編寫(xiě)一個(gè)程序,輸入一個(gè)三位數(shù)的正整數(shù),輸出時(shí)反向輸出。如:輸入456,輸出654。
5、輸入a,b,c 三個(gè)數(shù)字,打印出最大的。
6、打印除100以?xún)?nèi)的質(zhì)數(shù)。
條件語(yǔ)句補(bǔ)充
// switch case
//break
//continue
1、switch case 語(yǔ)句
switch(條件) {
? ? case條件判斷1:
? ? ? ? console.log('a');
? ? ? ? break;//終止語(yǔ)句,結(jié)束循環(huán)
? ? case條件判斷2:
? ? ? ? console.log('b');
? ? ? ? break;
? ? case條件判斷3:
? ? ? ? console.log('c');
? ? ? ? break;
}
2、break
終止循環(huán)
3、continue
跳出本次循環(huán)