Process control
During the execution of a program, the execution sequence of each code has a direct impact on the result of the program. Many times we need to control the execution sequence of the code to achieve the function we want to complete.
There are three main structures for process control
- Sequence structure
- Branch structure
- Cyclic structure
Sequential process control
The program will be executed in sequence according to the sequence of the code!
Branch flow control if statement
Judging age
var age=prompt('input your age:')
if(age>=18)
alert('Let us fly');
else
alert('can not fly');
Judging Leap Year
var year=prompt('input the year:')
if(year%4==0&&year%100!=0||year%400==0){
alert('yes');
}else{
alert('no');
}
Ternary expression
Conditional expression? Expression 1: Expression 2
If the result of the conditional expression is true, return expression 1, otherwise return the value of expression 2
Zero padding case
var time=prompt('input a number:')
console.log(time<10?'0'+time:time);
Branch flow control switch statement
var fruit=prompt('input your fruit:')
switch(fruit){
case 'apple':
alert('the price is 3.5');
break;
case 'pear':
alert('the price is 35');
break;
default:
alert('there is no kind of this furit!');
}