Flow control statements
Flow control statements are key components in programming used to manage the order and logic of code execution. In JavaScript, common flow control statements include conditional statements, loop statements, and jump statements, which help developers implement complex logical judgments and repetitive operations.
Conditional Statements
Conditional statements are used to execute different blocks of code based on different conditions. The most common conditional statements in JavaScript are if...else
and switch
.
if...else Statement
The if...else
statement executes a block of code if a condition is true and another block if the condition is false. The syntax is as follows:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
For example, checking if a number is positive:
let num = 10;
if (num > 0) {
console.log("Positive");
} else {
console.log("Non-positive");
}
You can also use else if
to handle multiple conditions:
let score = 85;
if (score >= 90) {
console.log("Excellent");
} else if (score >= 80) {
console.log("Good");
} else if (score >= 60) {
console.log("Pass");
} else {
console.log("Fail");
}
switch Statement
The switch
statement executes different blocks of code based on different values. The syntax is as follows:
switch (expression) {
case value1:
// Code block 1
break;
case value2:
// Code block 2
break;
default:
// Default code block
}
For example, outputting different messages based on the day of the week:
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Other");
}
Note: The break
statement is used to exit the switch
; otherwise, execution will continue to the next case
.
Loop Statements
Loop statements are used to repeatedly execute a block of code until a specific condition is met. Common loop statements in JavaScript include for
, while
, and do...while
.
for Loop
The for
loop is used to repeat code execution when the number of iterations is known. The syntax is as follows:
for (initialization; condition; increment) {
// Loop body
}
For example, printing numbers from 1 to 5:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
The for
loop can also be used to iterate through an array:
let arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
while Loop
The while
loop repeats code execution as long as the condition is true. The syntax is as follows:
while (condition) {
// Loop body
}
For example, printing numbers from 1 to 5:
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
do...while Loop
The do...while
loop is similar to while
but guarantees at least one execution of the loop body. The syntax is as follows:
do {
// Loop body
} while (condition);
For example, printing numbers from 1 to 5:
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
for...of and for...in Loops
for...of
is used to iterate over iterable objects (e.g., arrays, strings):
let arr = [1, 2, 3];
for (let item of arr) {
console.log(item);
}
for...in
is used to iterate over the properties of an object:
let obj = { a: 1, b: 2, c: 3 };
for (let key in obj) {
console.log(key + ": " + obj[key]);
}
Jump Statements
Jump statements are used to alter the flow of code execution and include break
, continue
, and return
.
break Statement
The break
statement is used to exit a loop or switch
statement. For example, exiting a loop when a specific condition is met:
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
continue Statement
The continue
statement skips the remaining part of the current loop iteration and proceeds to the next iteration. For example, skipping even numbers:
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
return Statement
The return
statement is used to return a value from a function and terminate its execution. For example:
function add(a, b) {
return a + b;
}
let result = add(1, 2);
console.log(result); // 3
Exception Handling
In JavaScript, the try...catch
statement is used to handle exceptions:
try {
// Code that might throw an error
let x = y + 1; // y is undefined, will throw an error
} catch (error) {
console.log("An error occurred: " + error.message);
} finally {
console.log("This will execute regardless of an error");
}
Labeled Statements
Labeled statements are used to identify blocks of code, often used with break
or continue
. For example:
outerLoop: for (let i = 0; i < 3; i++) {
innerLoop: for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break outerLoop;
}
console.log(`i=${i}, j=${j}`);
}
}
Ternary Operator
The ternary operator is a shorthand for if...else
:
let age = 18;
let message = age >= 18 ? "Adult" : "Minor";
console.log(message); // Adult
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn