📄 incrementdecrement.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>递增、递减、指定运算</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++ Gossip: 递增、递减、指定运算</a></h1>
在程式中对变数递增1或递减1是很常见的运算,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int i = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">i = i + 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << i
<< endl;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">i = i - 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << i
<< endl;</span><br>
</div>
<br>
这段程式会分别显示出1与0两个数,您也可以这么写这个程式:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int i = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << ++i
<< endl;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << --i
<< endl;</span><br>
</div>
<br>
其中写在变数 i 之前的++与--就是C++的“递增运算子”(Increment operator)与“递减运算子”(Decrement
operator),当它们撰写在变数之前时,其作用就相当于将变数递增1与递减1: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">++i; // i
= i + 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">--i;
// i = i - 1;</span><br>
</div>
<br>
您可以将递增或递减运算子撰写在变数之前或变数之后,但其实两者是有差别的,将递增(递减)运算子撰写在变数前时,表示先将变数的值加(减)1,然后再传
回变数的值,将递增(递减)运算子撰写在变数之后,表示先传回变数值,然后再对变数加(减)1,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int i = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int num = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">num = ++i;
// 相当于i = i + 1; num = i; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << num
<< endl; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">num = --i;
// 相当于i = i - 1; num = i; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << num
<< endl;</span><br>
</div>
<br>
在这段程式中,num的值会前后分别显示为1与0,再看看下面这段:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int i = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int num = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">num = i++;
// 相当于num = i; i = i + 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << num
<< endl;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">num = i--;
// 相当于 num = i; i = i - 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << num
<< endl;</span><br>
</div>
<br>
在这段程式中,num的值会显示前后分别为0与1。 <br>
<br>
接下来看“指定运算子”(Assignment
operator),到目前为止我们只看过一个指定运算子,也就是=这个运算子,事实上指定运算子还有以下的几个: <br>
<br>
<table style="text-align: left; width: 50%;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(204, 204, 204);"><small>运算子</small></td>
<td style="background-color: rgb(204, 204, 204);"><small>例子</small></td>
<td style="background-color: rgb(204, 204, 204);"><small>说明</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>+=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a += b </small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a + b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>-=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a -= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a - b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>*=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a *= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a * b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>/=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a /= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a / b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>%=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a %= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a % b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>&=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a &= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a & b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>|=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a |= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a | b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>^=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a ^= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a ^ b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small><<=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a <<= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a << b</small></td>
</tr>
<tr>
<td style="font-family: Courier New,Courier,monospace;"><small>>>=</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a >>= b</small></td>
<td style="font-family: Courier New,Courier,monospace;"><small>a = a >> b</small></td>
</tr>
</tbody>
</table>
<br>
就单一个陈述而言,使用++、--或指定运算子是还算可以理解,但与其它陈述结合时可就得考虑一下,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int i = 5; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">arr[--i %= 10] =
10;</span><br>
</div>
<br>
像这样的式子,i
是多少,以及阵列的指定索引位置在哪可就得想一下了(有兴趣算一下的话,i会是4,而阵列的指定索引也是4),总之,如何使用与何时使用,自己得拿捏着
点。 <br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -