CS 511-Web Engineering
Week 6
Topic 65-67
CS 511-Week 6:Topic 65-67 |
Topic 65:
Conditionals:
In JavaScript, conditions are similar to those in C++.
Types of conditional statements:
1. If-else if:
Conditional statements in JavaScript involve writing logical expressions. If the logical expression evaluates to true, a specific block of code is executed; otherwise, it is skipped.
For example:
var hourOfDay; // variable to hold the hour of the day
var greeting; // variable to hold the greeting message
In this example, the value of the hourOfDay variable determines the greeting message based on different conditions.
2. Switch Statements:
The switch statement evaluates an expression and matches its value to different case clauses. It then executes the statements associated with the matching case, as well as any statements in cases that follow it.
For example:
const expr = 'Papayas';
In this example, the value of the expr variable is compared to different cases, and the corresponding code block is executed. If no case matches, the code within the default block is executed.
3. Conditional assignment:
JavaScript's conditional (ternary) operator allows you to assign a value to a variable based on a condition. It takes three operands: a condition, a value to assign if the condition is true, and a value to assign if the condition is false. This operator is often used as a concise alternative to an if...else statement.
For example:
var x = (y == 4) ? "y is 4" : "y is not 4";
In this example, the value of x is determined based on the condition (y == 4). If the condition is true, the value "y is 4" is assigned to x; otherwise, the value "y is not 4" is assigned.
Topic 66:
To view the output in JavaScript, there are three commonly used mechanisms
- Alert: The alert() function displays a pop-up message to the user. It takes a string as an argument, which will be shown as the message in the pop-up.
- Document Write: The document.write() method allows you to write text directly into the HTML document. Whatever string you pass to document.write() will become part of the HTML page.
- Console Log: The console.log() function is primarily used for debugging JavaScript applications. It allows you to print messages, variables, or other data to the console, which can be accessed through the browser's developer tools. To open the console in most web browsers, you can use the shortcut "Ctrl + Shift + I" on Windows.
Topic 67:
The while loop in JavaScript allows you to repeat a block of code as long as a given condition remains true. It checks the condition before executing the code block, ensuring the loop is only entered if the condition evaluates to true.
1.1. Syntax of the While Loop
java script Copy code
while (condition) {
// Code to be executed
}
2. Do-While Loop
Similar to the while loop, the do-while loop repeatedly executes a block of code based on a condition. However, unlike the while loop, the do-while loop checks the condition after executing the code block, ensuring that the block is executed at least once.
2.1. Syntax of the Do-While Loop
javascriptCopy code
do {
// Code to be executed
} while (condition);
3. For Loop
The for loop is commonly used when the number of iterations is known or can be easily determined. It provides a compact syntax for defining the initialization, condition, and iteration steps within a single line.
3.1. Syntax of the For Loop
javascriptCopy code
for (initialization; condition; iteration) {
// Code to be executed
}
4. For Each Loop
The for Each loop is specifically designed for iterating over elements of an array.
4.1. Syntax of the For Each Loop
javascriptCopy code
array.forEach(function(element) {
// .....
});
Summary
To recap, JavaScript offers four main types of loops: the while loop, the do-while loop, the for loop, and the for-each loop. The while and do-while loops are useful when you want to repeat a block of code based on a condition. The for loop is suitable for a known number of iterations, and the forEach loop simplifies the process of iterating over array elements.
Conclusion
Loops are fundamental to programming, and JavaScript provides various types to cater to different looping requirements. By mastering the different loop constructs, you can efficiently perform repetitive tasks and manipulate data in your JavaScript programs.
Now that you understand the basics of loops in JavaScript, you can leverage them to optimize your code and build more robust applications.
Assessment:
Which conditional statement in JavaScript involves writing logical expressions?
How do you open the console in most web browsers?
Which operator is used for conditional assignment in JavaScript?
The document.write() method is used to:
The console.log() function is primarily used for:
Which loop in JavaScript checks the condition before each iteration?
The do-while loop in JavaScript guarantees that the code block is executed at least:
Which loop is commonly used when the number of iterations is known in advance?
For Each loop is used specifically with:
0 Comments