Jsフロー制御文(二)循環文
2834 ワード
主にfor、whileがあります.
var age=prompt(" ");
if(age<18){
alert(" "); //alert()
}else if(18>>>>>>>>>>
var str="abc123";
var num=parseInt(str)
if (num==NaN){ //NaN ,
alert(NaN);
}else if(num==123){
alert(123);
}else if(typeof num=="number"){
alert("num");
}else{
alert("str");
}
console.log(age);
while"""
while( ){
}
"""
var i=1;
while( i<=100){
document.write(i+'
');
i+=1;
}
#
# do while
"""
do{
}while( )
"""
# :
var j =1;
do{
if(j%2==0){
console.log(j);
}
j++;
}while(j<=10);
# >>>>
var sum=0;
var n=1;
while(n<=100){
sum+=n;
n++;
}
console.log(sum)
""" for ,while """
break、徹底的に循環continueを退出します.今回の循環を終了し、次の循環を開始します.var num=0;
for(var i=1;i<10;i++){
if(i%5==0){
break;
}
console.log(i)
}
for(var i=0;i<=10;i++){
if(i%5==0){
continue;
}
console.log(i);
}
多条件判定文法:new Date().getDay()機能:取得曜日戻り値:number(0-6)
var week = new Date().getDay();
var weekstr = "";
// switch
switch(week){
case 0:
// document.write(" ");//
weekstr = " ";
break;
case 1:
// document.write(" ");//
weekstr = " ";
break;
case 2:
weekstr = " ";
// document.write(" ");
break;
case 3:
weekstr = " ";
// document.write(" ");
break;
case 4:
weekstr = " ";
// document.write(" ");
break;
case 5:
weekstr = " ";
// document.write(" ");
break;
default:
weekstr = " ";
// document.write(" ")
}
document.write(" " + weekstr)