`
chaotienhisang
  • 浏览: 2513 次
  • 性别: 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语句

 

  未完....

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics