首页 > 社交 > 科普中国

C++的四类循环:Entry

常驻编辑 科普中国 2022-08-15 循环体   可读性   数组   指针   抽象   容器   语法   条件   代码   测试

In programming, sometimes there is a need to perform some operation more than once or (say) n number of times. Loops come into use when we need to repeatedly execute a block of statements.m5K拜客生活常识网

在编程中,有时需要多次执行某些操作,例如n次。当我们需要重复执行一个语句块时,就会使用循环。m5K拜客生活常识网

4 types of loops:m5K拜客生活常识网

① Entry Controlled loops: while loop, for loopm5K拜客生活常识网

② Exit Controlled Loops:m5K拜客生活常识网

③ Range-based for loopm5K拜客生活常识网

④ For_each loopm5K拜客生活常识网

可以理解后两种循环是前两种循环的语法糖,编程语法制定语法规则,确定如何抽象,编程语言的编译器实现抽象的编译,程序员按规则写代码。m5K拜客生活常识网

1 Entry Controlled loops

In this type of loop, the test condition is tested before entering the loop body. For Loop and While Loop is entry-controlled loops.m5K拜客生活常识网

在这种类型的循环中,在进入循环体之前测试测试条件。For循环和While循环是入口控制循环。m5K拜客生活常识网

1.1 for loopm5K拜客生活常识网

#include  int main(){    int i=0;         for (i = 1; i <= 10; i++)    {        printf( "Hello World
");       }     return 0;}

1.2 while loopm5K拜客生活常识网

#include  int main(){    // initialization expression    int i = 1;    // test expression    while (i < 6)    {        printf( "Hello World
");           // update expression        i++;    }     return 0;}

2 Exit Controlled Loops:

In this type of loop the test condition is tested or evaluated at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. the do-while loop is exit controlled loop.m5K拜客生活常识网

在这种类型的循环中,在循环体的末端测试或评估测试条件。因此,无论测试条件是真还是假,循环体将至少执行一次。do while循环是出口控制循环。m5K拜客生活常识网

#include  int main(){    int i = 2; // Initialization expression     do    {        // loop body        printf( "Hello World
");            // update expression        i++;     }  while (i < 1);   // test expression     return 0;}

3 Range-based for loop

Range-based for loop in C++ is added since C++ 11. It executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.m5K拜客生活常识网

C++中基于范围的for循环是从C++11开始添加的。它在一个范围内执行for循环。用作在一系列值(例如容器中的所有元素)上进行操作的传统for循环的可读性更强的等价物。m5K拜客生活常识网

syntax:m5K拜客生活常识网

for ( range_declaration : range_expression )     loop_statementParameters :range_declaration : a declaration of a named variable, whose type is the type of the element of the sequence represented by range_expression, or a reference to that type.Often uses the auto specifier for automatic type deduction.range_expression : any expression that represents a suitable sequence or a braced-init-list.loop_statement : any statement, typically a compound statement, whichis the body of the loop.

code demo:m5K拜客生活常识网

#include #include #include int main() {    // Iterating over whole array    std::vector v = {0, 1, 2, 3, 4, 5};    for (auto i : v)        std::cout << i << ' ';          std::cout << '
';          // the initializer may be a braced-init-list    for (int n : {0, 1, 2, 3, 4, 5})        std::cout     

相关阅读:

  • 宋圭武猜想(51)宇宙没有起源也没有结束
  • 如何输入数组(如何将数字输入数组)
  • 指针怎么用 c语言指针与一维数组?
  • java
  • 集合篇
  • GO学习
  • 技巧篇:常用的vba代码汇总
  • JavaScript中的数据类型判断
  • 「Leetcode刷题」「33」搜索旋转排序数组.py
  • 「java8」阿里架构师:Stream对集合的处理方式你全都知
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。