JavaScript - Control Structures Flashcards

1
Q

What are Control Structures?

A

Control structures are programming tools that manage the flow of code execution. They allow us to make decisions and repeat actions in our programs. Essential for creating dynamic and responsive JavaScript applications. We’ll explore conditional statements, loops, and other control structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why are, If Else and Else statements used?

A

Used for Making Decisions in Code/Javascript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Syntax (Code Outline) for If, If Else and Else Statements :

A

if (condition) {
// code to run if condition is true
} else if (anotherCondition) {
// code to run if anotherCondition is true
} else {
// code to run if no conditions are true
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why are Switch Statements used?

A

Used when you have Multiple Conditions to check against a single value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Syntax (Code Outline) for Switch Statements :

A

switch (expression) {
case value1:
// code to run
break;
case value2:
// code to run
break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why are While Loops used?

A

Repeats a block of code while a condition is true. Remember to be careful to avoid infinite loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Syntax (Code Outline) for While Loops :

A

while (condition) {
// code to repeat
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Example of Code counting down from 10 to 1 using While Loops.

A

let count = 10;

while (count >= 1) {
document.write(count);
count–;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why are Do While Loops used?

A

Similar to while loop, but guarantees the code block runs at least once. Useful when you need to execute code before checking the condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Syntax (Code Outline) for Do While Loops :

A

do {
// code to repeat
} while (condition);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Example of Code for Asking for user input until valid using Do While Loops.

A

let userInput;
do {
userInput = prompt(“Please enter a number between 1 and 10:”);
userInput = Number(userInput);
} while (isNaN(userInput) || userInput < 1 || userInput > 10);

document.write(“Valid input: “ + userInput);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Comparison between Do While and While Loops ?

A

While Loop : hecks condition first and may not execute if condition is initially false.
Do While Loop : Executes code block first, then checks condition
and always executes at least once. Choose based on whether you need guaranteed execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly