C/C++ study notes: complete basic operators and expressions, detailed case demonstration

In the C language, we have learned:

  • Arithmetic operators (+, -, *, /, %)
  • Increment and decrement operators (++, --)
  • Assignment operator (=)
  • Relational operators (>, <, >=, <=, !=, ==)
  • Logical operators (&&, ||,!)

Let's review briefly!

Operator

1. What is an expression

Formulas connected by operators and operands, including: simple expressions and complex expressions

Second, what is a sentence

An expression ending with ; is a statement

C statement: every statement is; as the end

Empty statement: Nothing directly ends with a semicolon;

Statement block: {} enclosed in curly braces

Three, operator

1. Assignment operator (=)

Variable name = constant (variable/expression)

Lvalue: the left value that appears in the assignment statement (constant cannot be used as an lvalue)

Rvalue: Rvalue that appears on the right side of the assignment statement

Initialization: When declaring a variable, assign an initial value to it

Assignment: Given a value

A, automatic type conversion

Conversion rules:

If the value of a shorter numeric type variable is assigned to a longer numeric type variable, the value of the shorter numeric type variable will be upgraded and expressed as a longer data type, and the data information will not be lost.

If a variable of a longer value type is assigned to a variable of a shorter value type, the data will be lowered and the data will exceed the range of the shorter value type, and data truncation will occur.

B, forced type conversion

If the data types are different, the type conversion will be performed automatically according to different situations, and the compiler will prompt a warning message. If you use forced type conversion, the compiler will not generate a warning.

General form: (type name) (expression)

2. Arithmetic operators

Unary operators: +,-, ++, -

Binary operator: +-* /% (modulo operator to find the remainder) (the operands on both sides can only be integers, and the sign of the remainder is determined according to the dividend)

Unary operator ++ - +-

The expression ++ is incremented before assigning

        ++Increase after first assignment

Priority, associativity

* /

+ -

=

3. Relational operators

 Used to determine the size relationship between two operands (return true or false)

0 means false, non-zero means true (-1, -2, 1, 2) and 1 means true

>

>=

<

<=

==

!=

4. Logical operators

&& logical and two are true, one is false, and one is false

|| Logical or one is true,

! Logic is not true or false, false, true

Short circuit phenomenon

if ((a=0) && (b=45))
{
//为假就不会执行,为真才执行
printf("真的\n");
}
else
{
//为假
printf("假的\n");
printf("a=%d,b=%d\n",a,b);
}
 
if ((a=45) || (b=2))
{
//为假就不会执行,为真才执行
printf("真的\n");
printf("a=%d,b=%d\n",a,b);//a=45,b=0
}
else
{
//为假
printf("假的\n");
printf("a=%d,b=%d\n",a,b);
}

5. Conditional Operator (Ternary Operator)

?:

expression? Statement 1: Statement 2

If the expression is true, execute statement 1

Is false, execute statement 2

Nesting: 1> 2? Printf("is true\n"): 2> 3?printf("2> 3\n") :printf("2 <3 \n");

 

//if else statement

in case

....

otherwise

....

6, bit operator

  • & Bitwise and
  • | Bitwise OR
  • ^ The bitwise XOR is the same as 0, and the difference is 1
  • ~ Bitwise negation
  •  Source code
  • Complement
  • Complement 
  • Positive number: three codes in one
  • Negative number: inverse code: the sign bit remains unchanged, and the remaining bits are inverted bit by bit
  • Complement code: the sign bit remains unchanged, the complement code is +1

7. The comma operator:

Multiple expressions can be separated by commas, and the value of the entire expression is the value of the last expression.

8, compound operator

 +=, -=, *=, /=, %=, ^=, |=, etc. 

Since the knowledge grammar has basically not changed, we will focus on the actual program demonstration in this part

 To split the number of bits, we can implement it in C++ as follows:

#include<iostream>
using namespace std;
int main()
{
    int a;//待判断的这个三位数
    int ge;//三位数中的个位
    int shi;//三位数中的十位
    int bai;//三位数中的百位
    cin>>a;
    ge = a%10;
    shi = a%100/10;
    bai = a/100;
    cout<<ge<<" "<<shi<<" "<<bai<<endl;
    return 0;
}

#include<iostream>
using namespace std;
int main()
{
   int x,y;
   cin>>x;
   if(x<1)
   {
      y=x;
   }
   else if(1<=x && x<10)
   {
      y=2*x-1;
   }
   else
   {
      y=3*x-11;
   }
   cout<<y<<endl;
   return 0;
}

The results are as follows:

 

 

Everyone still pays attention to the usage scenarios of the logical operator &&, don't use consecutive words like 1<=x<10!

Friends who are interested in programming can click to learn more , or add skirts: ①①⑤①③⑨⑤⑨⑦⑨, you can get video materials and online e-books for free~ There is also an online Q&A program by a master, and regular free classes

 

Guess you like

Origin blog.csdn.net/Python6886/article/details/111350027