什么是痰湿| 心脏难受是什么原因| 年年有今日岁岁有今朝什么意思| 脑白质病变是什么意思| 什么叫痛风| 白癜风早期症状是什么| 什么人一年只工作一天| 小211是什么意思| 爱理不理是什么意思| 肺结节是一种什么病| 周杰伦什么病| 手指痛挂什么科| 蛮什么意思| 月子能吃什么水果| 肥胖纹什么样子| 眉目的比喻义是什么| 什么样的人容易垂体瘤| 1970年五行属什么| 520送男朋友什么礼物| 李嘉诚是什么国籍| 组胺过敏是什么意思| 肺不好有什么症状| 黑眼圈是什么原因| 灰飞烟灭是什么意思| 肛门镜检查能查出什么| 乳腺增生吃什么药好| ed是什么病| 剪刀是什么生肖| 秋天是什么样子的| 猪横脷是什么| 什么是梅雨季节| 紫苏泡酒有什么功效| 黄疸高是什么原因| 上火喝什么饮料| 什么是尿潴留| 什么是面瘫| 过山风是什么蛇| 孕妇为什么不能吃桃子| 经期同房会有什么后果| 吕布的武器叫什么| 汗管瘤用什么药能去掉| 04年是什么生肖| 市场部是做什么的| 抗凝药是什么意思| 宝宝为什么喜欢趴着睡| 总胆红素偏高是什么病| 加拿大现在什么季节| 眼睛视力模糊用什么眼药水| 面瘫是什么引起的| 排湿气最快的方法吃什么| 黄金分割点是什么| 宫寒吃什么好| 三顾茅庐的顾是什么意思| 硼酸是什么| 小孩舌头发白什么原因| pr是什么职位| 蟑螂喜欢吃什么| 夜叉是什么| 林彪为什么叛逃| 嘴巴苦什么原因| 阑尾炎痛起来什么感觉| 猪心炖什么补气补血| 打摆子什么意思| 阿sir什么意思| 短装是什么意思| 失眠是什么原因| 肠子粘连有什么办法解决| 牛肉和什么炒最好吃| 谁发明了什么| 黄疸偏高有什么危害| 肝胆胰腺属于什么科| 橄榄枝象征着什么| 06属什么生肖| 7月18日什么星座| 鼻涕倒流吃什么药效果好| 唐僧姓什么| 梦见和尚是什么预兆| 心脏彩超ef是什么意思| 肝胃郁热吃什么中成药| 化痰止咳吃什么药最好| 洛阳以前叫什么名字| 吃什么快速排便| 马赛克是什么| 毛骨悚然是什么意思| 怀孕三个月吃什么对胎儿好| 油性皮肤适合用什么牌子的护肤品| 大刀阔斧是什么意思| 为什么同房会痛| 绿巨人是什么意思| 间质瘤是什么性质的瘤| 为什么会偏头痛| 鳞状上皮化生什么意思| 此刻朋友这杯酒最珍贵是什么歌| 五爷是什么菩萨| 92年是什么年| cd代表什么意思| 浮屠是什么意思| 为什么医院开的钙都是碳酸钙| 怀孕后吃避孕药有什么后果| 芹菜吃多了会有什么影响| 兔子不吃窝边草是什么意思| 为什么拉尿会刺痛| 调岗是什么意思| lg是什么| 什么人容易得肺结核| 可悲可叹是什么意思| 县纪委副书记什么级别| 熬夜吃什么对身体好| 脂溢性皮炎头皮用什么洗发水| 吃饭不规律会导致什么问题| puma是什么牌子| 九七年属什么生肖| 蝎子怕什么| 膀胱钙化是什么意思| 尿常规红细胞高是什么原因| 怎么知道自己五行缺什么| 虬是什么动物| 冷暖自知上一句是什么| 黄芪长什么样子的图片| 看鼻子挂什么科| 蚊子有什么用| 电脑关机快捷键是什么| 泡沫是什么材料做的| 高烧吃什么药退烧快| 撇清关系是什么意思| 七夕送什么礼物好| avia是什么牌子| theme什么意思| 喝茶喝多了有什么坏处| 非均匀性脂肪肝是什么意思| 联合创始人是什么意思| 福报是什么| cheese什么意思| 七八年属什么| 谷氨酰基转移酶低是什么原因| hca是什么意思| 亲吻是什么意思| 男人射精是什么感觉| 6月19日是什么日子| 私处为什么会发黑| 甲状腺结节挂什么科| 一度是什么意思| 肚脐左侧是什么器官| 头部紧绷感是什么原因| 儿童肚子疼吃什么药| 伊朗用什么语言| 数字绘画是什么| 质数是什么| 造影检查是什么意思| 喝什么水好啊| 梵文是什么意思| mc什么意思| 打太极是什么意思| 5月13号是什么星座| 水猴子长什么样| 叶酸什么时候吃最好| 结婚有什么好处| 煞星是什么意思| 为什么手脚老是出汗| 坐月子不能吃什么| 包皮开裂是什么原因| 医生为什么叫大夫| 急性胰腺炎是什么病| 胃病不能吃什么| 男人吃什么药时间长| 榴莲有什么品种| 什么病会引起背部疼痛| 粘纤是什么面料优缺点| 怕热出汗多是什么原因| 湿气重有什么表现| 香蕉不能和什么水果一起吃| 下巴老是长痘痘是什么原因| 长一智的上一句是什么| 说风就是雨什么意思| _什么字| 什么鱼最好养不容易死| 巳火是什么火| 急性肠炎吃什么食物好| 北斗是什么| wbc白细胞高是什么原因| 28岁属相是什么生肖| 贼头贼脑是什么生肖| 凝血四项是检查什么的| 苹果什么时间吃最好| 睡觉脚抽筋是什么原因引起的| 可乐杀精是什么意思| 猫砂是什么| 抗酸杆菌是什么| 什么东西最吸引蛇| 胰腺在什么位置图片| 低血压是什么意思| 莓茶什么人不适合喝| 榴莲和什么相克| 咽喉炎吃什么药最好| 看正月初一是什么生肖| chris是什么意思| 去脂肪粒最有效的药膏叫什么| 袍哥什么意思| 成人大便绿色是什么原因| 11月份什么星座| 窘迫是什么意思| 伤口增生是什么原因造成的| 左手麻木是什么原因引起的| 纳差是什么症状| 骨质增生是什么症状| 台湾什么时候回归的| 12月16是什么星座| 吃杨梅有什么好处和功效| 洗手做羹汤是什么意思| 劳模是什么意思| 女性肛裂要抹什么药好| 1月24号什么星座| 尿酸低有什么危害| 10月26日什么星座| 屁股出汗多是什么原因| 体检需要带什么| 胎盘低置需要注意什么| 宫颈出血是什么原因| 四川有什么特产| 似曾相识是什么意思| 吃什么能提高性功能| 肌钙蛋白高是什么意思| 荪是什么意思| 子宫痉挛是什么症状| 灌肠什么感觉| max是什么意思| 偈语是什么意思| 黄芪长什么样| 黄瓜和什么不能一起吃| 久而久之下一句是什么| acl是什么意思| as是什么| 抽烟手抖是什么原因| 丙字五行属什么| 什么冰箱好| 涂防晒霜之前要涂什么| 什么的眨眼| 喝什么祛湿气效果最好| 容易出汗什么原因| 棉纱是什么面料| 出院小结是什么| 挫伤用什么药| 12min是什么意思| 睡觉腿抽筋是什么原因| 做梦梦见鬼是什么意思| 男士吃什么壮阳最厉害| 三超是指什么| 跑马是什么意思| 唐氏综合征是什么意思| 01是什么意思| 生死劫是什么意思| 冰箱底部漏水是什么原因| 什么时候开始胎教| 女生腰疼是什么原因| 康熙叫什么名字| 安全生产职责是什么| 党内警告处分有什么影响| 头孢过敏什么症状| 包场是什么意思| 牙龈上火肿痛吃什么药| 女生爱出汗是什么原因| 背后长痘痘是什么原因| gigi 是什么意思| 鑫字属于五行属什么| 百度Jump to content

全国优秀县委书记廖俊波遇车祸殉职:曾被赞有大

From Wikibooks, open books for an open world
百度 会议期间,与会专家考察了奥体博览城、拱宸桥桥西历史文化街区、杭州工艺美术博物馆、运河水上巴士、西湖综保工程等城市建设管理的先进经验。



Most programming languages are composed of 'bricks' like tokens (keywords, variables, operators, ...), expressions like myArray.length + 1, statements (delimited by ;), blocks {...}, functions, and modules. At first glance, the execution of the program follows the sequence of statements, top down. But in nearly all cases, it is necessary that the program does not run in the strict order of the written statements. Instead, some parts must run only if certain conditions apply, and others will be omitted and run under different conditions. Or, it may become necessary that some parts are executed repetitively. Other parts may run in parallel and get synchronized later. Or, a function of a different module must compute a value before the next statement can be executed.

In this hierarchy of 'language bricks' the term block is essential for the understanding of the program flow. In JavaScript, a block is a sequence of zero or more statements (or smaller blocks) that are surrounded by braces { // zero or more statements }. The language constructions we discuss here invoke or repeat blocks.

if / else

[edit | edit source]

The if / else statement (yes, it's a single statement, even though it contains other statements in its blocks) invokes the execution of one of two blocks depending on the evaluation of a condition. The evaluation returns a boolean value. If it is true, the first block is executed; if it is false, the second block is executed. The respectively other block is skipped over.

if ( condition ) {
  // block of statements
} else {
  // block of statements
}

The else part is optional, i.e. it's possible to use if without the else part and its block.

if ( condition ) {
  // block of statements
}

An example:

"use strict";
const a = 3;
const b = "3";
if (a == b) {
  alert("The two variables contain the same value, but may have different data types.");
} else {
  alert("The two variables contain different values.");
}

// an example without 'else'
const c = 6 / 2;
if (a === c) {
  alert("The two variables contains the same value and are of the same data type.");
}

If one of the two blocks contains exactly ONE statement, the braces can be omitted. But for the clearness of the code, we recommend the use of a unified syntax with braces.

// same as above; but this abbreviated syntax is not recommended.

"use strict";
const a = 3;
const b = "3";

if (a == b) alert("The two variables contains the same value, but may have different data types.");
else alert("The two variables contain different values.");

const c = 6 / 2;
if (a === c) alert("The two variables contains the same value and are of the same data type.");

In many cases, the situation demands more complex decisions than a simple true/false alternative. For example, you may want to know whether a number is negative, zero, or positive. In such cases, a solution might look like this:

"use strict";
const x = 3;

if (x < 0) {
  alert("The number is negative.");
} else {
  // x is equal or greater than 0
  if (x === 0) {
    alert("The number zero.");
  } else {
    alert("The number is positive.");
  }
}

You can shorten this code a bit without losing clarity. Because the first else block contains only a single statement - namely the second if - you can omit its braces and combine the first else and the second if within one line.

"use strict";
const x = 3;

if (x < 0) {
  alert("The number is negative.");
} else if (x === 0) {
  alert("The number is zero.");
} else {
  alert("The number is positive.");
}

This is a clear and often-used programming style. It's used in situations where you have a manageable number of choices or where you have to make decisions with multiple variables.

switch

[edit | edit source]

If the number of decisions grows significantly, the code gets clearer if you use the switch statement instead of a long list of else if conditions.

The switch statement evaluates an expression and steers the flow of statements based on the comparison of its result with the labels behind the keyword case.

"use strict";

const myVar = "a";

// evaluation takes simple variables as well as complex expressions
switch (myVar.toUpperCase()) {
case "A":
  // …
  break;
case "B":
  // …
  break;
default:   // analog to 'else' without any further 'if'
  // …
  break;
}

If the result of the evaluation matches one of the labels, JavaScript executes the following statements up to the next break or the end of the entire switch. If none of the labels match, execution continues at the default label, or - if none is present - skips the switch statement entirely.

Labels are literals or expressions; e.g., case (2 + 1).toString(): is possible.

As soon as a break statement is reached, the execution of the switch gets terminated. Normally it appears at the end of each case to prevent execution of the code of the following cases. But it can be omitted if you intentionally want to execute them in addition to the current ones. In the following example, the same code will run for i equal to 1, 2, or 3.

"use strict";

const i = 2;

switch(i) {
case 1:
case 2:
case 3:
  // …
  break;
case 4:
  // …
  break;
default:
  // …
  break;
}

Because the expression to be evaluated as well as the labels can be complex expressions, it's possible to build very flexible constructions.

"use strict";

const i = 2;

switch(true) { // in this example it's a constant value
case (i < 10):
  alert("one digit");
  break;
case (i >= 10 && i < 100):
  alert("two digits");
  break;
default:
  // …
  break;
}


The continue keyword does not apply to the switch statement.

try / catch / finally

[edit | edit source]

If there is a possibility that a runtime error might occur, you can 'catch' that error and perform meaningful actions to handle the situation. E.g., a network connection or a database might no longer be available; a user input leads to a division by zero; ... .

try {
  // critical block where errors might occur
} catch (err) {
  // block to handle possible errors. Normally not executed.
} finally {
  // block that will be executed in ALL cases
}
"use strict";

const x = 15;
let average;
try {
  // block with critical statements
  x = x + 5;
  average = x / 0;
  alert("The average is: " + average);
} catch (err) {
  // block to handle possible errors
  alert("Something strange occurs. The error is: " + err);
} finally {
  // block that will be executed in ALL cases
  alert("End of program.");
}

If one of the statements in the critical block raises a runtime error, the execution of its remaining statements is omitted. Instead, the execution invokes the catch block. Lastly, the finally block is executed.

Please note that the finally block is executed in all cases, regardless of whether a runtime error occurs or not. That even applies if the critical or the catch block executes a return statement.

throw

[edit | edit source]

In the above example, the JavaScript engine throws an exception by itself. In other situations, the JavaScript engine acts in one way or another, but you may want to see it treated differently. E.g., in the case of a division by zero, the engine doesn't throw an error; it assigns Infinity to the result and jumps to the following statement. If you want a different behavior, you can create and throw exceptions by your own program.

"use strict";

const x = 15;
let average;
try {
  // block with critical statements
  average = x / 0;
  // or: const z = "abc"; average = z / 0;
  if (average === Infinity || Number.isNaN(average)) {
    // Throw your own exception with any text
    throw "Error during division. The result is: " + average;
  }
  alert("The average is: " + average);
} catch (err) {
  // block to handle possible errors
  alert("Something strange occurs. The error is: " + err);
} finally {
  // block that will be executed in ALL cases
  alert("End of program.");
}

If an exception occurs - generated by the JavaScript engine or by your program - and is not caught by a catch block, the script terminates or - if it is a function - it returns control to the calling function. The error handling may be implemented there or in one of the functions which have been called it.

"use strict";

const answer = prompt("How old are you?");
const age = Number(answer);

if (isNaN(age)) {
  throw answer + " cannot be converted to a number.";
  // The script terminates with this message (it's not a function)
}
alert("Next year you will be " + (age + 1));

Exercises

[edit | edit source]
... are available on another page (click here).

Loops

[edit | edit source]

Loops and iterations are other cases where the sequential flow of statements is manipulated by surrounding language constructs. This is described on the next page.

See also

[edit | edit source]
为什么会梦到一个人 professional是什么意思 眼珠子发黄是什么原因 容易口腔溃疡什么原因 马的尾巴有什么作用
柠檬片泡水喝有什么功效和作用 夏占生女是什么意思 痛风挂什么科室 发痧用什么方法好得快 闭口是什么样子图片
人为什么要火化 吃饭的时候恶心想吐是什么原因 我是什么课文 秦始皇的母亲叫什么名字 妈妈的表姐叫什么
枫叶的花语是什么 腊肉炒什么菜最好吃 口干吃什么药 神的国和神的义指的是什么 体重一直不变说明什么
红斑狼疮什么症状hcv7jop9ns1r.cn 任督二脉是什么意思hcv7jop6ns9r.cn 狐狸和乌鸦告诉我们什么道理hcv8jop7ns5r.cn 鄙人不才是什么意思hcv8jop5ns5r.cn 女人胸疼是什么原因hcv7jop5ns2r.cn
黛是什么颜色dajiketang.com 点痣不能吃什么东西naasee.com 井底之蛙的寓意是什么hcv7jop6ns4r.cn 人为什么会脸红hcv8jop1ns4r.cn 深圳属于什么气候hkuteam.com
帝女花讲的是什么故事hcv8jop8ns3r.cn 沙棘原浆什么人不能喝hcv9jop5ns6r.cn 什么是复韵母hcv7jop7ns1r.cn 乳清粉是什么ff14chat.com 低密度脂蛋白胆固醇是什么意思hcv7jop9ns4r.cn
医学hr是什么意思hcv8jop9ns1r.cn 缺钾吃什么水果hcv7jop6ns4r.cn 甲状腺手术后有什么后遗症hcv9jop1ns2r.cn 孟力念什么hcv9jop5ns1r.cn 一什么睡莲0297y7.com
百度