C# operators and data types

C# arithmetic operator and character operator concatenation and use

Arithmetic operators are the most commonly used type of operators, including addition, subtraction, multiplication, division, etc.

Operator Description
+ Add two operands
- Subtract two operands
* Multiply two operands
/ Divide two operands
% Do the remainder operation on two operands

Code demo

 class Program
    {
        static void Main(string[] args)
        {
            /* 这里是输出注释*/
            Console.WriteLine("Hello World!");
            Console.WriteLine("1+2=" +3);
            Console.WriteLine("1+2=" +1+2);//拼接
            Console.WriteLine("1+2="+(1+2));
            Console.WriteLine("1-2="+(1-2));
            Console.WriteLine("1*2="+(1*2));
            Console.WriteLine("4/2="+(4/2));
            Console.WriteLine("10%3="+(10%3));
        }
    }

Run as shown

Insert picture description here

C# Boolean type True and False

In the C# language, the boolean type is declared with bool, and it has only two values, namely True and False.

Code demo

  class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("3>5=" + (3 > 5));
            Console.WriteLine("3>2=" + (3 > 2));
        }
    }

Run as shown

Insert picture description here

C# logical operators

Logical operators mainly include AND, OR, NOT, etc., which are mainly used for operations between multiple Boolean expressions.

Operator meaning Description
&& Logical and If both sides of the operator are True, the entire expression is True, otherwise it is False; if the left operand is False, the right expression is not calculated, which is equivalent to the meaning of "and"
Double vertical line Logical OR 2
Logical negation 2

Guess you like

Origin blog.csdn.net/caoguanghui0804/article/details/114228228