class="tags" href="/C.html" title=
c>
content_views"
class="tags" href="/C.html" title=
c>
class="markdown_views prism-tomorrow-night">
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/class="tags" href="/C.html" title=c>cb7f73class="tags" href="/C.html" title=c>cda1a14130840e356class="tags" href="/C.html" title=c>cb0b6f9e4.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="100" />
<
class="tags" href="/C.html" title=
c>
center>
博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳]
class="tags" href="/C.html" title=
c>
center>
<
class="tags" href="/C.html" title=
c>
center>
本文专栏: C++
class="tags" href="/C.html" title=
c>
center>
class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />
💯前言
- C++ 作为一种高效、力量强大的编程语言class="tags" href="/C.html" title=c>c;提供了常用的控制流路:分支、循环和函数调用。循环语句中class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 是一种特殊的存在class="tags" href="/C.html" title=c>c;它与 <class="tags" href="/C.html" title=c>code>whileclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>forclass="tags" href="/C.html" title=c>code> 循环有突出的区别:不管条件是否满足class="tags" href="/C.html" title=c>c;循环体都会至少执行一次。本文将进一步分析 <class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 循环的语法规则、执行流程class="tags" href="/C.html" title=c>c;并提供一些实例和扩展想法class="tags" href="/C.html" title=c>c;以助助读者入门。
C++ 参考手册
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/f0db7class="tags" href="/C.html" title=c>c348331440fa266b3class="tags" href="/C.html" title=c>c24f558694.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />
💯1. 介绍 <class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 语法格式
1.1 基本语法
C++ 中class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 的基本语法分为两种格式:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 格式 1
class="tags" href="/C.html" title=c>class="token keyword">do语句class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(表达式class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 格式 2
class="tags" href="/C.html" title=c>class="token keyword">do class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{语句class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;语句class="tags" href="/C.html" title=c>class="token number">2class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(表达式class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>
特点:
- 这种循环会先执行循环体class="tags" href="/C.html" title=c>c;然后再判断条件是否满足class="tags" href="/C.html" title=c>c;所以保证循环体至少执行一次。
- 若条件为真class="tags" href="/C.html" title=c>c;重复执行循环体;若条件为假class="tags" href="/C.html" title=c>c;跳出循环class="tags" href="/C.html" title=c>c;进入循环体下一行。
1.2 <class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 与 <class="tags" href="/C.html" title=c>code>while/forclass="tags" href="/C.html" title=c>code> 区别
- <class="tags" href="/C.html" title=c>code>whileclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>forclass="tags" href="/C.html" title=c>code>:在进入循环体之前先判断条件class="tags" href="/C.html" title=c>c;如果条件为假class="tags" href="/C.html" title=c>c;直接跳过循环体class="tags" href="/C.html" title=c>c;从不执行循环内容。
- <class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code>:先执行循环体class="tags" href="/C.html" title=c>c;再判断条件class="tags" href="/C.html" title=c>c;因此class="tags" href="/C.html" title=c>c;循环体至少执行一次。
实例比较:
如果要打印1-5class="tags" href="/C.html" title=c>c;调用两种循环:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// while循环class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator"><= class="tags" href="/C.html" title=c>class="token number">5class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">" "class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 重置 class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// do-while循环class="tags" href="/C.html" title=c>class="token keyword">do class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">" "class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator"><= class="tags" href="/C.html" title=c>class="token number">5class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
运行结果:
两种循环都会输出「1 2 3 4 5」class="tags" href="/C.html" title=c>c;但当初始化条件不满足时class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>whileclass="tags" href="/C.html" title=c>code> 会直接跳过class="tags" href="/C.html" title=c>c;而 <class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 会先执行一次。
💯2. 执行流程分析
2.1 执行流程图
根据流程图:
- 先执行循环体(图中“语句”)。
- 再执行“判断表达式”。
- 如果表达式为真class="tags" href="/C.html" title=c>c;重复这个过程;若为假class="tags" href="/C.html" title=c>c;跳出循环。
- 循环中可以使用 <class="tags" href="/C.html" title=c>code>breakclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>continueclass="tags" href="/C.html" title=c>code> 进行中断和跳过一次。
2.2 分析比较
- 循环第一次执行: 直接执行class="tags" href="/C.html" title=c>c;不判断条件。
- 后续循环: 执行循环体后class="tags" href="/C.html" title=c>c;判断条件class="tags" href="/C.html" title=c>c;决定是否继续。
💯3. 实例分析
3.1 课上例子:打印1-10
原始代码:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 初始化变量 iclass="tags" href="/C.html" title=c>c;初始值为 1class="tags" href="/C.html" title=c>class="token keyword">do class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< i class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">" "class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出当前的 i 值class="tags" href="/C.html" title=c>c;并在后面加一个空格i class="tags" href="/C.html" title=c>class="token operator">= i class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// i 值自增 1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(i class="tags" href="/C.html" title=c>class="token operator"><= class="tags" href="/C.html" title=c>class="token number">10class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 循环条件:当 i <= 10class="tags" href="/C.html" title=c>c;继续循环class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 返回 0 表示程序正常结束
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
运行过程:
- 初始化变量class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>i = 1class="tags" href="/C.html" title=c>code>。
- 先执行循环体class="tags" href="/C.html" title=c>c;打印 <class="tags" href="/C.html" title=c>code>i = 1class="tags" href="/C.html" title=c>code>。
- 执行自增class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>i = 2class="tags" href="/C.html" title=c>code>。
- 判断条件class="tags" href="/C.html" title=c>c;如果满足class="tags" href="/C.html" title=c>c;进入下一轮循环。
- 循环到 <class="tags" href="/C.html" title=c>code>i = 11class="tags" href="/C.html" title=c>code> 时class="tags" href="/C.html" title=c>c;条件不满足class="tags" href="/C.html" title=c>c;退出循环。
输出:
<class="tags" href="/C.html" title=c>code>1 2 3 4 5 6 7 8 9 10
class="tags" href="/C.html" title=c>code>
3.2 循环应用扩展
例子:打印1-10的平方
将上面代码优化:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">do class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< i class="tags" href="/C.html" title=c>class="token operator">* i class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">" "class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出 i 的平方i class="tags" href="/C.html" title=c>class="token operator">= i class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(i class="tags" href="/C.html" title=c>class="token operator"><= class="tags" href="/C.html" title=c>class="token number">10class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
输出:
<class="tags" href="/C.html" title=c>code>1 4 9 16 25 36 49 64 81 100
class="tags" href="/C.html" title=c>code>
例子:用户输入菜单
循环在访问菜单系统中应用广泛class="tags" href="/C.html" title=c>c;如:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int optionclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">do class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"\u8bf7\u9009\u62e9\u83dclass="tags" href="/C.html" title=c>c\u5355\u9879\uff1a\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"1. \u5f00\u59class="tags" href="/C.html" title=c>cb\u6e38\u620f\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"2. \u8bbe\u7f6e\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token string">"3. \u9000\u51fa\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>cin class="tags" href="/C.html" title=c>class="token operator">>> optionclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">} class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(option class="tags" href="/C.html" title=c>class="token operator">!= class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
💯小结
- class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/74f42bclass="tags" href="/C.html" title=c>c984494class="tags" href="/C.html" title=c>cad8bclass="tags" href="/C.html" title=c>cea7783f07class="tags" href="/C.html" title=c>c995.png" alt="在这里插入图片描述" />
<class="tags" href="/C.html" title=c>code>do-whileclass="tags" href="/C.html" title=c>code> 循环在 C++ 中是一种特殊而实用的循环格式class="tags" href="/C.html" title=c>c;选择使用时class="tags" href="/C.html" title=c>c;需要重点考虑循环体是否必需要执行至少一次。通过上述分析class="tags" href="/C.html" title=c>c;希望能帮助读者更好地理解和实现这种循环。
class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />
学习C++是一段既充满挑战又令人收获颇丰的旅程。作为一门历史悠久且广泛应用的编程语言class="tags" href="/C.html" title=c>c;C++不仅提供了强大的功能class="tags" href="/C.html" title=c>c;还以其复杂性闻名。因此class="tags" href="/C.html" title=c>c;在学习这门语言的过程中class="tags" href="/C.html" title=c>c;我深刻体会到了它的深度和广度class="tags" href="/C.html" title=c>c;同时也总结了一些心得class="tags" href="/C.html" title=c>c;希望对未来的学习者有所帮助。
初识C++:从基础开始
在接触C++之前class="tags" href="/C.html" title=c>c;我对编程的了解仅限于一些基础的概念class="tags" href="/C.html" title=c>c;例如变量、循环和条件语句。C++的学习从这些最基本的部分开始class="tags" href="/C.html" title=c>c;帮助我夯实了编程的基础。C++的语法严格class="tags" href="/C.html" title=c>c;这在一开始可能会让人感到不适应class="tags" href="/C.html" title=c>c;但也正是因为这种严格性class="tags" href="/C.html" title=c>c;它迫使我养成了良好的编程习惯。例如class="tags" href="/C.html" title=c>c;必须明确变量的类型class="tags" href="/C.html" title=c>c;让我更注重程序的逻辑性和可读性。
掌握面向对象编程思想
C++最具特色的部分之一就是面向对象编程(OOP)。初次接触类和对象时class="tags" href="/C.html" title=c>c;我对这种编程范式感到新奇而又困惑。OOP的核心思想是将现实世界中的事物抽象为类class="tags" href="/C.html" title=c>c;通过类的实例化生成对象class="tags" href="/C.html" title=c>c;从而以更直观的方式管理程序的复杂性。
我记得刚开始学习类时class="tags" href="/C.html" title=c>c;写了一个关于"学生管理系统"的小程序。通过定义"学生"类class="tags" href="/C.html" title=c>c;设置姓名、学号和成绩等属性class="tags" href="/C.html" title=c>c;再为类添加一些方法class="tags" href="/C.html" title=c>c;如打印信息和计算平均分class="tags" href="/C.html" title=c>c;我逐渐理解了OOP的实际应用价值。这种封装、继承和多态的思想让我感受到C++的强大class="tags" href="/C.html" title=c>c;也让我认识到软件设计中的灵活性。
理解指针与内存管理
如果说C++中有什么让我印象最深刻且花费最多时间去理解的内容class="tags" href="/C.html" title=c>c;那一定是指针和内存管理。指针是C++的一大特色class="tags" href="/C.html" title=c>c;同时也是许多初学者的难点。我在学习过程中经常因为指针的误用导致程序崩溃class="tags" href="/C.html" title=c>c;甚至遇到一些难以调试的问题。
为了更好地理解指针class="tags" href="/C.html" title=c>c;我专门阅读了一些资料class="tags" href="/C.html" title=c>c;并亲手编写了许多练习程序。例如class="tags" href="/C.html" title=c>c;我通过编写动态数组、链表和二叉树等数据结构class="tags" href="/C.html" title=c>c;深刻体会到了指针在动态内存分配中的重要性。通过这些实践class="tags" href="/C.html" title=c>c;我不仅掌握了指针的使用class="tags" href="/C.html" title=c>c;还学会了如何使用<class="tags" href="/C.html" title=c>code>newclass="tags" href="/C.html" title=c>code>和<class="tags" href="/C.html" title=c>code>deleteclass="tags" href="/C.html" title=c>code>进行内存管理class="tags" href="/C.html" title=c>c;以及如何避免内存泄漏。
学习STL的便利性
C++的标准模板库(STL)是学习过程中的一个亮点。STL提供了许多现成的数据结构和算法class="tags" href="/C.html" title=c>c;例如向量(<class="tags" href="/C.html" title=c>code>veclass="tags" href="/C.html" title=c>ctorclass="tags" href="/C.html" title=c>code>)、队列(<class="tags" href="/C.html" title=c>code>queueclass="tags" href="/C.html" title=c>code>)、栈(<class="tags" href="/C.html" title=c>code>staclass="tags" href="/C.html" title=c>ckclass="tags" href="/C.html" title=c>code>)以及哈希表(<class="tags" href="/C.html" title=c>code>unordered_mapclass="tags" href="/C.html" title=c>code>)等。在学习STL之前class="tags" href="/C.html" title=c>c;我花了大量时间自己实现这些数据结构class="tags" href="/C.html" title=c>c;而STL的出现大大简化了我的开发过程。
通过学习STLclass="tags" href="/C.html" title=c>c;我认识到了高效代码的重要性class="tags" href="/C.html" title=c>c;也学会了如何快速解决问题。例如class="tags" href="/C.html" title=c>c;在一次编程竞赛中class="tags" href="/C.html" title=c>c;我需要快速实现一个排序功能class="tags" href="/C.html" title=c>c;而STL中的<class="tags" href="/C.html" title=c>code>sortclass="tags" href="/C.html" title=c>code>函数帮助我节省了大量时间。与此同时class="tags" href="/C.html" title=c>c;我还了解了STL背后的一些实现原理class="tags" href="/C.html" title=c>c;例如迭代器的使用和时间复杂度的分析。
错误与调试:不可避免的成长
在学习C++的过程中class="tags" href="/C.html" title=c>c;错误和调试是不可避免的。无论是编译错误还是运行时错误class="tags" href="/C.html" title=c>c;几乎每次编写代码时都要面对各种各样的问题。这让我一开始感到沮丧class="tags" href="/C.html" title=c>c;但随着经验的积累class="tags" href="/C.html" title=c>c;我学会了如何分析错误信息并快速定位问题。
调试工具是我的好帮手。例如class="tags" href="/C.html" title=c>c;使用<class="tags" href="/C.html" title=c>code>gdbclass="tags" href="/C.html" title=c>code>调试器让我能够逐行检查代码的执行过程class="tags" href="/C.html" title=c>c;而现代的集成开发环境(IDE)如Visual Studio则提供了更直观的调试界面。此外class="tags" href="/C.html" title=c>c;我还养成了在代码中添加日志和断点的习惯class="tags" href="/C.html" title=c>c;这些技巧帮助我在处理复杂问题时更加得心应手。
提升编程效率的技巧
随着对C++的深入学习class="tags" href="/C.html" title=c>c;我也总结了一些提升编程效率的技巧:
- 模块化编程:将代码分成多个文件class="tags" href="/C.html" title=c>c;使得每个模块专注于完成特定的任务class="tags" href="/C.html" title=c>c;从而提高代码的可读性和可维护性。
- 注重代码规范:遵循一致的命名规则和缩进风格class="tags" href="/C.html" title=c>c;让代码更易于理解。
- 利用C++11/14/17的新特性:例如class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>autoclass="tags" href="/C.html" title=c>code>关键字简化了变量类型的声明class="tags" href="/C.html" title=c>c;智能指针(<class="tags" href="/C.html" title=c>code>std::shared_ptrclass="tags" href="/C.html" title=c>code>和<class="tags" href="/C.html" title=c>code>std::unique_ptrclass="tags" href="/C.html" title=c>code>)帮助更安全地管理内存。
- 多阅读优秀代码:通过阅读开源项目中的代码class="tags" href="/C.html" title=c>c;我学到了很多实用的编程技巧和设计模式。
总结与展望
学习C++不仅让我掌握了一门编程语言class="tags" href="/C.html" title=c>c;更让我养成了严谨的思维方式。在这个过程中class="tags" href="/C.html" title=c>c;我学会了如何从问题中总结经验class="tags" href="/C.html" title=c>c;如何不断优化自己的代码class="tags" href="/C.html" title=c>c;以及如何以更高效的方式解决实际问题。
未来class="tags" href="/C.html" title=c>c;我希望能够继续深入学习C++的高级特性class="tags" href="/C.html" title=c>c;例如模板编程和多线程开发class="tags" href="/C.html" title=c>c;同时也希望能够将C++应用于更多实际项目中class="tags" href="/C.html" title=c>c;如游戏开发、嵌入式系统和高性能计算。我相信class="tags" href="/C.html" title=c>c;通过不断的实践和学习class="tags" href="/C.html" title=c>c;我将在C++的世界中发现更多的乐趣和可能性。
总之class="tags" href="/C.html" title=c>c;C++是一门值得花时间深入学习的语言。尽管它的学习曲线较陡class="tags" href="/C.html" title=c>c;但正是这种挑战让我更加热爱编程class="tags" href="/C.html" title=c>c;也让我在每次克服困难后获得了巨大的成就感。