WEB DEVELOPER SITE
PYTHONPHP7JAVA
 

C++ LOOPS


In this tutorial you learn about c++ loops

The loops are expected to execute a square of code a significant number of the occasions. In this, fundamentally the announcements are executed successively which implies the main proclamation in a capacity is executed first than the second explanation, etc. The loops are utilized to execute a solitary explanation or the gathering of articulations on different occasions.


Loops

A loop can define as repeatedly executes a set of statements until a particular condition is satisfied.

A wihle statements define as repeatedly executes a target statement as long as a given condition remains true.

while (condition) {
   statement(s);
}
 

The while loop

The while loop statement repeats a target statement as long as the given condition is true.

syntax

C++ syntax for while loops in C++:

while(condition)
{
    Statement(s);
}

Here, statement(s) can be a single statement or a block of code consisting of several statements. condition can be any expression that is true when any non-zero value is present. The loop is executed when the condition is true.

When the condition is false, the program flow will continue to execute the next statement immediately following the loop.

we know that while loop statements {} curly braces. The loop body is the block of statements within curly braces.
The example above declares a variable equal to 2 (int num = 2). The while loop checks the condition (num < 7), and executes the statements in its body, which increment the value of num by one each time the loop runs.

The increment value can be changed. If changed, the number of times the loop is run will change, as well.

int num = 1;
while (num < 6) {
  cout < "Number: " < num < endl;
  num = num + 3;
}

/* Outputs
Number: 1
Number: 4 
*/



The For Loop statements

syntax

The syntax for the for loop in C++:

for ( init; condition; increment )
{
   Statement(s);
}

The following is the control flow for the for loop:

  1. init will be executed first and will only be executed once. This step allows you to declare and initialize any loop control variables. You can also write no statements here, as long as a semicolon appears.
  2. Next, I will judge condition. If true, the loop body is executed. If false, the loop body is not executed and the control flow jumps to the next statement immediately following the for loop.
  3. After executing the for loop body, the control flow jumps back to the increment statement above. This statement allows you to update the loop control variables. This statement can be left blank as long as a semicolon appears after the condition.
  4. The condition is judged again. If true, the loop is executed, and the process repeats continuously (looping the body, then incrementing the step value, and then re-judge the condition). The for loop terminates when the condition becomes false.
Note
init step is executed first, and does not repeat.

condition is evaluated, and the body of the loop is executed if the condition is true.

Example
for (int x = 2; x < 8; x++) {
 // some code
 }

The for Loop

Here the simple example of for loop
int main()
{
    for (int a = 2; a < 8; a++) {
        cout << a << endl;
    }

    return 0;
}
//output

2 3 4 5 6 7

C++ do...while loop

Unlike the for and while loops, they test loop conditions in the loop header. The do...while loop is the condition that checks its tail at the end of the loop.

The

do...while loop is similar to a while loop, but the do...while loop ensures that the loop is executed at least once.

syntax

C++ syntax for do...while loops:

do
{
   Statement(s);

}while( condition );

Note that the conditional expression appears at the end of the loop, so the statement(s) in the loop will be executed at least once before the condition is tested.

If the condition is true, the control flow will jump back to the above do and then re-execute the statement(s) in the loop. This process is repeated until the given condition becomes false.

example

#include <iostream>
using namespace std;
 
int main ()
{
   // Local variable declaration
   int a = 10;

   // do Loop execution
   do
   {
       cout << "a Value´╝Ü" << a << endl;
       a = a + 1;
   }while( a < 20 );
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

a Value: 10
a value: 11
a value: 12
a value: 13
a value: 14
a value: 15
a value: 16
a value: 17
a value: 18
a value: 19

C++ nested loop

You can nest another loop within a loop. C++ allows at least 256 nesting levels.

syntax

The syntax of the nested for loop statement in C++:

for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); // can place more statements }

C++ syntax for nesting while loop statements:

while( Condition) { while(condition< Span class="hl-brackets">) { statement(s< Span class="hl-brackets">); } statement(s< Span class="hl-brackets">); // can place more statements }

C++ syntax for nested do...while loop statements:

do { statement(s< Span class="hl-brackets">); // can place more statements do { statement(s< Span class="hl-brackets">); }while(< Span class="hl-code"> condition ); }while(< Span class="hl-code"> condition );

It's worth noting about nested loops, you can nest any other type of loop inside any type of loop. For example, a for loop can be nested inside a while loop, and vice versa.

example

The following program uses a nested for loop to find prime numbers from 2 to 100:

#include <iostream> using namespace std; int main () { int i, j; for(i=2; i<100; i++) { for(j=2; j <= (i/j); j++) { if(!(i%j)) { break; // If found, it is not a prime number } } if(j > (i/j)) { cout << i << " Is a prime number\n"; } } return 0; }

When the above code is compiled and executed, it produces the following results:

2 Is a prime number
3 Is a prime number
5 Is a prime number
7 Is a prime number
11 Is a prime number
13 Is a prime number
17 Is a prime number
19 Is a prime number
23 Is a prime number
29 Is a prime number
31 Is a prime number
37 Is a prime number
41 Is a prime number
43 Is a prime number
47 Is a prime number
53 Is a prime number
59 Is a prime number
61 Is a prime number
67 Is a prime number
71 Is a prime number
73 Is a prime number
79 Is a prime number
83 Is a prime number
89 Is a prime number
97 Is a prime number

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

C++ supports the following control statements.

Sr.No Control Statement & Description
1 break statement

Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

2 continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

3 goto statement

Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.

Infinite Loop

If the condition is never false, the loop will become an infinite loop. The for loop can be used to implement an infinite loop in the traditional sense. Since none of the three expressions that make up the loop are required, you can leave some of the conditional expressions blank to form an infinite loop.

example

#include <iostream> using namespace std; int main () { for( ; ; ) { printf("This loop will run forever.\n"); } return 0; }

When a conditional expression does not exist, it is assumed to be true. You can also set an initial value and an incremental expression, but in general, C++ programmers prefer to use the for(;;) structure to represent an infinite loop.

Note: You can terminate an infinite loop by pressing Ctrl + C.