Control Structures: Iteration, Conditions, Nested Conditions
Control structures determine the flow of execution in a program. They help in decision-making and looping through code.
Types of Control Structures:
1. Conditional Statements (if, else, else if): These allow a program to execute different code blocks based on conditions.
let age = 9;
if (age >= 5) {
console.log("They have started elementary school")
}
In the platfomer game, we use a lot of conditional statements to check if we should allow the player to end the level, or to check if the player has collected something.
2. Nested Conditions: When if statements are placed inside other if statements to handle multiple levels of decision-making.
let hasPhone = true
let isTeen = true
if(hasPhone == true) {
if(isTeen == true) {
console.log("They go to high school")
}
}
In the platformer game, we use a lot of nested conditions to check for many things in the code, like if the player is colliding with the finish line and they have accomplished their corresponding task (depending on the level).
3. Loops (for, while, do-while): These repeat a block of code multiple times until a specified condition is met.
for(let i=0; i<5; i++)
{
console.log("Hello!")
}
In the platformer game, loops are used for the game status, and makes sure that the game is running smoothly