认识if语句
所有的条件语句都是同步代码
if关键字表示如果
()条件表达式
{ }只有( )是true或者表达式的返回值为true才会只给你{ }代码
if(true){
console.log('执行if条件语句')
}
条件表达式,需要根据条件去查找
else的作用
else是配合if一起使用的,当除了if语句的条件之外,出现的其他条件,else相当于if的补充条件
if(false){//如果满足条件时执行
console.log('满足条件');
}else{//不满足上面的条件时执行
console.log('不满足条件');
}
练习
1.输入a,b,c三个数,打印出最大的。
var a = parseInt(window.prompt(‘input’));
var b = parseInt(window.prompt(‘input’));
var c = parseInt(window.prompt(‘input’));
if(a>b){
if(a>c){
document.writw(a);
}else{
document.write(c);
}
}else{
if(b>c){
document.writw(b);
}else{
document.write(c);
}
}
随便输入一个数字,当数字是最大的时候会显示在页面中
2.著名的斐波那契额数列——1 1 2 3 5 8输出第n项
var n = parseInt(window.prompt('input'));
var first = 1;
var second = 1
var thir;
if(n>2){
for(var i = 0;i < n-2; i++){
thir = first + second;
first = second;
second =thir;
}
document.write(thir);
}else{
document.write(1);
}