Parsing the choice structure and the usage of switch statements in C + + programming _c language

Source: Internet
Author: User
Tags constant

C + + Write the selection structure of the program
Here are two examples to illustrate how to write more complex C + + programs.

"Example" to write a program to determine whether a year is a leap years.

#include <iostream>
using namespace std;
int main ()
{
  int year;
  BOOL Leap;
  cout<< "Please enter the year:";//output hint
  cin>>year; Enter year
  if (year%4==0)//year can be divisible by 4
  {
   if (year%100==0)//year can be divisible by 4 and divisible by 100
   {
     if (year%400==0)// The year can be divisible by 4 and divisible by 400
      leap=true;//leap years, so that Leap=true (true)
     else
      leap=false;
   }//Non-leap, make leap=false (fake)
   The Else//year can be divisible by 4 but not divisible by 100 is definitely a leap year
     leap=true
  }//Is a leap year, so that leap=true
  else//years cannot be divisible by 4 is definitely not a leap
   leap=false; /If not a leap year, make Leap=false
  if (leap)
   cout<<year<< "is"; If Leap is true, the output year and "Yes"
  else
   cout<<year<< "is not";/if Leap is true, output year and "not"
  cout<< "a" Leap year. " <<endl; Output "Leap year" return
  0;
}

The operating conditions are as follows:

①2005↙ the
"not" a leap year
. ②1900↙
1900 is NPT a leap year.

You can also rewrite the 8th to 16th line in the program to the following if statement:

if (year%4!=0)
  leap=false;
else if (year%100!=0)
  leap=true;
else if (year%400!=0)
  leap=false;
else
  leap=true;

You can also include all leap year conditions in a logical expression, replacing the IF statement with the following if statement:
if ((year%4 = = 0 && year%100!=0) | | (year%400 = 0)) Leap=true;
else Leap=false;

"Example" The shipping company calculates the freight for the user. The farther away (s), the lower the shipping cost per kilometer. The standard is as follows:

  s<250km No discount
  250≤s<500  2% discount
  500≤s<1000  5% discount
  1000≤s<2000  8% discount
  2000≤s<3000  10% discount
  3000≤s  15% Discount

The basic freight for each tonne of cargo per kilometer is p (price abbreviation), the goods are discount W (Wright's abbreviation), the distance is s, the discount is D (abbreviated), the total freight f (freight abbreviation) is calculated as

  f = p * W * s * (1-d)

The following procedure is written:

#include <iostream>
using namespace std;
int main ()
{
  int c,s;
  float p,w,d,f;
  cout<< "Please enter p,w,s:";
  cin>>p>>w>>s;
  if (s>=3000)
   c=12;
  else
   c=s/250;
  Switch (c)
  {case
   0:d=0;break;
   Case 1:d=2;break;
   Case 2: Case
   3:d=5;break;
   Case 4: Case
   5: Case
   6: Case
   7:d=8;break;
   Case 8: Case
   9: Case
   11:d=10;break;
   Case 12:d=15;break;
  }
  f=p*w*s* (1-d/100.0);
  cout<< "freight=" <<f<<endl;
  return 0;
}

The operating conditions are as follows:

Please enter p,w,s:100 20 300↙
freight=588000

C + + switch statement (multiple-selection branch structure)
A switch statement is a multiple-branch selection statement that implements a multiple-branching selection structure. Its general form is as follows:

switch (expression)
{case
  constant expression 1: statement 1 case
  constant expression 2: Statement 2
  ...
  case constant Expression N: statement n
  Default: statement n+1
 }

For example, a hundred fraction segment is required to be printed according to the grade of the test score and can be implemented using a switch statement:

Switch (grade)
{case
  ' A ': cout<< "85~100\n";
  Case ' B ': cout<< "70~84\n";
  Case ' C ': cout<< "60~69\n";
  Case ' D ': cout<< "<60\n";
  default:cout<< "error\n";
}

Description
1 The switch is followed by an "expression" in parentheses, allowing for any type.

2 when the value of a switch expression matches the value of a constant expression in a case clause, in the case of an inline statement in this case clause, if the value of a constant expression in all case clauses cannot match the value of the switch expression, the inline statement of the default clause is executed.

3 The value of each case expression must be different, otherwise there will be conflicting phenomena (the same value for the expression, there are two or more execution scenarios).

4 The order of occurrence of each case and default does not affect the execution result. For example, you can first appear "Default: ...", and then "Case ' D ': ..." and then "Case ' A ': ...".

5 after executing a case clause, Process control is transferred to the next case clause to continue execution. "Case constant expression" is only the function of sentence marking, and it does not make conditional judgment at that place. When the switch statement is executed, a matching case clause is found based on the value of the switch expression, and the execution of the case clause begins without judgment. For example, in the example above, if the value of Grade equals ' A ', the output will be continuous:

  85~100
  70~84
  60~69
  <60
  Error

Therefore, after executing a case clause, the process jumps out of the switch structure, terminating the execution of the switch statement. You can use a break statement to achieve this goal. Rewrite the above switch structure as follows:

Switch (grade)

{case
  ' A ': cout<< "85~100\n";
  Case ' B ': cout<< "70~84\n";
  Case ' C ': cout<< "60~69\n";
  Case ' D ': cout<< "<60\n";
  default:cout<< "error\n";



The last clause (default) can be without a break statement. If the value of grade is ' B ', only "70~84" is output.

Although there is more than one execution statement in the case clause, it is possible to enclose all the execution statements in this case clause automatically in sequence, without having to surround them with curly braces.

6 multiple case can be shared with a group of execution statements, such as

  Case ' A ': Case '
  B ': Case
  ' C ': cout<< ">60\n";
  ...

When the value of grade is ' A '? B ' or ' C ' executes the same set of statements.

Related Article

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.