Control Structures Flashcards
Statements and loops
If Statement
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.
else Statement
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.
Else-If Statement
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.
Switch Statement
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.
for Loop
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.
Foreach Loop
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.
While Loop
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.
Do-While Loop
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.
Break Statement
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.
Continue Statement
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.
Return Statement
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.
Usage Tips
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.