Loop


Loop

A situation can arise if you have executed a section of code multiple times. In general, statements are performed sequentially: First, followed by second, are performed in one function.

Programming languages give multiple control structures to make execution routes more sophisticated.

The following loops are provided in the C++ programming language for handling loop requirements.

Sr.No Loop Type & Description
1 while loop
Repeats some statements while a given condition proves to be true. It checks the condition before the execution of the body of the loop
2 for loop
Execute some statements many times and abbreviates the code for the loop variables’ management
3 do...while loop
It is similar to a ‘while’ statement, just that it checks the condition after the loop body’s execution
4 nested loops
You get to use some loops inside other ‘for,’ ‘while,’ or ‘do..while,’ loop.

while loop

Repeats some statements while a given condition proves to be true. It checks the condition before the execution of the body of the loop

Syntax

while (test_expression) { // statements update_expression; }

Example while loop:

#include <iostream> using namespace std; int main() { // Declaring and assign the value variables i int i; // only executed if while is true for (i < 5) { cout <<"\n\t Welcome to www.Knowledge2life.com "; i++ } return 0; }

OUTPUT:

loop

for loop

Execute some statements many times and abbreviates the code for the loop variables’ management

Syntax

for (statement 1; statement 2; statement 3) { // code block to be executed }

Example for loop:

#include <iostream> using namespace std; int main() { // Declaring variables i int i; for (int i = 0; i < 5; i++) { // only executed if statement 2; is true cout <<"\n\t Welcome to www.Knowledge2life.com "; } return 0; }

OUTPUT:

loop

do...while loop

It is similar to a ‘while’ statement, just that it checks the condition after the loop body’s execution

Syntax

do { statements } while (expression)

Example:

#include <iostream> using namespace std; int main() { // Declaring and assign the value variables i int i=0; // only executed if while is true do { cout <<"\n\t Welcome to www.Knowledge2life.com "; i++; } while (i<5); return 0; }

OUTPUT:

data type

nested loops

You get to use some loops inside other ‘for,’ ‘while,’ or ‘do..while,’ loop.

Syntax

loop_1(condition) { loop_2(condition) { // inner loop statements. } // outer loop statements. }

Loop Control Statements

Closed control statements vary its typical sequence execution. All automatic objects produced within that scope are deleted when execution exits an area.

C++ supports the following statements of control.

Sr.No Control Statement & Description
1 break statement
Termination of the loop or decision statement and then transferring the statement’s execution immediately following the decision statement or the loop.
2 continue statement
It causes skipping the loop for the remaining body and then instantly retest its condition before reiterating
3 goto statement
Transferring the control to labeled statement. Even though it isn’t advised using the goto statement in a program

Example break statement & continue statement:

#include <iostream> using namespace std; int main() { int i = 1; while (i < 10) { if (i == 4) { i++; continue; } if (i == 8) { break; } cout << i << "\n"; i++; } return 0; }

OUTPUT:

data type