Control Structures
Control Structures
Selection Statements
The selection statements in JavaScript are primarily if-else statements where the selection form is a logical condition. The type is boolean, either true or false (mozilla.org, 2024). The boolean expressions are evaluated by looking inside the if statement.
As an example, lowerCaseQuestion.includes("txt") will be evaluated as true or false, depending on the user's input. The else clause will be executed when the if condition is true. In this case, the then clause is the console.log ("TXT Summaries:", summaries.txtSummaries). If the condition is false, it will execute the else clause.
The if-else statements can also have multiple conditions sequentially. The condition will be executed in order based on the boolean evaluations. The first expression evaluated as true will be executed and break the selection statement. If not, the else clause will be executed.
In addition, JavaScript is not indentation-sensitive. Thus, the indentation is used for readability purposes and easy code debugging, especially for larger and more complex code.
Both codes show the same output, despite having a change in the indentation.
JavaScipt also used multiple-way selection, which is a Switch case. The case can only be expressed by an integer value, which works similarly like C++.
- Nur Insyirah Iman
Iterative Statements
Iteration is the process of running code once for every item in a collection, typically an array's elements or an object's attributes. Looping on the other hand is used to run through a particular block of codes a number of times, or until the condition set is met. Iterations and loops are often used interchangeably.
There are two protocols for controlling iteration, the iterable protocols and the iterator protocols.
Iterable protocols
The protocols allow objects to customize the iteration behavior, such as in the logically controlled loops. Custom iterable objects must implement the '[Symbol.iterator]()' method. This method will return an object with a 'next()' method implementing the protocols.
Iterator protocols
The protocols specify a standard way to generate a series of values, and return the values when all of them has been generated. Iterator objects must implement the 'next()' method that can return objects with 'value', the next value in the sequence, and 'done', a boolean indicating if the iteration is complete. the iteration is complete once 'done' returns true.
The control mechanisms usually appear in the loop itself. We will dive in deeper for each loop statement.
Counter-controlled loops
A type of loop that uses a counter to monitor the iteration of the program (BBC Bitesize, 2023) .
For loop
A for loop serves as a control mechanism by executing the block code based on the user's conditions. It will continue looping as long as the conditions are true. It will terminate and exit the loop if the conditions are met or false (Loops and Iteration - JavaScript | MDN, 2024).
Control mechanisms:
Initialization (expression 1)
the user initializes a variable to be used in the loop.
only executes once in a for loop
Terminal (expression 2)
the user will specify the conditions
continues execution as long as condition is true
Stepsize (expression 3)
modify the counter variable after each iteration
Logically-controlled loops
A type of loop that continues execution based on the condition specified or boolean expression.
while loop
The control mechanism of a while loop is specified in '(condition)'. The loop will continue execution while the conditions are true.
2. do...while loop
Similar to a while loop, do...while loop control mechanism is specified in '(condition)'. The loop will execute once before checking the condition. It will continue execution until the condition is evaluate to false.
3. for...in loop
The control mechanism in a for...in loop is the iteration over the enumerable properties of an object or an array.
'key' or 'variable' is used to store current property during each iteration.
'object' or 'array' represents the target object or array whose enumerable property keys the loop will iterate over.
User-located loop controls
A user decides the location of the loop control mechanism rather than using iteration. This type of loop is useful for single loops and nested loops (Concepts of Programming Language, n.d.). JavaScript supports unconditional unlabeled exits using break statements and switch statements. It also allows labeled breaks for exiting a nested loop using break statement.
Break statement (unlabeled)
Break statement (labeled)
'break' statement is used to break out of a switch case or a loop. In a switch case, break statement is used stop executing the codes inside the case, while in a loop, it is utilized to break out of the loop and continues execution of the codes after the loop (W3Schools.com, n.d.) .
Unlabeled break can only exit the innermost loop or switch statement while a labeled break exit a specific outer loop or control structure, regardless of the number of nested loop it has. We can specify the loop to exit at the 'labelname'.
- Anis Syifaa'
REFERENCES
Selection Statements
mozilla.org. (2024, November 22). Control flow and error handling - JavaScript | MDN. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling
W3Schools.com. (n.d.). https://www.w3schools.com/js/js_switch.asp
Iterative Statements
BBC Bitesize (2023, February 15).Count-controlled loops - Iteration in programming. https://www.bbc.co.uk/bitesize/guides/z3khpv4/revision/2#:~:text=A%20count%2Dcontrolled%20loop%20is%20used%20when%20the%20number%20of,times%20the%20algorithm%20has%20iterated.
Concepts of programming language (tenth). (n.d.). Pearson.
W3Schools.com. (n.d.). https://www.w3schools.com/jsref/jsref_break.asp
GeeksforGeeks. (2023, December 20). Control statements in JavaScript. GeeksforGeeks. https://www.geeksforgeeks.org/control-statements-in-javascript/
W3Schools.com. (n.d.-b). https://www.w3schools.com/js/js_loop_while.asp
mozilla.org. (2024, October 28). Loops and iteration - JavaScript. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration