search
HomeBackend DevelopmentC++In C++, 'for' and 'while' have different uses
In C++, 'for' and 'while' have different usesAug 28, 2023 pm 01:01 PM
while loopfor loopc language programming

In C++, for and while have different uses

Loops in programming are used to calculate a piece of code multiple times. Here, we will see the difference between two types of loops in the program, For loop and While loop.

For Loop

The For loop is a repetitive control loop that allows the user to loop through a given block of code a specific number of times.

Syntax

for(initisation; condition; update){
   …code to be repeated
}

While loop

While loop is an entry control loop that allows the user to repeatedly execute a given statement until a given condition is true.

Grammar

while(condition){
   …code to be repeated
}

The difference between For loop and While loop

  • For loop is a controlled loop, while while loop is a conditional loop

  • Control loop.

  • The conditional statement of the for loop allows the user to add an update statement in it, while in the while condition there is only control The expression can be written as.

  • In a for loop, the test condition is usually an integer comparison, whereas in a while loop, the test condition can be any other expression that evaluates to a Boolean value.

  • Case in which two loops in the code can provide different solutions

    One situation is when the loop body contains a In while loop, continue statement before update statement, but in for loop The update statement already exists in initialization.

    Example

    Procedural example to illustrate how our solution works: (for loop)

    #include<iostream>
    using namespace std;
    
    int main(){
    
       cout<<"Displaying for loop working with continue statement\n";
       for(int i = 0; i < 5; i++){
          if(i == 3)
          continue;
          cout<<"loop count "<<i<<endl;
       }
       return 0;
    }

    Output

    Displaying for loop working with continue statement
    loop count 0
    loop count 1
    loop count 2
    loop count 4

    Example

    Program to demonstrate how our solution works: (while loop)

    #include<iostream>
    using namespace std;
    
    int main(){
    
       cout<<"Displaying for loop working with continue statement";
       int i = 0;
       while(i < 5){
          if(i == 3)
          continue;
          cout<<"loop count "<<i<<endl;
          i++;
       }
       return 0;
    }

    Output

    Displaying for loop working with continue statementloop count 0
    loop count 1
    loop count 2

    The above is the detailed content of In C++, 'for' and 'while' have different uses. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
    怎么用php实现求100以内的奇数怎么用php实现求100以内的奇数Dec 23, 2022 pm 06:54 PM

    实现步骤:1、使用for语句控制范围来遍历1~100的数字,语法“for ($i = 1; $i <= 100; $i++) {循环体代码}”;2、在循环体中,利用if语句和“%”运算符获取并输出奇数即可,语法“if($i % 2 != 0){echo $i." ";}”。

    PHP中如何利用while循环语句实现字符串拼接PHP中如何利用while循环语句实现字符串拼接Mar 07, 2024 pm 02:15 PM

    标题:PHP中利用while循环实现字符串拼接在PHP语言中,利用while循环语句实现字符串拼接是一种常见的操作。通过循环遍历数组、列表或者其他数据源,将每个元素或者值依次拼接到一个字符串中。这种方法在处理大量数据或者需要动态生成字符串的情况下非常有用。下面我们来看一些具体的代码示例。首先,我们准备一个数组作为数据源,然后使用while循环来实现字符串拼接

    在C语言环境下如何对中文字符进行排序?在C语言环境下如何对中文字符进行排序?Feb 18, 2024 pm 02:10 PM

    如何在C语言编程软件中实现中文字符排序功能?在现代社会,中文字符排序功能在很多软件中都是必不可少的功能之一。无论是在文字处理软件、搜索引擎还是数据库系统中,都需要对中文字符进行排序,以便更好地展示和处理中文文本数据。而在C语言编程中,如何实现中文字符排序功能呢?下面将简要介绍一种方法。首先,为了在C语言中实现中文字符排序功能,我们需要使用到字符串比较函数。然

    JS循环学习:while循环语句的使用(示例详解)JS循环学习:while循环语句的使用(示例详解)Aug 03, 2022 pm 06:04 PM

    循环的目的就是为了反复执某段代码,使用循环可以减轻编程压力,避免代码冗余,提高开发效率,方便后期维护。while 循环是 JavaScript 中提供的最简单的循环语句,下面我们来了解一下 while循环和do-while循环的使用。

    如何在C++中管理完整的循环队列事件?如何在C++中管理完整的循环队列事件?Sep 04, 2023 pm 06:41 PM

    介绍CircularQueue是对线性队列的改进,它被引入来解决线性队列中的内存浪费问题。循环队列使用FIFO原则来插入和删除其中的元素。在本教程中,我们将讨论循环队列的操作以及如何管理它。什么是循环队列?循环队列是数据结构中的另一种队列,其前端和后端相互连接。它也被称为循环缓冲区。它的操作与线性队列类似,那么为什么我们需要在数据结构中引入一个新的队列呢?使用线性队列时,当队列达到其最大限制时,尾指针之前可能会存在一些内存空间。这会导致内存损失,而良好的算法应该能够充分利用资源。为了解决内存浪费

    PHP中for循环的执行顺序是什么PHP中for循环的执行顺序是什么Sep 22, 2021 pm 06:24 PM

    执行顺序:1、执行“初始化表达式”;2、执行“条件判断表达式”,如果表达式的值为真,则执行“循环体”,否则结束循环;3、执行完循环体后,执行“变量更新表达式”;4、变量更新后,进入下一次循环,直到条件判断值为假,结束循环。

    在C语言中编写一个打印右箭头和左箭头图案的程序在C语言中编写一个打印右箭头和左箭头图案的程序Aug 28, 2023 pm 01:38 PM

    程序说明打印左右箭头图案算法接受打印左右箭头图案的行数.PrintUpperPartoftheArrowwithStarsPatternsPrintInvertedRightTrianglewithStarsPatternsPrintBottomPartoftheArrowwithStarsPatternsPrinttheRightTrianglewithStarsPatterns示例/*ProgramtoprinttheLeftandrightarrowpattern*/#include<

    在C语言中编写一个打印镜像空心平行四边形的程序在C语言中编写一个打印镜像空心平行四边形的程序Aug 30, 2023 pm 06:29 PM

    程序描述这是一个四边形,其中两对对边是平行的。有六个重要的平行四边形属性需要了解对边相等(AB=DC)。对角线相等(D=B)。相邻角互补(A+D=180°)。如果一个角是直角,则所有角都是直角。平行四边形的对角线互相平分。平行四边形的每条对角线将其分成两个相等的部分。算法从用户那里接受行数和列数。将其存储在rows和cols变量中。为了迭代行,运行一个外部循环,循环结构应该是for(r=1;r

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor