[C Language Elementary] Take you to easily play with all common operators (3) - relational operators, logical operators, conditional operators, comma expressions

insert image description here

Junxi_'s personal homepage

Be diligent and encourage the years to wait for no one

C/C++ game development


Hello, this is Junxi_, I have been busy recently and I have time to update you. Today I bring you the third part about operators. Without further ado, let’s start directly!

  • Before the official start, we still use a mind map to help briefly recall the basic content of operators

insert image description here

  • Today we will talk about it after the unary operator

1. Relational operators

>
>=
<
<=
!=  用于测试“不相等”
==    用于测试“相等”
  • These relational operators are relatively simple, there is nothing to talk about, but we should pay attention to the pitfalls when using some operators.
  • For example the following code:
#include<stdio.h>
//判断并在屏幕上打印闰年
int main()
{
    
    
	int y = 0;
	scanf("%d", &y);
	//1. 能被4整除,并且不能被100整除
	//2. 能被400整除是闰年
	if (((y % 4 = 0) && (y % 100 != 0)) || (y % 400 == 0))
	{
    
    
		printf("闰年\n");
	}
	else
	{
    
    
		printf("不是闰年\n");
	}
	return 0;
}
  • In our code, what we want to judge is whether the number of years entered is divisible by 4, but pay attention here:
if (((y % 4 = 0) && (y % 100 != 0)) || (y % 400 == 0))
  • Here we accidentally wrote "y%4=="0 as assigning the value of 0 to y%4. At this time, the program can't even pass the compiler.
    insert image description here

warn:

  • In the process of programming, == and = are accidentally written wrong, resulting in errors

good coding style

  • 注意,在某些情况下,类似“==”和“=”用错时,编译器是不会报错的。
int main()
{
    
    
	int x = 0;
	if (x = 1)
	{
    
    
		printf("HEHE\n");
	}
	else
		printf("haha\n");
	return 0;
}

insert image description here

  • The initial value of x here is 0, and "haha" should be printed, but because we mistakenly wrote "==" as "=",
  • So the wrong print "HEHE", the compiler did not report an error at this time, because logically speaking, this code is not wrong, so how can we modify the code at this time to make the compiler remind us that there is an error in the code at this time?
int main()
{
    
    
	int x = 0;
	if (1=x)
	{
    
    
		printf("HEHE\n");
	}
	else
		printf("haha\n");
	return 0;
}

insert image description here

  • We can swap the positions of the left and right numbers. Since it is impossible for us to assign the value of a variable to a constant, we can use this method to remind us whether the code is wrong.
  • correct code
int main()
{
    
    
	int x = 0;
	if (1==x)
	{
    
    
		printf("HEHE\n");
	}
	else
		printf("haha\n");
	return 0;
}

insert image description here

  • It is undoubtedly a good code style to help us find possible bugs in the code through simple modifications like the above! !

2. Logical operators

What are the logical operators:

&&   逻辑与
||   逻辑或
  • Distinguish between logical and and bitwise and
  • Distinguish between logical or and bitwise or
1&2----->0//按位与作用与二进制补码,对应二进制位数都为1才为1,否则为0
1&&2---->1//一般作用于两边的条件,当两边的条件都i为真时才为真,否则为假
1|2----->3//按位或也是作用于二进制补码,对应二进制位数只要有一个为1就为1,只有两个同时为0才为0
1||2---->1//逻辑与两边条件只要有一个为真就为真,只有都为假才是假
  • I have already talked about the knowledge of bitwise operators such as bitwise and, bitwise or, etc. in the first detailed explanation of operators. If you are interested, you can take a look at the following link: [C Language Elementary] Take you to easily play with all commonly used operators (1) - arithmetic operator, shift operator, bit
    operator

Application of Logical Operators Interview Question Examples

  • Let's take a related interview question from a big factory to deepen everyone's impression of logical operators.
  • code show as below:
#include <stdio.h>
int main()
{
    
    
  int i = 0,a=0,b=2,c =3,d=4;
  i = a++ && ++b && d++;
  printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d);
  return 0;
  }
  • Try to judge the result of running the above program:
    insert image description here
  • Why?
  • As we said, as long as one of the conditions of the logical AND does not meet the condition, it is false. At this time, when the program runs to a++, because it is postfixed ++, a is equal to 0 at this time, which means it is false. This line of code does not continue to run downwards and stops directly. At this time, only a is added to the postfix, and the value of bcd remains unchanged, so the above printed result appears.
  • What if the code is like this?
#include <stdio.h>
int main()
{
    
    
  int i = 0,a=0,b=2,c =3,d=4;
   i = a++||++b||d++;
  printf("a = %d\n b = %d\n c = %d\n d = %d\n", a, b, c, d);
  return 0;
  }
  • What is the result of this code?
    insert image description here

  • Why?

  • We know that the logical or is true as long as one of the conditions on both sides of || is satisfied, we bring in a=0 at this time; at this time, post ++, the incoming 0 is still false, the logical or continues to run downwards, and the front ++b, at this time b=3, is true, and the logical or will no longer run downwards. At this time, only the values ​​of a and b are added to 1, and the rest are the original numbers, so the result on the screen is printed.

  • Through the above two examples of interview questions, I believe that you should have your own insights on logical operators, so let's continue to learn.


3. Conditional operator

exp1 ? exp2 : exp3//判断表达式1真假,条件如果为真,则执行表达式2,如果为假,则执行表达式3
  • Therefore, the above code can also be implemented with if
    if(exp1)
    exp2;
    else
    exp3;
  • Give a simple code example
int main()
{
    
    
	int a = 0;
	int b = 0;

	if (a > 5)
		b = 3;
	else
		b = -3;

	

	return 0;
}
  • Implemented with the conditional operator;
int main()
{
    
    
   int a=0;
   int b=0;
//(a > 5) ? (b = 3) : (b = -3);
	b = ((a > 5) ? (3) : (-3));//与上一行代码两者等价
	return 0;
}

comma expression

exp1, exp2, exp3, …expN
  • 逗号表达式,就是用逗号隔开的多个表达式。
  • 逗号表达式,从左向右依次执行。整个表达式的结果是最后一个表达式的结果。
  • Give some sample code
int main()
{
    
    
   //代码1
	int a = 1;
	int b = 2;
	int c = (a > b, a = b + 10, a, b = a + 1);//逗号表达式
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n", c);
	//代码2
   //if (a = b + 1, c = a / 2, d > 0)
   //代码3
  //a = get_val();
  //count_val(a);
  //while (a > 0)
  //{
    
    
  //	
  //		//业务处理
  //		a = get_val();
  //	count_val(a);
  //}
  //如果使用逗号表达式,改写:
  //while (a = get_val(), count_val(a), a > 0)

	return 0;
}
  • If you have problems understanding the above code, you might as well try to run it yourself! , I won’t go into details here.

Summarize

  • Today's content ends here for the time being. Today we talked about relational operators, logical operators, conditional operators, and the application of comma expressions and error-prone points. If you are still confused, you may wish to do it yourself. The knowledge in this area needs to be memorized, and we must practice repeatedly to get familiar with it!
  • The above is the content of the third part about operators. The detailed explanations of other operators will be updated in batches in the near future, so stay tuned! !
  • Well, if you have any questions, please ask me in the comment area or private message, see you next time!

It is not easy for a new blogger to create. If you feel that the content of the article is helpful to you, you may wish to click on this new blogger before leaving. Your support is my motivation to update! ! !

**(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)**

insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/131435005