Your program won't work if it doesn't flow correctly. This is such a fundamental aspect of imperative programming that we often don't think about it.
This reference covers the commonly used control flow structures. These are used daily in coding. You should know all of them for an interview. If your language lacks an explicit structure, ensure you know an equivalent form.
Note that terminology varies depending on the language, in particular related to the definition of functions and arguments. Try to learn the words from your preferred language and the common terms.
Functions are part of the standard flow of a program. Some languages offer distinct lambda syntax, in which case you should also know the below using lambdas.
- Function without return: Create and call a function that has arguments but does not have a return value.
- Function with return: Create and call a function that has arguments and a return value.
- Multiple return values: Create and use a function that returns multiple values.
- Return: Use the return statement to return from a function without a value, with a value, and with multiple values.
- Implicit Returns: If offered by the language, use an implicit, or expression based return.
Conditionals can apply to both statements and expressions. Some languages offer only one form, while others offer both. Learn how to use both the statement and expression form where available.
- if: Write a simple conditional.
- if-else: Write a conditional with an else block.
- if-else chain: Include at least two conditional blocks in a chain, showing how multiple if-else connect together.
- switch: Also known as a case, select, or match statement. Show how to group many conditions together.
Loops are a way to repeat blocks of code. There are several common forms across languages. Additionally, each language may offer its own special forms.
- Count loop: Execute a loop a specific number of times.
- While loop: Execute a loop so-long-as a condition holds true. Your language may have a form for each of pre-check or post-check of the condition.
- Collection loop: Execute a block of code for every item in a collection. Specific forms of this operation are also included in the lists and map reference.
- Break: Prematurely end a loop.
- Next/Continue: Skip to the start of the next iteration of a loop.
- Infinite loop: On occassion you'll need an infinite loop that only exits via a break statement.
Whether the language uses exceptions, or wrapped return values, you should know how to raise, propagate, and handle errors. You should at least know the most common convention, in case multiple ones exist.
- Raise: Create an error and start its propagation.
- Propagate: How to propagate an error through a function without handling it. In most exception based languages this is implicit.
- Catch: How to catch an error and stop its propagation.
- Resume: How to resume propagation of a caught error.
Not all languages will contain all of the below mechanisms. You should nonetheless know the purpose of each and how to achieve it.
- Defer: Many languages offer a way to defer flow until the end of a scoped block. If the language does not allow this, you should nonetheless know how to achieve it.
- Finally: In error flow, you will often want to execute code at the end of block without blocking the error.
- With: A resource containing flow that releases a resource at the end of a block.