`
chaotienhisang
  • 浏览: 2565 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

How to Do Everything With JavaScirpt (条件语句)翻译

阅读更多

 

Control Program Flow with Statements

用语句控制程序流程

     A statement is the basic action item in any program code.In effect,each statement is telling the computer to do something,Statements can be divided up into five categories.

    在任何程序代码中语句是最基本的动作元素,事实上,每一条语句将要告诉计算机做的事情,语句大体上被分为五个分类。

       Conditional //条件

       Loops       //循环

       Object manipulation //面向对象操作

       Expresions          //表达式

    The typical JavaScript program uses statements that fall into each of those categories. Often,statements inside a program are organized into functions and classes as well, to make the program easier to manager and more effiecient to develop .

    典型的JavaScript程序在每一个分类中都使用语句,为了使程序的便于管理和更高效的开发,语句经常在程序中被编译成函数(方法)以及类

Execute Code Conditionally

执行代码条件

    Computer programs almost always contain conditional statements. There are two conditional statements in JavaScript: if and switch. The if statement allows the program to choose one of two alternatives, based on some predefined factor. In real life, you might decide that, if it were not raining, you would like to go to the baseball game. Of course, if it were raining, you would then decide to stay home. You can make that same decision in a JavaScript program with the following code:

   电脑程序大多会包含语句,在javascript有两种条件语句:if语句和swith语句,基于一些被预先定义的因素if语句允许程序二选一,就像在现实生活中一样,你可以选择不下雨的时候出去打棒球,当然如果不幸下雨了你将不得不待在家里。在JavaScript程序中你可以使用下面的语句做出同样的选择:

      function stay_or_go(raining){

       if(raining ==false){

        return "Go to baseball game!";

       }else{

        return "Got to stay home today.";

       }

     }

    So you see, conditional statements give programs a choice between two or more alternatives in much the same way we make those choices in real life.

   正像你看到的条件与据给程序在连个或者更多的程序中选择你需要的和现实生活中的情况没有什么差别。

The if Statement

条件语句

 

    Depending on how you use it, the if statement can be very simple or very complex. The if statement can be used in the following ways.

条件语句既可以很复杂也可以很简单,这完全取决于你使用的情况。条件语句可以有以下方式

   Syntax 

Use

if (expression) {

statements;

}

If expression evaluates to true, execute statements.

如果expreesion条件是真在执行语句statements

 

if (expression) {

statements1;

}

else {

statements2;

}  

If expression evaluates to true, execute statements1.Otherwise, execute statements2.

  如果exipression条件为真执行statements1否则执行条件satements2.

 

if (expression1) {

statements1;

}else if (expression2) {

statements2;

}         

 else {statements3;

}    

If expression1 evaluates to true, execute statements1.Otherwise, if expression2 evaluates to true, execute  statements2. Otherwise, execute statements3.

如果expression1条件为真那么执行statement1其次如果expression2条件为真执行statements2否则执行statement3;

 

 

    The if statement evaluates an expression to determine which set of statements to execute,if any. Since the if statement expects a Boolean expression (one that evaluates to either true or false), it will try to convert expressions of other data types to either true or false.

    if语句会执行哪一个语句的取决于对expression的判断,如果可能,if语句以为expression是一个Boolean(或真或假),他会尝试把其他数据类型转化为Boolean类型

 

Tip:JavaScript makes certain assumptions when converting from other data types to Boolean. The strings “true” and “false” evaluate to the Booleans true and false. The integers 1 and 0 are also converted to the Booleans true and false, respectively.

    JavaScript把其他类型的数据转化为boolean时会假设,字符串"true"和字符串"false" ,整型数字10都会被各自看作truee或者fasle

 

    In computer programming terminology, an expression is a piece of code that, when evaluated, returns a value. In JavaScript, the following can be used as expressions.

    计算机程序技术,expression可以是一段程序代码返回的值 JavaScript中下面几种可以被用作expressions.

Expression  

   Example

 

A variable

if ( x ) {statements;}

A function that returns a value

if ( myfunc(x) ) {statements;}

A literal 

if ( true ) {statements;}

Variables, functions, and literals combined using operators

if ( a > 5 ) {statements;}

 

    Even the same if statement can be written in at least four different ways, all of which are valid:

    甚至于if语句能被书写成至少四种方式 每一种都是有效的。

    if (expression)

        statement;

    if (expression)

        statement;

    if (expression) {

         statements;

       }

    if (expression) {

         statements;

       }

 

     The various preceding forms of the if statement are technically equivalent. The first two can include only one statement. Notice that the third and fourth forms use curly brackets, { and }, to enclose the statements. Statements enclosed in curly brackets are generally treated as a group, called a statement block.

      上面各种不同的if语句在技术上说是相同的。前面两个仅仅是有一个语句.注意后面的两个有一个大括号包裹,被打括号包裹起来的的一般按照一个组别进行处理称之为 "语句块"

 

     The if statement can include an optional else clause, to decide between one of two alternatives. This form of the statement is sometimes called the if-else statement:

      if (expression) {

         statements1;

      }

      else {statements2;

      }

     if语句可以包含一个else选项,选择二者中的一个,这种构成也为称之为if-else语句

 

    The else clause allows you to specify a statement or group of statements that will be executed if the expression does not evaluate to true. Only one else clause is allowed in any if statement. Again, the statement can be used with or without the curly brackets.

    如果if语句的expression条件不为true那么将执行else分句指定的单个语句或者一组语句.一个else分句被允许追加到任何一个if语句.此外,语句可以使用或者不使用大括号包裹的语句块。

 

    Finally, the if statement can be used to choose between one of three or more alternatives. With the introduction of the else-if clause, the if statement can include multiple expressions that will each be evaluated until either one of them evaluates to true or the else clause is encountered. Multiple else-if clauses can be included, but keep in mind that the else-if clause must always precede any else clause.

    通过使用eslse-if从句if语句能被用于选择三个或者更多选项中的一个,if语句能包含多重expressions每一个都被判断是否是true以决定是否else从句被执行,多重else-if使用的的时候必须在esle从句前。

  

  

      if (expression) {

          statements;

      } else if (expression) {

      statements;

      } else {

      statements;

      }

 

Tip:If you find yourself using more than two or three else-if clauses in a single if statement,you may want to consider using a switch statement instead, as described in the following section.

 

     在一个语句中使用超过三个esle-if从句的时候,可以用switch语句

 

  未完....

 

分享到:
评论

相关推荐

    javascirpt

    javascirpt的一些经典特效 第一章.文本特效类 第二章.图片特效类 第三章.鼠标键盘类 第四章.按钮特效类 第五章.日期时间类 第六章.计数转换类 第七章.系统检测类 第八章.页面特效类 第九章.菜单特效类 第十章.密码...

    JAVASCIRPT

    通用javaScript的验证框架

    JAVAscirpt代码

    JavaScript的核心概念包括变量、数据类型(如字符串、数字、布尔值、对象等)、控制结构(如条件语句、循环)、函数以及事件处理。在JavaScript中,我们可以使用DOM(Document Object Model)来操作HTML元素,实现...

    JavaScirpt的基本用法

    JavaScript语法与C++和Java有相似之处,但更宽松,它允许在一行内编写多条语句。变量声明使用`var`关键字,如`var myVar = "Hello, World!";`。数据类型包括字符串、数字、布尔值、null、undefined以及对象。在...

    javascirpt权威指南中英版

    But you do not need to know any of those languages, or be familiar with those terms, to use this book and learn JavaScript. The name “JavaScript” is actually somewhat misleading. Except for a ...

    javascirpt 小技巧 javascirpt 小技巧

    JavaScript 是一种广泛应用于网页和网络应用的脚本语言,它在浏览器环境中运行,为用户提供交互式体验。以下是一些JavaScript的小技巧和知识点: ...而 `event.srcElement.tagName` 和 `event.srcElement.type` 分别...

    Javascirpt定义类详细介绍

    在JavaScript中,类(Class)是ES6引入的一种新的语法糖,用来实现面向对象编程。在ES5之前,我们通常使用函数构造器和原型链来模拟类的行为,但ES6的类提供了一种更加简洁、易读的语法,使得代码更接近传统的面向...

    JavaScirpt搜索关键字提示

    在"JavaScirpt搜索关键字提示"这个主题中,我们主要讨论的是如何利用JavaScript实现一个搜索关键字的下拉提示功能,这种功能常见于搜索引擎或网站搜索框中,能够为用户提供便捷的搜索建议。 在描述中提到的"中英文...

    javascirpt源码学习

    let, const)、数据类型(原始类型如字符串、数字、布尔值,以及引用类型如对象、数组)、运算符(算术、比较、逻辑、赋值等)、流程控制(条件语句if...else, switch,循环for, while, do...while)以及函数定义和...

    echarts_javascirpt

    echarts_javascirpt

    JavaScirpt经典教程+demo

    1. **基础语法**:JavaScript的基础包括变量、数据类型(如字符串、数字、布尔、null、undefined)、操作符(算术、比较、逻辑等)、流程控制(条件语句、循环语句)以及函数的使用。 2. **对象与数组**:...

    javaScirpt编程好东东

    此外,书中可能还会涉及JavaScript的基本语法,如变量声明、数据类型(包括原始类型和引用类型)、控制流程(条件语句、循环语句)以及函数。函数是JavaScript的核心,不仅用于封装代码,还可以作为一等公民,即函数...

    javascirpt图片切换效果

    JavaScript是一种广泛应用于网页和网络应用中的编程语言,它在网页动态效果和用户交互方面起着至关重要的作用。本文将深入探讨如何使用JavaScript实现图片切换效果,以提升网站的用户体验。 首先,图片切换效果通常...

    javascirpt树形菜单例子

    在给定的“javascirpt树形菜单例子”中,我们可以深入探讨以下相关知识点: 1. **DOM操作**:JavaScript树形菜单的实现通常涉及对DOM(文档对象模型)的操作。通过创建、修改或删除DOM元素,我们可以动态地构建和...

    Reactive for Javascirpt介绍

    RxJS的官方介绍和使用教程已经提供了相当丰富的学习资源,但由于国内资源相对较少,阮老师的ES6入门教程和ES6TS中文文档被翻译后用作参考,这显示了社区对文档和资源本地化的巨大需求。在文档的使用过程中,若遇到...

    javascirpt对象与继承

    ### JavaScript中的对象与继承 #### 一、JavaScript对象的基础概念 在JavaScript中,对象是一种非常重要的数据类型,它能够存储键值对的形式来表示复杂的结构数据。这些键通常被称为属性名,而对应的值则称为属性...

    HTML/javascirpt/css

    ... JavaScript是一种强大的客户端脚本语言,用于增加网页的交互性。...ES6(ECMAScript 6)的推出为JavaScript提供了更多现代编程特性,如类、模块、箭头函数等。...CSS(Cascading Style Sheets)则负责网页的样式和布局...

Global site tag (gtag.js) - Google Analytics