📄 relationalconditional.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>
<br>
数学上有比较的运算,像是大于、等于、小于等等,C++中也提供了这些运算子,这些运算子我们称之为“关系运算子”(Relational
operator)或“比较运算子”(Comparison
operator),它们有大于(>)、不小于(>=)、小于(<)、不大于(<=)、等于
(==)以及不等于(!=)。
<br>
<br>
请看看下面这几行会显示哪些数值: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
<< "10 >
5\t\t" << (10 > 5) << 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
<< "10
>= 5\t\t" << (10 >= 5) <<
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
<< "10
< 5\t\t" << (10 < 5) << 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
<< "10
<= 5\t\t" << (10 <= 5) <<
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
<< "10
== 5\t\t" << (10 == 5) << 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
<< "10
!= 5\t\t" << (10 != 5) << endl;</span><br>
</div>
<br>
程式的执行会显示0或1,分别表示false或true,如下所示: <br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">10 >
5
1<br>
10 >=
5
1<br>
10 <
5
0<br>
10 <=
5
0<br>
10 ==
5
0<br>
10 !=
5
1</span></small></td>
</tr>
</tbody>
</table>
<br>
在C++中,所有非零的数值在作为条件式时都被视为true。 <br>
<br>
关系运算在使用时有个即使是程式设计老手也可能犯的错误,且不容易发现,也就是等于运算子(==),注意它是两个连续的等号(=)所组成,而不是一个等
号,一个等号是指定运算子,这点必须相当注意,例如若有两个变数x与y要比较是否相等,是写成x == y,而不是x =
y,后者的作用是将y的值指定给x,而不是比较运算。 <br>
<br>
即然谈到了条件式的问题,我们来介绍C++中的“条件运算子”(Conditional operator),它的使用方式如下: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">条件式 ?
成立传回值 : 失败传回值</span><br>
</div>
<br>
条件运算子的传回值依条件式的结果而定,如果条件式的结果为true,则传回冒号前的值,若为false,则传回冒号后的值,下面这个程式可以作个简单的
示范:<br>
<br>
<pre>#include <iostream><br>using namespace std;<br> <br>int main() { <br> int score = 0; <br><br> cout << "输入学生分数:"; <br> cin >> score; <br> cout << "该生是否及格?" <br> << (score >= 60 ? 'Y' : 'N') <br> << endl; <br><br> return 0;<br>} </pre>
<br>
执行结果:<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">输入学生分数:60<br>
该生是否及格?Y </span></small></td>
</tr>
</tbody>
</table>
<br>
这个程式会依您所输入的分数来判断学生成绩是否不小于60分,以决定其是否及格,如果是则传回字元'Y',否则传回字元'N',下面这个程式可以判断使用
者输入是否为奇数:<br>
<br>
<pre>#include <iostream><br>using namespace std;<br> <br>int main() { <br> int input = 0; <br><br> cout << "输入整数:"; <br> cin >> input; <br> cout << "该数为奇数?" <br> << (input%2 ? 'Y' : 'N') <br> << endl;<br><br> return 0;<br>}</pre>
<br>
执行结果: <br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">输入整数:3 <br>
该数为奇数?Y </span></small></td>
</tr>
</tbody>
</table>
<br>
在C++中非零数值都可以表示true,而0表示false,所以当您输入的数为奇数时,就不能被2整除,所以余数一定不是0,在条件式中表示true,
因而传回字元'Y',若数值为偶数,则2整除,所以余数为0,在条件式中表示false,所以传回字元'N'<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -