抹布什么意思| becky是什么意思| eb病毒是什么病| 寿眉属于什么茶| 箨是什么意思| 更年期是什么意思| 头皮屑多是什么原因怎么去除| 槟榔肝是由什么引起的| 肉松是什么做的| 正视是什么意思| 体重除以身高的平方是什么指数| 嗯是什么意思| lgbtq是什么意思| 子宫癌筛查做什么检查| 草龟吃什么食物| 抽电子烟有什么危害| 离子水是什么水| 拿什么拯救你我的爱人演员表| 朱顶红什么时候开花| 孕妇吃什么血糖降得快| 麻痹是什么意思| 弱水三千是什么意思| 孕妇喝什么水比较好| 家里停电了打什么电话| 黑色素通过什么排出来| 空腹不能吃什么水果| 婀娜多姿是什么意思| 喝什么茶降血脂| 面藕是什么做的| 耻骨疼是什么原因| 莲是什么结构的字| 什么对眼睛好| 脸浮肿是什么病的前兆| 喝酒胃出血吃什么药| 月经调理吃什么好| 曙光什么意思| 梦见自己生二胎是什么意思| 什么头什么面| ckmb是什么意思| 儿童脾胃不好吃什么调理脾胃| 血小板压积偏低是什么原因| 骨髓增生活跃是什么意思| 10月出生是什么星座| 晚上7点到9点是什么时辰| 环孢素是什么药| 芼什么意思| 什么运动可以瘦肚子| 子宫附件包括什么| 肉芽是什么| 过氧化氢阳性是什么意思| b族维生素什么人不能吃| smzco是什么药片| 梨的功效与作用是什么| 梦见着大火了是什么征兆| 腰椎间盘突出和膨出有什么区别| hsv是什么| 天仙配是什么剧种| 唇釉是什么| 后厨打荷是干什么的| qn是什么意思| 氮质血症是什么意思| 网盘是什么东西| 童心未泯是什么意思| 手链断了是什么预兆| 荨麻疹擦什么药| 高血压吃什么好降压快| 以免是什么意思| 试金石是什么意思| 甲状腺阳性是什么意思| 吃得苦中苦方为人上人是什么意思| 体内湿气太重吃什么药能快速除湿| ctu是什么意思| 雀舌是什么茶| 1月22号什么星座| 鸡的贵人是什么生肖| 2000年属什么生肖| 钙化斑是什么意思| 感觉心慌是什么原因| 做梦梦到和别人吵架是什么意思| 鹅吃什么| 脂肪是什么意思| 鼻子上长脓包型痘痘是什么原因| 玄机是什么意思| 正月初七什么星座| 50岁眼睛模糊吃什么好| 肝胆湿热吃什么中成药最好| 喝牛奶就拉肚子是什么原因| 佛珠断了有什么预兆| 糖衣炮弹什么意思| 乌豆是什么| 浙大校长什么级别| 梦见蛇是什么预兆| 治疗褥疮用什么药| 调理肠胃吃什么好| 语重心长是什么意思| 书到用时方恨少下一句是什么| 胃气上逆是什么原因| 炖羊排放什么调料好吃| 交可以组什么词| rh阳性是什么意思| 血凝是什么意思| 攫住是什么意思| 睡觉被口水呛醒是什么原因| 湖南简称什么| 猴跟什么生肖配对最好| 自律性是什么意思| 银杏树叶像什么| 红枣桂圆泡水喝有什么好处和坏处| 为什么发烧会觉得冷| 零星是什么意思| 重症肌无力是什么原因引起的| ao是什么| 酸菜鱼用什么鱼做好吃| 什么的光华| 尿碘是检查什么的| 什么是gay| 为什么会有阴虱子| 1月16日是什么星座| 一什么雨衣| 胸闷气短什么原因| other是什么意思| 花胶适合什么人吃| 新生儿吃什么钙好| 梦游的人为什么不能叫醒| 6月12日什么星座| 肚脐眼为什么会有臭味| 福州立冬吃什么| 海马用什么呼吸| 心悸症状是什么感觉| 狗狗流眼泪是什么原因| 为什么会长花斑癣| 当你从我眼前慢慢走过是什么歌| 肠胃炎可以吃什么水果| 用什么泡脚去湿气寒气| 子宫内膜增厚有什么影响| 一五行属什么| 胃不舒服想吐是什么原因| 什么什么为什么| tf是什么| 十一点是什么时辰| 6.17什么星座| kcal是什么意思| 舒张压偏高是什么原因| 塑形是什么| 血糖低会出现什么症状| 观音成道日是什么意思| 胸膜增厚吃什么药| 什么东西放进去是硬的拿出来是软的| 老实的动物是什么生肖| s档是什么档| 为什么会手麻| 宿命是什么意思| 植物神经紊乱中医叫什么病| 莲字五行属什么| 阴虱有什么症状| 打飞机是什么| 肚脐上面疼是什么原因| 子宫内膜16mm说明什么| 钙吃多了有什么副作用| 胎盘中药叫什么| 急火攻心是什么生肖| 红霉素软膏有什么作用| ca199偏高是什么原因| 只是女人容易一往情深是什么歌| 免疫缺陷是什么意思| 开飞机是什么意思| 穿青人是什么民族| 茧是什么意思| 带刺的玫瑰是什么意思| 什么食物属于发物| 宅男是什么意思| 1976年五行属什么| 小海绵的真名叫什么| 白细胞偏高是什么原因引起的| 苦瓜不能和什么一起吃| 晚上喝柠檬水有什么好处| 什么是结缔组织| 早孕期间吃什么最营养| 鼻子闻不到味道是什么原因| 狗怀孕有什么症状| 眉毛白是什么原因引起的| 靶向治疗是什么意思| 落枕挂什么科| 早上10点是什么时辰| 狗狗产后吃什么下奶多| 助听器什么牌子好| 人体乳头瘤病毒是什么| 什么原因引起甲亢| 酸是什么| 七月份什么星座| 什么烟好抽又便宜| 慢心律又叫什么药| 胎盘低是什么意思| 晚上喝牛奶有什么好处和坏处| 在家里可以做什么赚钱| 孩子头晕挂什么科| 防弹衣是由什么材料制成的| 尿蛋白高吃什么食物好| 归脾丸和健脾丸有什么区别| 什么宽带网速快又便宜| 落汤鸡是什么意思| 减肥期间吃什么主食| 屁为什么是臭的| 丝瓜不能和什么食物一起吃| rx是什么意思| ecc是检查什么的| 为什么低血糖| 4ever是什么意思| 女人右眼皮跳是什么预兆| 西宁有什么好玩的| 乳胶是什么意思| 婴幼儿屁股红擦什么| ir是什么意思| 七月十三什么星座| mrmrs是什么牌子| 尿酸高都有什么症状| 尿酸高吃什么药最好| 牛皮和牛皮革有什么区别| 做梦梦到自己生病了是什么意思| 歹人是什么意思| rag是什么意思| 上午9点是什么时辰| 脚趾第二个比第一个长有什么说法| 小便发白是什么原因| 迷迭香是什么| 动车跟高铁有什么区别| 小孩脱发是什么原因引起的| 植物生长需要什么| 赛博朋克是什么意思| 小孩头晕是什么原因| 翡翠属于什么五行| 头晕有点恶心是什么原因| 苯三酚注射有什么用| 降火喝什么茶| 做面包用什么面粉| 一什么宝石| 石膏的主要成分是什么| 腼腆什么意思| 五红汤什么时候喝最好| 什么中药能降血压| 什么是鸡皮肤图片| 亲子鉴定需要什么| 肚子不舒服是什么原因| 眼睛充血是什么原因造成的| 卸妆用什么最好| 冰火是什么意思| 6月18号是什么星座| 胃有灼热感是什么原因| 昆虫记是什么类型的书| 中午喜鹊叫有什么预兆| 什么是一桌餐| 敬邀是什么意思| 为什么新生儿会有黄疸| 五行属什么怎么看| 屁很多是什么原因造成的| 直视是什么意思| 黑曼巴是什么意思| 麻鸡是什么鸡| 便秘吃什么药快速排便| 乳房结节挂什么科室| 拔罐之后要注意什么| 一阴一阳是什么数字| 后羿属什么生肖| 什么叫跨境电商| 百度Jump to content

From Wikibooks, open books for an open world
百度 泰安市中级人民法院经审理认为,当涉及公众利益的知情权和监督权与保障性住房申请人一定范围内的个人隐私相冲突时,应首先考量保障性住房的公共属性,使获得这一公共资源的公民让渡部分个人信息。

This is the current revision of this page, as edited by 79.150.180.19 (discuss) at 19:29, 21 July 2024 (Changed "the two variables containS" to "the two variables contain".). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)



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]
肠子长息肉有什么症状 什么是相位 有什么好听的歌曲 为什么会有脚气 韬的意思是什么
牛肉不能和什么食物一起吃 6月23号是什么星座 宫颈lsil是什么意思 什么是生物制剂药 多吃丝瓜有什么好处和坏处
什么是阴茎 六十而耳顺是什么意思 火头鱼是什么鱼 天蝎座喜欢什么样的女生 去医院点痣挂什么科
什么是hpv感染 乱花渐欲迷人眼是什么意思 纤维增殖灶是什么意思 新生儿拉肚子是什么原因引起的 阴囊湿疹用什么药膏
ida是什么意思hcv8jop3ns2r.cn 新生儿湿肺是什么意思hcv8jop6ns7r.cn 周围神经病是什么病hcv9jop5ns3r.cn 母仪天下是什么意思hcv7jop6ns0r.cn 症结是什么意思hcv9jop3ns3r.cn
皮肤晒伤用什么药hcv9jop4ns7r.cn 酱油色尿是什么原因hcv8jop0ns6r.cn 还替身是什么意思hcv9jop6ns4r.cn 蹭饭吃是什么意思hcv9jop3ns3r.cn 吃月饼是什么生肖hcv8jop8ns3r.cn
大骨节病是一种什么病hcv7jop9ns3r.cn 成人晚上磨牙是什么原因hcv8jop8ns7r.cn 男人太瘦吃什么可以长胖hcv9jop3ns4r.cn 发烧什么症状hcv8jop1ns9r.cn 鹿角有什么功效和作用hcv8jop8ns4r.cn
黄豆酱做什么菜好吃hcv8jop4ns6r.cn 为什么会肛裂hcv8jop8ns4r.cn 朋友是什么jingluanji.com study是什么意思hcv7jop6ns6r.cn 头部MRI检查是什么意思zhongyiyatai.com
百度