⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ifstatement.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  
  <title>if 条件判断</title>
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip: if 条件判断</a></h1>

<br>

为了应付程式所可能遇到的各种状况,C++提供了if 条件判断陈述,它的语法如下:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式)
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; 陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; 陈述句二;</span><br>

</div>

<br>

这个语法的意思,白话来说,就是当条件式成立时(true),则执行陈述句一,要不然就执行陈述句二;如果条件式不成立时并不想作任何事,则else可以
省略。 <br>

<br>

在 if 后如果有两个以上陈述句,称之为“复合陈述句”(compound statement),此时您必须使用{
}将复合陈述句包括起来,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式)
{ </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; 陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; 陈述句二; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else {
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; 陈述句三; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; 陈述句四; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

下面来写个简单的程式,判断使用者的输入是奇数还是偶数: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    int input = 0; <br>    int remain = 0; <br><br>    cout &lt;&lt; "输入整数:"; <br>    cin &gt;&gt; input; <br><br>    remain = input % 2; <br>    if(remain == 1) <br>        cout &lt;&lt; input &lt;&lt; "为奇数" &lt;&lt; endl; <br>    else <br>        cout &lt;&lt; input &lt;&lt; "为偶数" &lt;&lt; endl; <br><br>    return 0;<br>}</pre>

<span class="postbody"><br>

执行结果:</span><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<br>

10为偶数</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

这个程式很简单,使用余数运算来计算除以2后的余数,偶数的余数会是0,而奇数会是1,由此判断输入为奇数还是偶数;事实上如果您C++了解true与
false的意义的话,这个程式也可以这么写: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    int input = 0; <br><br>    cout &lt;&lt; "输入整数:"; <br>    cin &gt;&gt; input; <br><br>    if(input % 2) <br>        cout &lt;&lt; input &lt;&lt; "为奇数" &lt;&lt; endl; <br>    else <br>        cout &lt;&lt; input &lt;&lt; "为偶数" &lt;&lt; endl; <br>    <br>    return 0;<br>}</pre>

<span class="postbody"><br>

</span>这只是一点程式写作的技巧,因为在C++中0在条件式中相当于false,而非0值都被视为true,所以上面的程式可以直接将余数运算的
结果当作条件式来使用。 <br>

<br>

在 if 中您也可以再设定执行的条件,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式一) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
if(条件式二) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; 陈述句二; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句三; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

这只个简单的例子,其中陈述句二要执行,必须同时满足条件式一与二才行;再来看个例子: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式一) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; //
其它陈述句 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
if(条件式二) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; 陈述句二;</span><br>

</div>

<br>

如果条件式一不满足,就会执行else中的陈述,而您在这边进行条件式二的测试,如果满足就执行条件式二,由于C++是个自由格式语言,您可以适当的排列
这个程式,这会比较好懂一些: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式一) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; //
其它陈述句 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else if(条件式二) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; 陈述句二;</span><br>

</div>

<br>

基于这个方式,您可以如下设定多个条件,且易读易懂: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式一) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; 陈述一;
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else if(条件式二) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句二; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else if(条件式三) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句三; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; 陈述句四;</span><br>

</div>

<br>

陈述句四会在条件式一、二、三都不成立时执行;下面这个程式是个简单的例子,处理学生的成绩等级问题: <br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() { <br>    int score = 0; <br><br>    cout &lt;&lt; "输入分数:"; <br>    cin &gt;&gt; score; <br><br>    if(score &gt;= 90) <br>        cout &lt;&lt; "得A" &lt;&lt; endl; <br>    else if(score &gt;= 80 &amp;&amp; score &lt; 90) <br>        cout &lt;&lt; "得B" &lt;&lt; endl; <br>    else if(score &gt;= 70 &amp;&amp; score &lt; 80) <br>        cout &lt;&lt; "得C" &lt;&lt; endl; <br>    else if(score &gt;= 60 &amp;&amp; score &lt; 70) <br>        cout &lt;&lt; "得D" &lt;&lt; endl; <br>    else <br>        cout &lt;&lt; "得E(不及格)" &lt;&lt; endl; <br>    <br>    return 0;<br>}<br></pre>

<span class="postbody"><br>

</span>执行结果:<span class="postbody"></span><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>

得D</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

在这边要注意的是,if与else是就最近的一组来配对,例如下面这个程式依缩排来看,您觉得有无问题存在?<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式一) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
if(条件式二) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; 陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; 陈述句二;</span><br>

</div>

<br>

很多人都会以为条件式一的if会与else配对,但事实上是条件式二的if与else配对,加上括号的话就可以一目了然了: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(条件式一) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
if(条件式二) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; 陈述句一; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; else
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
陈述句二; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

如果想避免这种错误,在程式中多使用括号是必要的,多写一些总是比少写一些来得保险一点。
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -