Switch confusions of VB transfer VC

Source: Internet
Author: User
Tags case statement
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:
  1. Switch (I)
  2. {
  3. Case 1:
  4. Cout <"1 ";
  5. Case 2:
  6. Cout <"2 ";
  7. Case 3:
  8. Cout <"3 ";
  9. }

This is actually the case:

  1. Int _ LBL = I;
  2. Goto _ LBL;
  3. 1:
  4. Cout <"1 ";
  5. 2: //
  6. Cout <"2 ";
  7. 3: // B
  8. 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.

  1. Double value = 0;
  2. Double currency = 232.5;
  3. Double B _money = money;
  4. Switch (INT) money/100)
  5. {
  6. Case 9:
  7. Case 8:
  8. Value = money * 0.05;
  9. Money = money-value;
  10. Case 7:
  11. Case 6:
  12. Value = money * 0.03;
  13. Money = money-value;
  14. Case 5:
  15. Case 4:
  16. Value = money * 0.02;
  17. Money = money-value;
  18. Case 3:
  19. Case 2:
  20. Money-= 10;
  21. Break;
  22. Default:
  23. Money-= 5;
  24. }
  25. Value = B _money-money;
  26. Cout <value;

This utilizes the "cross-case" feature of the switch to make the program concise and clear!

Complete code:

  1. # Include <iostream>
  2. Using spacename STD;
  3. Void main ()
  4. {
  5. Double value = 0;
  6. Double currency = 832.5;
  7. Double B _money = money;
  8. Switch (INT) money/100)
  9. {
  10. Case 9:
  11. Case 8:
  12. Value = money * 0.05;
  13. Money = money-value;
  14. Case 7:
  15. Case 6:
  16. Value = money * 0.03;
  17. Money = money-value;
  18. Case 5:
  19. Case 4:
  20. Value = money * 0.02;
  21. Money = money-value;
  22. Case 3:
  23. Case 2:
  24. Money-= 10;
  25. Break;
  26. Default:
  27. Money-= 5;
  28. }
  29. Value = B _money-money;
  30. Cout <value;
  31. }

The code is implemented in WINXP and vc2005.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.