How to solve a large number of if statements or switch case statements?

Hongliu Academy will make you a few steps faster.
The content of this article comes from the interpretation of the book "Code Encyclopedia 2" by Hongliu Reading Club.

Many interviewers like to ask such questions, how to solve a large number of if statements or switch case statements? If you only answer at an optimized level, you may not be able to satisfy the interviewer.

So how to answer better?

table-driven approach

In this case a table-driven approach can be used .

The table-driven method is a programming mode (scheme) that looks up information from the table without using logic statements (if and case) . In fact, all things that can be selected by logical statements can be selected by looking up tables. For simple cases, it is easier and more straightforward to use logical statements. But as the logic chain becomes more and more complex, the look-up table method becomes more and more attractive.

Under the right circumstances, a table-driven approach can generate code that is simpler, easier to modify, and more efficient than complex logic code. Suppose you want to divide characters into letters, punctuation marks, and numbers, then you may use the following complex logic chain:

On the other hand, if you use an ookup table, you can store the type of each character in an array accessed by character code. Then the complex code snippet above can be replaced with:

This code assumes that the charTypeTable array has been created in advance. At this time, you store the information in the program in the data instead of the logic—that is, in the table instead of the if test.

Two problems that must be solved when using the table-driven method

1. You must answer the question of how to query entries from the table

You can directly access the table with some data. For example, if you want to group your data by month, then creating a table of months is pretty straightforward. You can do it with an array with indices from 1 to 12.

Other data may be difficult to use directly for table lookup. For example, if you want to classify data by social security number, then unless you can afford to store 99-99999 records in the table, you cannot directly look up the table by social security number. You'll be forced to take a more complicated approach. Here is a list of methods for querying records from a table :

  • Direct access . For example, insurance rate tables, data tables commonly used in games, and so on. You can directly find the corresponding data through a value, such as the age in the insurance rate table.
  • Indexed access . Sometimes, data such as age cannot be converted into table key values ​​with only a simple mathematical operation. Some of these situations can be resolved by using indexed access. When you use an index, first use a basic type of data to find a key value from an index table, and then use this key value to find the main data you are interested in. For example, in the game, it is necessary to determine the rewards obtained according to the player's occupation, level, etc., and the method of index conversion can be used.
  • Stair-step access . There is another way to access tables, and that is step access. This access method is not as direct as the indexed structure, but it is less space than the indexed access method. The basic idea of ​​the ladder structure is that the records in the table are valid for different data ranges, not for different data points. The most common is the scoring system, which may need to be based on different scoring ranges to obtain the final score.

2. What should you store in the table?

Sometimes, the result of table query is data. If this is the case you encounter, then you can save the data to the table. In other cases, the result of a table query is an action. In this case, you can save a code that describes the action, or, in some languages, you can save a reference to the subroutine that implements the action. In either case, the table becomes more complex.

further reading

[Extended Learning]读书会 You can read all articles in this series by replying to the official account of Hongliu Academy


I'm Dazhi (vx: zhz11235), your technology pathfinder, see you next time!

don't go! Like it , collect it!

OK, you can go.

Guess you like

Origin blog.csdn.net/zhenghongzhi6/article/details/111986863