for循环在c++中的用法

Loops come into picture when we need to execute a particular action in a repeated manner.

当我们需要重复执行特定的动作时,就会出现循环。



C ++中的循环类型 (Types of loops in C++)

C++ Loops C ++循环

There are two broad categories of loops in C++ :

C ++中有两大类循环:

  • Entry Controlled Loops入口控制回路
  • Exit Controlled Loops退出控制循环


入口控制回路 (Entry Controlled Loops)

In entry controlled loops, the test expressions are tested beforehand i.e. they are tested before the execution of the preceding statements.

在条目控制的循环中,测试表达式是预先测试的,即它们是在执行前面的语句之前进行测试的。

There are two entry controlled loops:

有两个条目控制的循环:

  • for Loop循环
  • while Loopwhile循环

对于循环 (For Loops)

The for loop is an iterative statement that executes for a given number of times based on the initialization expression.

for循环是一个迭代语句,该语句根据初始化表达式执行给定的次数。

For Loop C++
对于循环C ++

Working of For Loops:

For循环的工作:

The for loops use a loop variable as a pointer to point to the particular instance of the loop being executed.

for循环使用循环变量作为指针来指向要执行的循环的特定实例。

  • Initially, the loop variable is declared and initialized. 最初,循环变量被声明和初始化。
  • Post which, the validity of the test expression in accordance with the particular program is done.之后,根据特定程序完成测试表达式的有效性。
  • Then, the loop variable is updated to a certain value.然后,将循环变量更新为某个值。

The above steps are repeated until it meets the exit condition.

重复上述步骤,直到满足退出条件。

Note: In for loops, the number of iterations i.e. the number of times the loop will execute is known by definition.

注意:在for循环中,迭代次数(即循环执行的次数)在定义上是已知的。

Syntax:

句法:


for (initialization expression; Test expression; Update expression)
{    // body of the loop
}

Example:

例:


#include <iostream>
using namespace std; int main()
{ for (int x = 0; x < 10; x++) { cout <<" "<< x; } return 0;
} 

Output:

输出:

0 1 2 3 4 5 6 7 8 9

While循环 (While Loops)

In while loops, the termination of the loop completely depends on the test expression because in this loop we do not specify the number of iterations.

在while循环中,循环的终止完全取决于测试表达式,因为在此循环中,我们不指定迭代次数。

While Loop In C++++
在C ++++中进行While循环

Syntax:

句法:


Initialization_expression;
while (Test_condition)
{// statementsUpdate_expression;
}

Example:

例:


#include <iostream>
using namespace std; int main()
{   int x = 0;while(x<10){cout<<" "<<x;x++;}return 0;
} 

Output:

输出:


0 1 2 3 4 5 6 7 8 9


退出控制循环 (Exit Controlled Loops)

In exit controlled loops, the test expression is analyzed and evaluated at the end of the loop.

在出口控制的循环中,测试表达式在循环结束时进行分析和评估。

Thus, the loop will run and execute at-least-once irrespective of the test expression.

因此,无论测试表达式如何,循环都会至少运行一次并执行。

循环执行 (Do-while loop)

Do-while loops serve the same purpose as that of the while loops. But, in do-while loops, the test condition is evaluated at the end of the loop.

Do-while循环的功能与while循环的目的相同。 但是,在do-while循环中,测试条件在循环结束时进行评估。

Do While Loop In C++
在C ++中执行While循环

As you must have understood from the illustration above, the loop will always execute first, check for the condition later.

正如您必须从上图理解的那样,循环将始终首先执行,然后再检查条件。

Syntax:

句法:


Initialization_Expression;
do
{// statementsUpdate_Expression;
} while (Test_Expression);

Example:

例:


#include <iostream>
using namespace std; int main()
{ int x = 0; do{ cout <<" "<<x; x++; } while (x < 10); return 0;
} 

Output:

输出:

0 1 2 3 4 5 6 7 8 9


循环控制语句 (Loop Control Statements)

The following loops statements are necessary to decide and control the flow of termination of the loop in the program:

以下循环语句对于确定和控制程序中循环终止的流程是必需的:

  • break statement: As soon as the compiler encounters the break statement, the loop terminates on an immediate basis and the flow of control gets passed to the statement following the loop.break语句 :编译器遇到break语句时,循环立即终止,并且控制流在循环后传递给该语句。
  • continue statement: As soon as the compiler encounters the continue statement at a particular instance of test-condition, it immediately leaves the current condition of the loop and restarts the loop.continue语句 :编译器在特定的test-condition实例上遇到Continue语句时,它将立即离开循环的当前条件并重新启动循环。


结论 (Conclusion)

Thus, in this article, we have understood the basic working of the C++ loops.

因此,在本文中,我们已经了解了C ++循环的基本工作。



参考资料 (References)

C++ Loops Documentation

C ++循环文档

翻译自: https://www.journaldev.com/35146/loops-in-c-plus-plus

for循环在c++中的用法

for循环在c++中的用法_C ++中的循环相关推荐

  1. python foreach用法_C# 中 foreach 遍历的用法

    foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素. 该循环的运行过程如下: ...

  2. java attributes用法_C#中的Attributes的用法

    今天研究了一下C#中的Attributes的用法,感觉很有用,现总结以下: 在前台用JS写的脚本方法,除了可以直接用在前台控件的属性中,还可以在后台运用. 即在后台页面加载时,调用JS方法.语法格式有 ...

  3. oracle中%type用法,oracle中declare用法

    第8 章 函数与存储过程 Oracle数据库中不仅可以使用单条语句对数据库进行 数据库中不仅可以使用单条语句对数据库进行 查操作,而且可以多条语句组成一个语句块, 增.删.改.查操作,而且可以多条语句 ...

  4. c语言中 队列用法,c中queue的用法

    下面小编就跟你们详细介绍下c中queue的用法的用法,希望对你们有用. c中queue的用法的用法如下: Model ----------------------------------------- ...

  5. vba中dir用法_VBA中DIR用法举例.doc

    VBA中DIR用法举例 VBA中DIR函数用法讲解 1.dir代码演示 先上一段代码,作用是获取某个文件夹下子文件夹和文件的名称 没加注释的代码 Sub 获取文件夹和文件名() Dim str1 As ...

  6. python中remove用法_python中remove的一些坑

    前几天,使用python时遇到这么一个需求,删除一个列表中值为1的元素.我寻思着使用remove方法,但是remove方法只会删除第一个,于是我使用for循环去删除.代码和运行结果如下: 当时这个结果 ...

  7. python中rjust用法_python中rjust的用法

    英文对话是要加引号的,而且是双引号和单引号的使用方法与中文一样的用法,接下来小编在这里给大家带来,我们一起来看看吧!引号分单引号(singlequotationmarks)和双引号(doublequo ...

  8. oracle中rollback用法,Oracle中SAVEPOINT和ROLLBACK用法

    savepoint是事务内部允许部分rollback的标志符.因为事务中对记录做了修改,我们可以在事务中创建savepoint来标识不同的点.如果遇到错误,就可以rollback到不同的点或直接回来事 ...

  9. oracle中using用法,Oracle中Using用法

    Oracle中Using用法 1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数 ...

最新文章

  1. UVa 1607 (二分) Gates
  2. Windows Mobile常用程序代码(串口、图象、网络、3D、数据库、音频视频等等)
  3. [转]史上最全的CSS hack方式一览
  4. C/C++——输入输出字符相关,cin.get()、getchar()和cin.getline()
  5. idea常用快捷键设置
  6. MyBatis四大核心概念
  7. wps怎么把ppt里的字体一起保存_PPT基础教程!看懂少走弯路!
  8. 2010年3月份第二周51aspx发布源码
  9. 薪资不如 Java、C,BAT 需求大,揭秘 Python 程序员跳槽现状!
  10. 动态规划之《寻找最大上升序列》
  11. CSS进阶(4)—— 温和padding中的诡异CSS现象
  12. 快速突破面试算法之二分查找篇
  13. Windows使用快捷键
  14. 2020计算机毕设选题推荐可视化方向,前端方向本科应届生有什么毕设选题推荐?...
  15. 解决mysql报错ERROR 2002 (HY000)
  16. 15W无线充电芯片介绍
  17. C语言100个囚犯和灯泡,一百个囚犯和一个灯泡
  18. 6-10 使用函数求余弦函数的近似值 (15 分)本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e:cos(x)=x0/0!−x2/2!+x4/4!−x6/
  19. 【强化学习论文合集】三十三.2021国际人工智能联合会议论文(IJCAI2021)
  20. Win flex-bison 的简单使用

热门文章

  1. 深入一点 让细节帮你和Fragment更熟络
  2. arm 添加 samb 文件共享
  3. 覆盖与隐藏的区别 (一个列子)
  4. HTML1.0 - html 环境搭建 开发工具
  5. 动态SQL实现批量删除指定数据库的全部进程
  6. SharePoint 2013版本功能对比介绍
  7. ASP.Net下使用ExtJS报“Ext未定义”错误的原因
  8. [转载] c++list遍历_List、Set、数据结构、Collections
  9. [转载] python中chr和str,以及ordint
  10. Spring源码解析之BeanFactory