C ++循环-while,for和do while循环

In any programming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

在任何编程语言中,循环均用于重复执行一组语句,直到满足特定条件为止。

这个怎么运作 (How it works)

loopflow diagram in C++

A sequence of statement is executed until a specified condition is true. This sequence of statement to be executed is kept inside the curly braces { } known as loop body. After every execution of loop body, condition is checked, and if it is found to be true the loop body is executed again. When condition check comes out to be false, the loop body will not be executed.

执行语句序列,直到指定条件为真为止。 该要执行的语句序列保留在称为循环主体的花括号{ } 。 每次循环体执行后,都会检查条件,如果发现条件为 ,则再次执行循环体。 当条件检查结果为false时 ,将不执行循环体。

C ++语言中有3种循环类型 (There are 3 type of loops in C++ language)

  1. while loop

    while循环

  2. for loop

    for循环

  3. do-while loop

    do-while循环

while循环 (while loop)

while loop can be address as an entry control loop. It is completed in 3 steps.

while循环可以作为入口控制循环来寻址。 分3步完成。

  • Variable initialization.(e.g int x=0;)

    变量初始化(例如int x=0; )

  • condition(e.g while( x<=10))

    条件(例如while( x<=10) )

  • Variable increment or decrement (x++ or x-- or x=x+2)

    可变增量或减量( x++x--x=x+2 )

Syntax:

句法:

variable initialization;
while (condition)
{
    statements;
    variable increment or decrement; 
}

for循环 (for loop)

for loop is used to execute a set of statement repeatedly until a particular condition is satisfied. we can say it an open ended loop. General format is,

for循环用于重复执行一组语句,直到满足特定条件为止。 我们可以说这是一个开放式循环。 通用格式是

for(initialization; condition; increment/decrement)
{
    statement-block;
}

In for loop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.

for循环中,我们恰好有两个分号,一个在初始化后,另一个在条件后。 在此循环中,我们可以使用逗号运算符分隔多个初始化或增量/减量。 for循环只能有一个条件

嵌套for循环 (Nested for loop)

We can also have nested for loop, i.e one for loop inside another for loop. Basic syntax is,

我们还可以嵌套for循环,即一个for循环在另一个for循环内。 基本语法是

for(initialization; condition; increment/decrement)
{
    for(initialization; condition; increment/decrement)
    {
        statement;
    }
}

do...while循环 (do...while loop)

In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. General format of do-while loop is,

在某些情况下,有必要在测试条件之前执行循环主体。 这种情况可以在do-while循环的帮助下进行处理。 do语句首先评估循环的主体,最后使用while语句检查条件。 do-while循环的一般格式是

do
{
    // a couple of statements
}
while(condition);

跳出循环 (Jumping out of a loop)

Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becocmes true, that is jump out of loop. C language allows jumping from one statement to another within a loop as well as jumping out of the loop.

有时,在执行循环时,有必要跳过一部分循环或在某些条件变为真时即退出循环而退出循环。 C语言允许在循环中从一个语句跳转到另一个语句,也可以跳出循环。

1) break声明 (1) break statement)

When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

当在循环内遇到break语句时,循环将立即退出,程序将在循环后立即继续执行该语句。

2) continue声明 (2) continue statement)

It causes the control to go directly to the test-condition and then continue the loop process. On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.

它使控制直接进入测试条件,然后继续循环过程。 遇到继续时,光标离开当前循环循环,并从下一个循环开始。

翻译自: https://www.studytonight.com/cpp/loops-in-cpp

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值