Control Structures Flashcards

Statements and loops

You may prefer our related Brainscape-certified flashcards:
1
Q

If Statement

A

Description: Executes a block of code if a specified condition is true.

When to Use: Use when you need to make decisions based on conditions. Ideal for checking single conditions or multiple conditions with else if.

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

else Statement

A

Description: Executes a block of code if the condition in the if statement is false.

When to Use: Use in conjunction with if to provide an alternative block of code when the if condition is not met.

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

Else-If Statement

A

Description: Checks another condition if the previous if or else if condition was false.

When to Use: Use when you have multiple conditions to check in a sequential manner.

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

Switch Statement

A

Description: Selects a block of code to execute from multiple options based on the value of a variable.

When to Use: Use when you have multiple potential values for a single variable and each value requires a different block of code to execute. Ideal for cleaner code instead of multiple if-else statements.

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

for Loop

A

Description: Repeats a block of code a specific number of times.

When to Use: Use when you know in advance how many times you need to execute a block of code. Ideal for iterating over arrays or collections.

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

Foreach Loop

A

Description: Iterates over each element in a collection or array.

When to Use: Use when you need to perform an action on each item in a collection or array without modifying the collection.

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

While Loop

A

Description: Repeats a block of code as long as a specified condition is true.

When to Use: Use when you need to execute a block of code an unknown number of times, but you know the condition that must be true for the loop to continue.

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

Do-While Loop

A

Description: Similar to a while loop, but it executes the block of code once before checking the condition.

When to Use: Use when you need to ensure that the block of code runs at least once before checking the condition.

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

Break Statement

A

Description: Exits the nearest enclosing loop or switch statement.

When to Use: Use when you need to exit a loop or switch statement immediately based on a certain condition.

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

Continue Statement

A

Description: Skips the rest of the current loop iteration and proceeds to the next iteration.

When to Use: Use when you want to skip the current iteration of a loop based on a condition but continue with the subsequent iterations.

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

Return Statement

A

Description: Exits a method and optionally returns a value.

When to Use: Use when you need to exit a method and possibly provide a value back to the calling method.

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

Usage Tips

A

If-Else vs. Switch: Use if-else for complex conditions and switch for multiple specific values.

For vs. Foreach: Use for when you need index-based access, and foreach for read-only iterations over collections.

While vs. Do-While: Use while when the condition should be checked before entering the loop, and do-while when the code should run at least once before checking the condition.

Break and Continue: Use break to exit loops early, and continue to skip to the next iteration.

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