C + + program execution sequence structure and relational and logical operators to explain _c language

Source: Internet
Author: User
Tags arithmetic numeric logical operators cmath

C + + Sequential structure program
"Example" to find the root of the equation ax2+bx+c=0 two times. A,b,c values are entered at run time by the keyboard, and their values satisfy b2-4ac≥0. According to the algorithm of finding x1,x2. It can write the following C + + programs:

#include <iostream>
#include <cmath>//Because the program uses mathematical function sqrt, it should include header file Cmath
using namespace std;
int main ()
{
 float a,b,c,x1,x2;
 cin>>a>>b>>c;
 X1= (-b+sqrt (B*b-4*a*c))/(2*a);
 X2= (-b-sqrt (B*b-4*a*c))/(2*a);
 cout<< "x1=" <<x1<<endl;
 cout<< "x2=" <<x2<<endl;
 return 0;
}

The operating conditions are as follows:

4.5 8.8 2.4↙
x1=-0.327612
x2=-1.17794

If you want to use a mathematical function in your program, include the header file Cmath (you can also use the old form of the header file math.h, but promote the use of C + + new form of header file). When writing a program, be sure to be careful to translate the mathematical expression into a valid C + + expression.

As you can see, the execution statements in sequential structure programs are executed sequentially. This procedure is the simplest and easiest to understand.

C + + relational and logical operations (relational operators and logical operators)
It is often a requirement to decide what to do based on whether a given condition satisfies. For example, shopping at 1000 yuan below the dozen 95 percent, 1000 yuan and above the dozen 90 percent.

C + + provides an If statement to implement this conditional selection. Such as:

  If amount<1000 tax=0.95; Amount represents the total amount of shopping, tax represents discount
  else tax=0.9;//If amount<1000, conditions are met, tax=0.95, otherwise tax=0.9 pay=amount*tax
  for real payment

Relational operations and relationship expressions

The "amount<1000" in the If statement above implements not arithmetic operations, but relational operations. In fact, comparison operations, two data are compared to determine the results of comparison. "Amount<1000" is a comparison, called a relational expression in a high-level language, where ">" is a comparison character called a relational operator.

The relational operators for C + + are:

    • < (less than)
    • <= (less than or equal) precedence (high)
    • > (greater than)
    • >= (greater than or equal to)
    • = = (equal to)
    • != (not equal to) the same priority (low)

On priorities:
The first 4 relational operators (<,<=,>,>=) have the same precedence, and the last two are the same. The first 4 kinds are higher than the latter two kinds. For example, ">" takes precedence over "= =". The ">" and "<" priorities are the same.
The precedence of the relational operator is lower than the arithmetic operator.
Relational operators have precedence over assignment operators.

For example:

    • C>a+b equivalent to c> (A+B)
    • A>b==c is equivalent to (A>B) ==c
    • A==b<c equivalent to a== (B<C)
    • A=b>c equivalent to a= (B>C)

A formula that joins two expressions with a relational operator is called a relational expression. The general form of a relational expression can be expressed as:

  expression-relational operator expressions

Which expression can be an arithmetic expression or a relational expression? A logical expression? An assignment expression? a character expression. For example, the following are legitimate relational expressions:

  A>b, A+b>b+c, (a==3) > (b==5), ' A ' < ' B ', (A>B) > (b<c)

The value of a relational expression is a logical value, either "true" or "false." For example, the value of the relationship expression "5==3" is "false" and the value of "5>=0" is true. In C and C + +, the value 1 is used for "true" and 0 for "false". If you have the following assignment expression:

    • D=a>b D Gets a value of 1
    • F=a>b>c F Gets a value of 0

Logical Constants and Logical variables

The C language does not provide logical data, and the value of the relationship expression (true or false) is represented by values 1 and 0, respectively. C + + has increased the logical type of data. There are only two logical constants, that is, False (false) and True (true).

A logical variable is defined with the type identifier bool, and its value can only be one of true and false. Such as

  BOOL found, flag=false; Define logical variables found and flag, and make flag initial value false
  found=true;//Assign a logical constant TRUE to a logical variable found

Because the logical variable is defined by the keyword bool, it is also called a Boolean variable. A logical constant is also called a Boolean constant. The so-called logical type, is the Boolean type.

The purpose of setting up a logical type is to see the program intuitively and easily.

When compiling the system to process the logical data, the false is treated as 0, and the true is treated as 1. Therefore, logical data can be mathematically calculated with numeric data.

If a Non-zero integer is assigned to a logical variable, it is treated as true, as

  flag=123; Flag value After assignment is true
  cout<<flag;

The output is a value of 1.
Logical operations and Logical expressions

Sometimes only one relationship expression does not correctly represent the specified condition.

C + + provides 3 kinds of logical operators:
(1) && logic (equivalent to and in other languages)
(2) | | Logical OR (equivalent to or in other languages)
(3)! Logical non (equivalent to not in other languages)

Examples of logical operations are as follows:

    • A && b if A,b is true, then a && B is true.
    • a| | b if one of the a,b is true, then a| | B is true.
    • !a If A is true, then!a is false.

If multiple logical operators are included in a logical expression, the following precedence is given:
(1)! (non) →&& (with) →? (or), that is, "!" Is the tallest of the three.
(2) "&&" and "| |" in logical operators Below the relational operator, "!" is higher than the arithmetic operator.

For example:

    • (a>b) && (x>y) can be written in A>b && x>y
    • (a==b) | | (x==y) can be written as A==b | | X==y
    • (!a) | | (a>b) can be written as!a | | A>b

Connecting two relational expressions with logical operators becomes a logical expression, with the above expressions being logical. The general form of a logical expression can be expressed as

  Expression logical operator expression

The value of a logical expression is a logical amount of "true" or "false." As explained earlier, when the result of a logical operation is given, a value of 1 represents "true", and 0 represents "false", but when judging whether a logical quantity is "true", the criterion is that if the value is 0 it is considered "false" and if its value is Non-zero 0 it is considered "true". For example:
(1) If a=4, the!a value is 0. Because the value of a is not 0, is considered "true", it is "not" operation, get "false", "false" to 0 representatives.
(2) If a=4,b=5, the value of a && B is 1. Because both A and B are not 0, they are considered "true".
(3) A,b value Previous, a-b| | The value of A+b is 1. Because the values for A-b and a+b are not 0 values.
(4) A,b value of the former,!a | | The value of B is 1.
(5) 4 && 0 | | A value of 2 is 1.

In C + +, Integer data can appear in logical expressions, and in logical operations, depending on the value of the integer data is 0 or not 0, take it as logical amount false or True, and then participate in the logical operation.

Through these examples, we can see that the logical operation result is not 0 or 1, it cannot be other values. In a logical expression, an operation object that participates in logical operations can be either 0 ("false") or any value other than 0 (treated as "true"). If you have numeric values in different places in an expression, you should distinguish between objects that act as numeric operations or relational operations, and which are objects that are logical operations.

In fact, the expressions on either side of the logical operator can be not only relational expressions or integers (0 and 0), but also any type of data, such as character type, floating-point type, or pointer type. The system finally determines that they belong to "true" or "false" by 0 and 0来. For example, the value of ' C ' && ' d ' is 1.

After mastering the relational and logical operators of C + +, you can skillfully represent a complex condition with a logical expression. For example, to determine whether a year is a leap years. A leap year is a condition that conforms to either of the following: ① can be divisible by 4, but not divisible by 100. ② can be divisible by 100 and divisible by 400. Like 2004? 2000 is leap year, 2005? 2100 is not a leap year.

You can use a logical expression to represent:

(Year% 4 = 0 && Year% 100!= 0) | | Year% 400 = 0

When the given year is an integer value, if the expression value is true (1), Year is a leap year, otherwise the year is not a leap year. can add a "!" Used to identify non leap years:

! ((year% 4 = 0 && Year%!= 0) | |-year% 400 = 0)

If the expression value is true (1), Year is not a leap year. You can also use the following logical expression to identify a non-leap year:

(Year% 4!= 0) | | (Year% = 0 && Year% 400!=0)

If the expression value is true, year is not a leap year. Note the operation precedence of the different operators (%,!,&&,==) within the brackets to the right of the expression.

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.