JavaScript循環構造例問題(do{}.which();for(){}
20827 ワード
while(循環条件){循環体}
1.1-100
var n=0;
var sum=0;
while(n<100){
n++;
sum+=n;
document.write(n+'
')
}
document.write('1-100 :'+sum+'
')//5050
2.1-100
var n01=0;
var sum01=0;//
while(n01<100){
n01++;
if (n01%2==0){
sum01+=n01;//sum01=sum01+n01;
document.write(n01+'
')
}
}
document.write('1-100 '+sum01+'
');//2550
3.1-100 7
var n02=0;
while ( n02<100){
n02++;
if (n02%7==0) {
document.write(n02+'
')
}
}
4.1-100 ,
var n03=0;
var sum03=0;
while (n03<100){
n03++;
if (n03%2!=0){ //n03%2==1
sum03+=n03;
document.write(n03+'
');
}
}
document.write('1-100 :'+sum03+'
');//2500
ド{循環体}while(循環条件);1.1-100 3
var n001=0;
var sum001=0;
do{
n001++;
if (n001%3==0){
sum001+=n001; //
document.write(n001+'
');
}
}while(n001<100);
document.write('1-100 3 :'+sum001+'
');//1683
2. do-while : “ , ?”, “ ?(y/n):", y ” “, n,
do{
var tex=prompt(' ?y/n');
}while(tex!='y');
alert(' ');
for(初期化式;判定式;自己増加式){循環体}1. 1-100
for (var i=1;i<=100;i++) {
document.write(i+'
');
}
3. 1-100
var sum0=0;
for (var i=1;i<=100;i++) {
sum0+=i;
}
document.write('1-100 :'+sum0/100);//50.5
4. 1-100 ,
var sum1=0;
var sum2=0;
for (var i=1;i<=100;i++) {
if (i%2==0) {
//document.write('
'+i);
sum1+=i;
}
if(i%2!=0){
sum2+=i;
}
}
document.write('
'+'1-100 :'+sum1+'
');//2550
document.write('1-100 :'+sum2+'
');//2500
5. 1-100
var cheng=1;
for (var i=1;i<=100;i++) {
cheng*=i;
}
document.write('1-100 :'+cheng+'
');
//9.33262154439441e+157
6. 1-100
var sum00=0;
for (var i=1;i<=100;i++) {
if (i%2!=0) {
sum00+=i;
}
}
document.write('1-100 :'+sum00+'
');//2500
7. 1-100 3
var sum01=0;
for (var i=1;i<=100;i++) {
if (i%3==0) {
sum01+=i;
}
}
document.write('1-100 3 :'+sum01+'
');//1683
8. 1-100 7
var sum02=0;
for (var i=1;i<=100;i++){
if (i%7!=0) {
sum02+=i;
}
}
document.write('1-100 7 :'+sum02+'
');//4315