Banner Image

C++

C++ Loops: Types and Examples

Written by Rahul Lath

Updated on: 12 Aug 2024

tutor Pic

Introduction 

Loops in C++ are used to execute a block of code repeatedly until a certain condition is met. They are an essential part of programming and are used to automate repetitive tasks.

C++ Loops

In C++, loops are used to execute a block of code repeatedly. There are two types of loops: entry-controlled loops and exit-controlled loops.

Entry-controlled loops are loops where the condition is checked before the loop body is executed. If the condition is false, the loop body is not executed at all. The for loop is an example of an entry-controlled loop.

Exit-controlled loops are loops where the condition is checked after the loop body is executed. If the condition is false, the loop exits. The while loop and do-while loop are examples of exit-controlled loops.

For Loop

The for loop is an entry-controlled loop that is used to execute a block of code repeatedly for a fixed number of times. The syntax of a for loop is:

Here’s a brief explanation of each part of the for loop:

initialization: This is where you initialize the loop variable.

condition: This is the condition that is checked before each iteration of the loop. If the condition is true, the loop body is executed. If the condition is false, the loop exits.

update: This is where you update the loop variable after each iteration.

In this example, the for loop initializes the loop variable i to 1, checks if i is less than or equal to 5, executes the loop body (which prints the value of i to the console), and then updates the value of i by adding 1 to it. This process is repeated until i is no longer less than or equal to 5.

While Loop

The while loop is an exit-controlled loop that is used to execute a block of code repeatedly as long as a certain condition is true. The syntax of a while loop is:

Here’s a brief explanation of the while loop:

condition: This is the condition that is checked before each iteration of the loop. If the condition is true, the loop body is executed. If the condition is false, the loop exits.

Here’s an example of a while loop that prints the numbers from 1 to 5:

Do-while

A do-while loop is a type of loop in programming languages that executes a block of code repeatedly until a certain condition is met. The syntax of a do-while loop in C++ is as follows:

In this syntax, the code block is executed at least once, regardless of whether the condition is true or false. After each iteration, the condition is checked, and if it is true, the loop continues to execute. If the condition is false, the loop terminates.

Here’s an example of a do-while loop that prints the numbers from 1 to 5:


FAQs

What are nested loops?

Nested loops are loops that are placed inside other loops. They are used to perform repetitive tasks that require multiple iterations.

What are the best loops in C++?

The best loop to use in C++ depends on the specific situation. For example, a for loop is often used when the number of iterations is known in advance, while a while loop is used when the number of iterations is not known in advance. A do-while loop is used when the code block needs to be executed at least once.

What does i++ mean in C++?

i++ is a shorthand notation for incrementing the value of a variable by 1. It is equivalent to i = i + 1.

What is the Infinite Loop?

An infinite loop is a loop that never terminates because the condition is always true. This can cause the program to become unresponsive and crash. To terminate an infinite loop, you can use a break statement or modify the condition so that it eventually becomes false.

Written by by

Rahul Lath

Reviewed by by

Arpit Rankwar

Share article on

tutor Pic
tutor Pic