Enhancing Skills

Control Flow in JavaScript

Control Flow

Control flow statements determine the order in which code executes based on conditions and looping constructs. Understanding these statements is vital for implementing game logic, such as responding to player actions and managing game states.

CommandDescription
Conditional Statements:
ifExecutes a block of code if the specified condition is true.
else ifChecks another condition if the previous if condition is false.
elseExecutes a block of code if none of the preceding conditions are true.
switchEvaluates an expression and executes different blocks of code based on its value.
Ternary OperatorShorthand for an if-else statement. Evaluates a condition and returns one of two values.
Loops:
forExecutes a block of code a specific number of times.
whileContinues to execute a block of code as long as a specified condition is true.
do…whileExecutes a block of code at least once and then continues while a specified condition is true.

Ternary Operator (? :)

The ternary operator is a concise way to handle conditional expressions. It evaluates a condition and returns one of two values based on whether the condition is true or false. It works as follows:

let result = condition ? value_if_true : value_if_false;

Example:

let lives = 3;
let status = lives > 0 ? 'Player is alive' : 'Game Over';
console.log(status); // Output: 'Player is alive'

Sample Code

// Conditional Statements
let playerScore = 85;

// if...else if...else
if (playerScore >= 90) {
    console.log('Grade: A');
} else if (playerScore >= 80) {
    console.log('Grade: B');
} else if (playerScore >= 70) {
    console.log('Grade: C');
} else {
    console.log('Grade: F');
}

// switch statement
let gameLevel = 2;

switch (gameLevel) {
    case 1:
        console.log('Level 1: Beginner');
        break;
    case 2:
        console.log('Level 2: Intermediate');
        break;
    case 3:
        console.log('Level 3: Advanced');
        break;
    default:
        console.log('Unknown Level');
}

// Loops
// for loop
for (let i = 0; i < 5; i++) {
    console.log(`Player ${i + 1} is ready!`);
}

// while loop
let attempts = 0;
while (attempts < 3) {
    console.log(`Attempt ${attempts + 1}`);
    attempts++;
}

// do...while loop
let countdown = 3;
do {
    console.log(`Countdown: ${countdown}`);
    countdown--;
} while (countdown > 0);

Output

Grade: B
Level 2: Intermediate
Player 1 is ready!
Player 2 is ready!
Player 3 is ready!
Player 4 is ready!
Player 5 is ready!
Attempt 1
Attempt 2
Attempt 3
Countdown: 3
Countdown: 2
Countdown: 1

Use Case

Game Logic:
Control flow statements are fundamental in game programming. They allow developers to create dynamic responses to player input, control game state transitions, and implement game mechanics. For instance, conditional statements can manage score thresholds to determine player progression or game difficulty, while loops can handle repetitive tasks like updating the game state or rendering graphics until a certain condition is met (e.g., the game ends). By effectively using control flow, developers can create engaging and interactive gameplay experiences.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.