Home > Web Front-end > JS Tutorial > Detailed explanation of javascript flow control statements if, if else and multiple if else syntax examples

Detailed explanation of javascript flow control statements if, if else and multiple if else syntax examples

伊谢尔伦
Release: 2017-07-24 09:32:52
Original
3613 people have browsed it

Make judgment (if statement)

The if statement is a statement used to execute the corresponding code based on the condition being true.
Grammar:


 if(条件){
   条件成立时执行代码
 }
Copy after login

Example: Suppose you apply for a web front-end technology development position. If you know HTML technology and you succeed in the interview, you are welcome to join the company.


<script type="text/javascript">
   var mycarrer = "HTML";
   if (mycarrer == "HTML"){
     document.write("你面试成功,欢迎加入公司。");
   }
 </script>
Copy after login

if...else statement

if... The else statement is the code that executes when the specified condition is true, and the code after else is executed when the condition is not true.
Grammar:


 if(条件){
   条件成立时执行的代码
 }else{
   条件不成立时执行的代码
 }
Copy after login

Example: Suppose you apply for a web front-end technology development position. If you know HTML technology and you succeed in the interview, you are welcome to join the company, otherwise you will not be interviewed. Successful, cannot join the company.


<script type="text/javascript">
   var mycarrer = "HTML"; //mycarrer变量存储技能
   if (mycarrer == "HTML"){
     document.write("你面试成功,欢迎加入公司。"); 
   }else{
     //否则,技能不是HTML
     document.write("你面试不成功,不能加入公司。");
   }
 </script>
Copy after login

Multiple judgment if...else nested statements
To be in multiple groups of statements To select a group to execute, use if..else nested statements.
Grammar:


 if(条件1)
 { 条件1成立时执行的代码}
 else if(条件2)
 { 条件2成立时执行的代码}
 ...
 else if(条件n)
 { 条件n成立时执行的代码}
 else
 { 条件1、2至n不成立时执行的代码}
Copy after login

Example: According to the United Nations World Health Organization’s age classification standards, those under 44 years old are young people; those between 45 and 59 years old are middle-aged people. young people. The elderly are those between 60 and 89 years old; the longevity elderly are those over 90 years old. Zhao Hong is 99 years old this year. Which age group does she belong to?


<script type="text/JavaScript">
   var myage =99;//赵红的年龄为99
   if(myage<=44){
     document.write("青年");
   }else if(myage<=59) {
     document.write("中年人");
   }else if (myage<=89){
     document.write("老年人");
   }else {
     document.write("长寿老年人");
   }
 </script>
Copy after login

The above is the detailed content of Detailed explanation of javascript flow control statements if, if else and multiple if else syntax examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template