[JS] 第五课:语句

1.条件语句

  1. if…else…
var price=1000;
if(price>2000)
{
	console.log('expensive');
}
else if(price<2000&&price>500)
{
	console.log('a proper price');
}
else
{
	console.log('too cheap');
}
  1. switch语句
var choice=2;
switch(choice){
	case 1:
		console.log('this is 1');
		break;
	case 2:
		console.log('this is 2');
		break;
	default:
		console.log('this is a default choice');
		break;
	}

2.循环语句

  1. while语句
var loop=1;
while(loop<=10)
{
	let sentence=`this is the ${loop}th loop<br/>`;
	document.write(sentence);
	loop++;
}
  1. do…while…
  2. for语句
for(var loop=1; loop<10; loop++)
{
	document.write(`this is the ${loop}th loop<br/>`);
}
  1. continue/break
  2. for in 语句
var nameList={
	"Tom":95,
	"John":100,
	"Sahra":86
};
for(student in nameList)
{
	if(nameList.hasOwnProperty(student))
		document.write(`${namelist.student}<br/>`);
}

3.异常捕获语句

try{

} catch(exception)
{

} finally{

}

[例]

try{
	document.write(nameList);
}
catch(exception)
{
	document.write(exception);
}
finally{
	alert('The end of test!');
}

4.With 语句

var Kitty={
	age:15,
	friend:{
		name:'Tom',
		age:2
	}
};
with(Kitty)
{
	document.write(`${friend.name} ${friend.age} `);
}
发布了51 篇原创文章 · 获赞 5 · 访问量 4184

猜你喜欢

转载自blog.csdn.net/qq_43519498/article/details/103812021