【C++】do-while 循环

server/2025/1/1 7:12:30/
class="tags" href="/C.html" title=c>cle class="tags" href="/C.html" title=c>class="baidu_pl">
class="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content" class="tags" href="/C.html" title=c>class="articlass="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content class="tags" href="/C.html" title=c>clearfix">
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>cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-bloclass="tags" href="/C.html" title=c>ck" style="-webkit-tap-highlight-class="tags" href="/C.html" title=c>color: rgba(0, 0, 0, 0);">

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>class="toclass="tags" href="/C.html" title=c>c">

文章目录

  • 💯前言
  • 💯1. 介绍 `do-while` 语法格式
    • 1.1 基本语法
    • 1.2 `do-while` 与 `while/for` 区别
  • 💯2. 执行流程分析
    • 2.1 执行流程图
    • 2.2 分析比较
  • 💯3. 实例分析
    • 3.1 课上例子:打印1-10
    • 3.2 循环应用扩展
      • 例子:打印1-10的平方
      • 例子:用户输入菜单
  • 💯小结


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-5࿰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 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 执行流程图

根据流程图:

  1. 先执行循环体(图中“语句”)。
  2. 再执行“判断表达式”。
  3. 如果表达式为真࿰class="tags" href="/C.html" title=c>c;重复这个过程;若为假࿰class="tags" href="/C.html" title=c>c;跳出循环。
  4. 循环中可以使用 <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">// 初始化变量 i࿰class="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 <= 10࿰class="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>

运行过程:

  1. 初始化变量࿰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>。
  2. 先执行循环体࿰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>。
  3. 执行自增࿰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>。
  4. 判断条件࿰class="tags" href="/C.html" title=c>c;如果满足࿰class="tags" href="/C.html" title=c>c;进入下一轮循环。
  5. 循环到 <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的出现大大简化了我的开发过程。

通过学习STL࿰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;而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;我也总结了一些提升编程效率的技巧:

  1. 模块化编程:将代码分成多个文件࿰class="tags" href="/C.html" title=c>c;使得每个模块专注于完成特定的任务࿰class="tags" href="/C.html" title=c>c;从而提高代码的可读性和可维护性。
  2. 注重代码规范:遵循一致的命名规则和缩进风格࿰class="tags" href="/C.html" title=c>c;让代码更易于理解。
  3. 利用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>)帮助更安全地管理内存。
  4. 多阅读优秀代码:通过阅读开源项目中的代码࿰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;也让我在每次克服困难后获得了巨大的成就感。


class="tags" href="/C.html" title=c>class="blog-vote-box">

http://www.ppmy.cn/server/154245.html

相关文章

文档大师:打造一站式 Word 报告解决方案1

前言 在政府、医院、银行、财务以及销售等领域&#xff0c;常常需要创建各种报告文件来展开工作汇报&#xff0c;譬如季度销售报告、年度总结报告、体检报告和保险合同等。在没有报表工具支持之前&#xff0c;这类报告主要通过 Word 制作&#xff0c;费时费力且难以维护&#…

【Seata】分布式事务问题和理论基础

目录 1.分布式事务问题 1.1本地事务 1.2分布式事务 2.理论基础 2.1CAP定理 2.1.1一致性 2.1.2可用性 2.1.3分区容错 2.1.4矛盾 2.2BASE理论 2.3解决分布式事务的思路 1.分布式事务问题 1.1本地事务 本地事务&#xff0c;也就是传统的单机事务。在传统数据库事务中…

大型系统中 Redis 的优化与挑战

一、引言 在当今数字化浪潮下&#xff0c;分布式系统已然成为构建大规模、高性能应用的中流砥柱&#xff0c;广泛应用于电商、社交、金融等诸多领域。而 Redis&#xff0c;作为一款开源的高性能键值对存储数据库&#xff0c;在其中扮演着不可或缺的关键角色。它以内存存储为基…

14. HDFS基准测试

当Hadoop集群搭建完成后&#xff0c;为了弄清楚HDFS的读写性能&#xff0c;我们可以对集群做一次基准测试。Hadoop安装包中自带了对HDFS进行基准测试的工具类。 一. 测试写入速度 查看命令帮助 [roothadoop1 www]# yarn jar hadoop-3.2.4/share/hadoop/mapreduce/hadoop-mapre…

Django中创建自增主键字段的几种方法

Django是一个高级的Python Web框架,它鼓励快速开发和简洁、实用的设计。在Django中,数据库模型是用来定义应用程序数据结构的类。每个模型映射到一个单独的数据库表,而模型的属性则映射到该表的列。 在很多情况下,我们需要为模型创建一个自增主键字段。Django提供了几种方…

Axure10

如果还是不行就将字体图标安装在控制面板–字体下 打开原型了之后&#xff0c;icon没有 一定要将字体库放到–》控制面板\外观和个性化\字体 里面

【测试】接口测试

长期更新好文&#xff0c;建议关注收藏&#xff01; 目录 接口规范接口测试用例设计postmanRequests 复习HTTP超文本传输协议 复习cookiesession 实现方式 1.工具 如postman ,JMeter&#xff08;后者功能更全&#xff09; 2.代码 pythonrequests / javahttpclient【高级】 接…

惠州市政数局局长杨伟斌:惠州市公共数据授权运营模式探索

近期&#xff0c;2024数字资产管理大会召开。会上&#xff0c;惠州市政务服务和数据管理局局长杨伟斌在会上做了题为基于“隐私计算区块链”的惠州市公共数据授权运营模式探索主旨演讲&#xff0c;从三个方面展开&#xff0c;一是建制度汇数据&#xff0c;二是夯基础保安全&…