How to use C + + conditional and conditional operators to explain the _c language

Source: Internet
Author: User
Tags mathematical functions cmath

3 forms of an IF statement

1 an if (expression) statement.

For example:

 if (x>y) cout<<x<<endl;

The execution of this if statement is shown in the following figure.

2 if (expression) statement 1 Else Statement 2

For example:

 if (x>y) cout<<x;
 else cout<<y;

See figure.

3)

if (expression 1) Statement 1
 else if (expression 2) Statement 2
 else if (expression 3) Statement 3
 ...
 else if (expression m) statement m
 Else statement n

For example:

if (number>500) cost=0.15;
else if (number>300) cost=0.10;
else if (number>100) cost=0.075;
else if (number>50) cost=0.05;
else cost=0;

The flowchart is shown in the diagram.

Description
From figure 3.5 and Figure 3.6, you can see that 3 forms of if statements are entered by a single entry, executed according to the "expression", and finally grouped into a common export. This form of the program structure is called the selection structure. In C + +, the If statement is the main statement to implement the selection structure.
In 3 forms of an If statement, there is an expression enclosed in parentheses, which is the "condition" that the program writer requires the program to judge, typically a logical expression or a relational expression.
In the 2nd or 3rd form of the IF statement, there is a semicolon at the end of each else, and a semicolon at the conclusion of the entire statement.
You can include only one inline action statement (such as the previous example) after the if and else, and you can have multiple action statements that enclose several statements into a compound statement with curly braces "{}".

"Example" to find the area of the triangle.

#include <iostream>
#include <cmath>//using mathematical functions to include header files Cmath
#include <iomanip>//Use i/ o Stream control character to include header file Iomanip
using namespace std;
int main ()
{
 double a,b,c;
 cout<< "Please enter a,b,c:";
 cin>>a>>b>>c;
 if (a+b>c && b+c>a && c+a>b)
 {//Compound statement start
  double s,area;//define variable in compound statement
  s= (a+b+c)/ 2;
  Area=sqrt (s* (s-a) * (s-b) * (s-c));
  Cout<<setiosflags (ios::fixed) <<setprecision (4); Specifies that the number of outputs contains 4-bit decimal
  cout<< "area=" <<area<<endl;//value of local variable output within compound statements
 }//Compound statement end
 Else cout << "It is not a trilateral!" <<endl;
 return 0;
}

The operating conditions are as follows:

Please enter a, B, c:2.45 3.67 4.89↙
area=4.3565

The variables S and area are only used within the compound statement, so defined within the compound statement, which ranges from defining the variable to the end of the compound statement. If you use S and area outside of a compound statement, an error occurs at compile time, and the system considers the two variables undefined. Limiting certain variables to a certain range and isolating them from the outside can avoid being mistakenly invoked elsewhere.
Nesting of IF statements

The IF statement contains one or more if statements that are nested as if statements. The general form is as follows:

 if ()
  if () Statement 1
  Else Statement 2
 Else
  if () Statement 3
  Else Statement 4

You should pay attention to the pairing relationship between if and else. Else is always paired with the most recent and unpaired if on it. If written:

 if ()
  if () statement 1
 Else if () statement
  2
  Else Statement 3

The writer writes the first else on the same column as the first if (outer if), expecting else to correspond to the first if, but actually else is paired with the second if, because they are far apart, and the second if is not paired with any else. In order to avoid misuse, it is a good idea to have the IF statement embedded in each layer contain the ELSE clause (as listed at the beginning of this section) so that if the number of if is the same as the number of else, there is no error from the inner layer to the outer one by one.

If the number of if and else is not the same, in order to implement the program designer's attempt, you can add curly braces to determine the pairing relationship. For example:

 if ()
 {
  if () statement 1
 }//This statement is an inline if else statement of the previous line if
 2//the bank is paired with the first if

At this time {} limits the scope of the inline if statement, and else outside of {} is not paired with if in {}. The relationship is clear and error-prone.

C + + conditional operator (?:)
in an If statement, when the value of the other expression is either true or FALSE, an assignment statement is executed and a value is assigned to the same variable, which can be handled with a simple conditional operator. For example, if you have the following if statement:

 if (a>b) max=a;
 else max=b;

You can work with the conditional operator (?:):

 max= (a>b) a:b;


where "(a>b)? A:b" is a "conditional expression". It does this: if the (a>b) condition is true, then the value of the conditional expression is taken "?" The value that follows, that is, the value of the conditional expression is a, otherwise the value of the conditional expression is the value after ":", that is, B.

The conditional operator requires 3 action objects, called the three-mesh (meta) operator, which is the only three-mesh operator in C + +. The general form of a conditional expression is:

 Expression of 1? Expression 2: Expression 3;

The order in which the conditional operators are executed is to solve the expression 1 first, or 0 (true) to solve the expression 2, at which point the value of the expression 2 is the value of the entire conditional expression. If the value of expression 1 is 0 (false), the expression 3 is solved, and the value of expression 3 is the value of the entire conditional expression. The result of "max= a>b" A:B is to assign the value of the conditional expression to max. Which is to give Max the big of both A and B. The conditional operator takes precedence over the assignment operator, so the solution to the above assignment expression is to first solve the conditional expression and then assign its value to max.

In a conditional expression, the type of expression 1 can be different from the type of expression 2 and expression 3. Such as

 X? ' A ': ' B ';

If x is defined as an integer variable, if x=0, the value of the conditional expression is the ASCII code of the character ' B '. The type of expression 2 and expression 3 can also be different, at which point the type of the value of the conditional expression is the higher type. If you have conditional expression x>y?1:1.5, if x≤y, the value of the conditional expression is 1.5, and if x>y, the value should be 1, because C + + 1.5 is treated as double, the type of double is higher than the integer, so the 1 is converted to a double-precision number as the value of the expression.

"Example" enter a character to determine whether it is uppercase and, if so, convert it to lowercase letters; And then output the resulting character.

#include <iostream>
using namespace std;
int main ()
{
 char ch;
 cin>>ch;
 Ch= (ch>= ' A ' && ch<= ' Z ')? (ch+32): ch; To determine if CH is a capital letter, is to convert
 cout<<ch<<endl;
 return 0;
}

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.