I just transferred from VB to VC and found that switch statements are very similar to select case. They are actually quite different!
First, the expression in the case section of the select case statement is flexible, while the case section of the switch statement can only accept constants. Therefore, when the values are within a certain range, they must be enumerated one by one. Not as flexible as VB. In fact, this structure aims to improve efficiency by constructing special binary encoding and using register technology. In VB, the Select case statement is eventually converted to If Else, therefore, there is a big difference in execution efficiency between the two.
Second, the two processes are different. After each case part in the select case is executed, the loop will jump out. If the switch does not encounter a break, it will continue to be executed.
In fact, in C ++, the case part is processed as the label.
See the following switch statement:
- Switch (I)
- {
- Case 1:
- Cout <"1 ";
- Case 2:
- Cout <"2 ";
- Case 3:
- Cout <"3 ";
- }
This is actually the case:
- Int _ LBL = I;
- Goto _ LBL;
- 1:
- Cout <"1 ";
- 2: //
- Cout <"2 ";
- 3: // B
- Cout <"3 ";
In this way, if I is equal to 2, it will jump to row A, output 2, ignore the label of Row B, and continue to run down. This function is flexible, but many people do not understand why it is not embedded with a break behind the case label. In fact, if we do so, it will limit the flexibility of C ++!
For example:
Calculate the discount for products below 1000 yuan, 5 yuan for profits below two hundred yuan, 10 yuan for profits below four hundred yuan, six hundred yuan for profits below 2% yuan, and 10 yuan for profits below eight hundred yuan, 3% for profits below yuan, then, the balance will make a profit of 2%, and then let the profit of 10 yuan, the profit of one thousand yuan or less, and then let the profit of 5%, and then let the profit of 3% again, and then let the profit of 10 yuan.
- Double value = 0;
- Double currency = 232.5;
- Double B _money = money;
- Switch (INT) money/100)
- {
- Case 9:
- Case 8:
- Value = money * 0.05;
- Money = money-value;
- Case 7:
- Case 6:
- Value = money * 0.03;
- Money = money-value;
- Case 5:
- Case 4:
- Value = money * 0.02;
- Money = money-value;
- Case 3:
- Case 2:
- Money-= 10;
- Break;
- Default:
- Money-= 5;
- }
- Value = B _money-money;
- Cout <value;
This utilizes the "cross-case" feature of the switch to make the program concise and clear!
Complete code:
- # Include <iostream>
- Using spacename STD;
- Void main ()
- {
- Double value = 0;
- Double currency = 832.5;
- Double B _money = money;
- Switch (INT) money/100)
- {
- Case 9:
- Case 8:
- Value = money * 0.05;
- Money = money-value;
- Case 7:
- Case 6:
- Value = money * 0.03;
- Money = money-value;
- Case 5:
- Case 4:
- Value = money * 0.02;
- Money = money-value;
- Case 3:
- Case 2:
- Money-= 10;
- Break;
- Default:
- Money-= 5;
- }
- Value = B _money-money;
- Cout <value;
- }
The code is implemented in WINXP and vc2005.