Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use switch statement in C++

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article gives you a detailed introduction of "how to use the switch statement in C++". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the switch statement in C++" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Basic format

The basic format of the switch statement is as follows:

Switch (expression) {

Case constant expression 1: the content in "statement sequence 1"break;" / "" can be saved.

……

Case constant expression n: "statement sequence n"break;" / / same as above, same as below

"default: statement sequence"

}

Where:

Expression-called a "conditional expression" and is used as a judgment condition, with values of integer, character, Boolean, or enumerated.

Constant expression-consists of constants with the same type of value as a conditional expression.

Statement sequence-it can be a statement or a set of statements.

If statement and switch statement

It is believed that the students who have studied if Cure + have long known the similarities and differences between the two sentences. As a conditional judgment, the if sentence enters the if sentence block if it meets the condition, and if it does not meet the condition, it enters the else sentence block, and the if and else sentence blocks can continue to nest if sentences. Switch determines which case statement to enter by judging the value of an integer expression, and enters the default statement block if all case conditions are not met.

/ / simple if statement if (a = = 1) I = 1else if (a = = 2) I = 2else I = 3X switch / simple switch statement switch (a) {else 1: I = 1; case 2: I = 2; default: I = 3;}

How does the compiler implement switch statements?

Now the compiler is smart and powerful enough, after testing, there are at least three ways to implement switch statements. According to the actual situation of the code, the compiler will weigh the time efficiency and space efficiency to choose the one with the highest comprehensive efficiency for the current code.

There are three ways for a compiler to implement switch statements:

Judge by condition

Jump table

Binary search method

Later, we will test and analyze the code scenarios in which these three implementation methods are applicable.

1. Conditional judgment method

The method of conditional judgment is actually related to if. The assembly implementation of the else statement is the same, and the compiler judges each case condition in the switch statement one by one until the correct case statement block is found. This method is suitable for situations where there are few case conditions in the switch statement, and even judging on a condition-by-condition basis will not result in a large waste of time and space, such as the following code:

# include int test_switch () {int i; int a = std::rand (); switch (a) {case 0: I = 0; case 1: I = 1; case 2: I = 2; default: I = 3;} return I;}

The assembly code corresponding to this code is as follows:

Movl-4 (% rbp),% eax cmpl $1,% eax je .L3 cmpl $2,% eax je .L4 testl% eax,% eax jne .L8 movl $0,-8 (% rbp) jmp .L6.L3: movl $1,-8 (% rbp) jmp .L6.L4: movl $2,-8 (% rbp) jmp .L6.L8: movl $3,-8 (% rbp) nop

The eax register stores the judgment condition value (corresponding to the a value in C++ code). First, it determines whether an is equal to 1, if it is equal to 1, then jumps to .L3 to execute the code segment corresponding to astatine 1, and then determines whether an is equal to 2. If it is equal to 2, jump to .L4 to execute the code segment corresponding to astatine 2. What may be difficult to understand is line 6 code testl% eax,% eax. In fact, this is just a small skill for the compiler to improve the efficiency of judging whether a register is 0. If eax is not equal to 0, then jump to .L8 code segment and execute the code corresponding to default snippet. If eax equals 0, then execute the corresponding code segment.

From the analysis of the assembly code generated by the compiler, we can find that the compiler uses condition-by-condition judgment to implement the switch statement in this case.

two。 Jump table realization method

When the compiler uses this way of implementing switch statements, it will generate a jump table in the program, and the jump table stores the address of each instruction block of case statements. When the program runs, it first judges the value of the switch condition, and then uses the condition value as the offset of the jump table to find the instruction address of the corresponding case statement, and then executes it. This method is suitable for cases where there are many case conditions, but the values of case are continuous. Using this method can improve time efficiency without significantly reducing space efficiency. For example, the following code compiler will adopt the jump table implementation method:

# include int test_switch () {int I; int a = std::rand (); switch (a) {case 0: I = 0; case 1: I = 1; case 2: I = 2; case 3: I = 3; case 4: I = 4; case 5: I = 5; case 6: I = 6; case 7: I = 7; case 8: I = 8; case 9: I = 9 Default: I = 10 Breakout;} return I;}

The assembly code corresponding to this code is as follows:

Movl-4 (% rbp),% eax movq .L4 (,% rax,8),% rax jmp *% rax.L4: .quad .L3 .quad .L5 .quad .L6 .quad .L7 .quad .L8 .quad .L9 .quad .L10 .quad .L11 .quad .L12 .quad .L13 .text.L3: movl $0,-8 (% rbp) jmp .L14.L5: movl $1,-8 (% rbp) jmp .L14 # is omitted.

In x64 architecture, the eax register is the low 32 bits of the rax register, where we can think that the two values are equal. The first line of code copies the judgment condition (corresponding to the a value in the C++ code) to the eax register, the second line of code assigns the address of the .L4 offset from the rax register to the rax register, and the third line of code takes out the address stored in rax and jumps to that address. We can see clearly that the .L4 code segment is the jump table generated by the compiler for the switch statement and stored in the .text section, and each case corresponds to an address value in the jump table. We can calculate the offset of the address stored in the corresponding code segment address relative to .L4 by judging the value of the condition, so as to achieve efficient jump.

3. Binary search method

If the case value is large and the distribution is extremely discrete, if the condition-by-condition judgment is adopted, the time efficiency will be very low, if the jump table method is used, the jump table will take up a lot of space, and the first two methods will lead to low program efficiency. In this case, the compiler will use the binary search method to implement the switch statement. When the program is compiled, the compiler first sorts all the case values and then writes the assembly code according to the binary search order. When the program executes, it uses the binary search method to find the conditional values in each case value. If it is found, the corresponding case statement is executed, and if it is not finally found, the default statement is executed. This binary search method is used by the C++ code compiler to implement the switch statement as follows:

# include int test_switch () {int i; int a = std::rand (); switch (a) {case 4: I = 4; case 10: I = 10; case 50: I = 50; case 100: I = 100; case 200: I = 200; case 500: I = 500; default: I = 0;;}

The assembly code corresponding to the code segment is:

Movl-4 (% rbp),% eax cmpl $50,% eax je .L3 cmpl $50,% eax jg .L4 cmpl $4,% eax je .L5 cmpl $10,% eax je .L6 jmp .L2.L4: cmpl $200,% eax je .L7 cmpl $500,% eax je .L8 cmpl $100,% eax je .L9 jmp .L2

The condition value in the second line of code is first compared to 50. Why 50 instead of the first 4? This is because the binary search first looks for the value in the middle, so compare it with 50 first, and if eax equals 50, execute case.

50 corresponds to the code, if the eax value is greater than 50, jump to the .L4 code segment, and if the eax is less than 50, continue to compare with 4. Until the condition value is found or found, the condition value does not exist. It can be seen that the binary search method not only maintains a high query efficiency, but also saves space.

After reading this, the article "how to use the switch statement in C++" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, welcome to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report