Let us learn C in Code <7> operators

Hi, everyone! This article will introduce some mathematical and logical manipulation called operators. Actually we have learnt some operators in code through previous chapters, like the assignment signal "=" , addictive signal "+", subtraction signal  "-" multiplication signal "*" etc. This chapter we will learn the operators more detailed.

The operators are include arithmetic operators, relational operators,logical operators, bit wise operators, all these operators are taught in our primary school ,high school or university .So let us just list them on types

0) Arithmetic operators

OperatorMeansexample
 +add two operands 1+2
 -the first operands subtract the second operand 2-1
 *multiples two operands 1*2
 /divides numerator by sub-numerator 1/2


These basic arithmetic operators are enough for us to code lately, in the summary chapter  I will list them all. Now, we just need to get these.


1) Relational operators

Opratermeansexample
 >the left operand is more than the right one if right
return true ,otherwise return false
1 > 2
false
 <the left operand is less than the right one if right
return true, otherwise return false 
1<2
true
 ==

if the two operands equal, if right return true 

if not equal ,return false

1==2
false
 !=

if the two operands are not equal, if right return

 true, else return false

1!=2
true
 >=

the left operand is more than or equals the right one ,

 if right return true, otherwise return false

1>=2
false
 <=

the left operands is less than or equal the right one,

 if right return true, otherwise return false

1<=2
true


These relational operators are the basic relationship.

2) Logical operators

Now let us declare some variables  " int a = 1;   int b = 2;   int c = 3;   int d = 4; "

Logical signmeansexample
 &&AND(a>b)&&(c<d)
 ||OR(a>b)||(c<d)
 !NOT!(a>b)

In math, the logical true table shows below

AND:

x1010
y1100
 1000
OR

x1010
y1100
 1110
NOT

x10
 01

When you want to design a program depends on  the different situation ,you can use  these logical operators in your program . The judgement related program will be introduced later.

3)Bitwise Operators

Here we just talk about two bitwise operators. As the time goes on, we'll learn all of them in our Code.


OperatorMeansExample
&Bit AND if both bits are 1 ,the result is 1,
otherwise the result is 0.
0b01&0b11*
|Bit OR only one bit is 1 ,the result is 1,
otherwise the result is 0
0b01|0b00

*0b represents the binary


Ok,   we have list  a lot of operators , next, let us use some operators to solve the problems which we met in daily life.

0) I want to buy a bottle of coca cola which priced $2 , I give the cashier $5, and how much the cashier should give change?

1) My friend Jen. will have the birthday, i really want to give her a present,  but it really depends on the time and the money, if i have free time i can prepare a home made present, but if i don't have enough time , so just buy a present. How can i handle this situation?

2) Last year, I bought a new bicycle on $200, now today i want to sell it on 40 percent off, how much is the second hand bicycle now?

3) A switch can represent two state on or off , when turn it on  , the light lights. When turn it off ,the light off. But now I have two switches series connection , then join the light.Please detail the state of two switches and the light.


All these question are a logical or arithmetic problem, so think over and write down our own answers, next chapter we will learn the program base on conditions called Decision making, after get the decision making , we will be able to solve these problems in code.

Have a nice weekend everyone! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值