answersLogoWhite

0

What are c plus plus loops?

Updated: 8/21/2019
User Avatar

Wiki User

10y ago

Best Answer

C++ has three structured loops and one procedural loop. The three structured loops are the for loop, the whileloop and the do-while loop. The procedural loop is the goto loop.

Which you use is largely down to which is the most logical for the particular loop you are trying to achieve, or that aids in the readability of your code. In general, however, you will use the for loop whenever a loop should be executed a known number of times (counting loops). The while and do-whileloops can also be used for counting but are best utilised with infinite loops. The only difference between the two is that a do-while loop always executes the loop body at least once, whereas a while loop may not execute at all.

The following examples demonstrate the different ways we can print the numbers from 1 to 10 using each type of loop:

for (int num=1; num<=10; ++num)

{

std::cout << num << std::endl;

}

------------

int num=1;

while (num<=10)

{

std::cout << num << std::endl;

++num;

}

------------

int num=1;

do

{

std::cout << num << std::endl;

++num;

} while (num<=10);

------------

int num=1;

again:

std::cout << num << std::endl;

++num;

if (num<=10) goto again;

Clearly the for loop makes the most sense with this type of loop.

We'll ignore the conditional loop as this is seldom used in real-world applications. It does have its uses within highly complex loops (aiding efficiency), but it can result in "spaghetti code" that is often difficult to read. Structured loops are designed to aid readability.

You will note that the body of the while and the do-while loops are exactly the same, the only difference being when the conditional expression (num<=10) is evaluated. With a while loop, the evaluation occurs before each loop begins. With a do-while loop, the evaluation occurs after each loop ends. So long as the evaluation is true, the loop will continue to iterate.

The for loop is actually the same as a while loop (evaluating the conditional expression before each loop begins), thus you will often have the choice of using one or the other. However, the for loop is the most flexible of the two as it allows us to initialise a control variable, supply a conditional expression, and provide a statement (either simple or compound) to be executed at the end of each iteration, all within one statement. This makes it clear from the outset what is expected of the loop and is therefore ideal for counting loops.

However, each of the expressions within a for loop is optional. Thus we can use for to create an indefinite loop:

for (;;)

{

// loop body

}

This is actually the same as writing:

for (; true;)

{

// loop body

}

We can do the same thing with while as follows:

while (true)

{

// loop body

}

Of the two, the while loop is the preferred method of initiating an indefinite loop as it aids in the readability.

With these types of loop, the loop body will simply iterate indefinitely. In other words, we create an infinite loop with no way out. In order to break out of the loop, the loop body must include at least one control statement in conjunction with a conditional expression.

Control statements are used to control the flow of execution from within the loop body. The break, continue, goto and return keywords. The continue keyword is the only control statement that does not end the loop, it simply starts a new iteration and is typically used when we wish to start a new loop before the end of the loop body. All others will terminate the loop.

Thus to count from 1 to 10 using an indefinite loop, we might use the following:

int num=1;

while (true)

{

std::cout << num << std::endl;

++num;

if (num>10)

break;

}

Again, a for loop would aid the readability of this type of loop.

You will also notice that with a for loop, we can both instantiate and initialise the control variable from within the for statement itself. This means that the control variable will fall from scope when the loop ends. In order to make the control variable accessible from outside of the loop body, we must instantiate it from outside of the loop body, just as we did with the while.

Sometimes you might have more than one statement that must be executed at the end of each loop. With a while loop, these statements must appear at the end of the loop itself. But with a for loop, we can include those statements within the for statement itself. For instance:

for (int x=1, y=1;x<=10; ++x, y*=2)

{

std::cout << "x=" << x << ", y=" << y << std::endl;

}

In the above example, we have a standard 1 to 10 counting loop using x as the control variable. But on each iteration we also double the value of y, which is initially 1. Thus we get the following output:

x=1, y=1

x=2, y=2

x=3, y=4

x=4, y=8

x=5, y=16

x=6, y=32

x=7, y=64

x=8, y=128

x=9, y=256

x=10, y=512

To achieve the same thing with a while loop, we'd need to do the following instead:

int x=1, y=1;

while (x<=10)

{

std::cout << "x=" << x << ", y=" << y << std::endl;

++x;

y*=2;

}

Arguably, the for loop is easier to understand as the while loop looks like a counting loop (which is generally easier to read as a for loop), but it's not clear what the purpose of y is until we look at the final line in the loop. Imagine how much more difficult it would be if the loop body were much larger. With the for loop, the entire purpose of the loop is contained within the for statement itself, thus aiding in the overall readability of the loop.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are c plus plus loops?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How does C plus plus endeavor to represent the loop paradigm?

Iterative loops in C/C++ are represented by for(), while() and do...while() code blocks. Recursive loops are represented by functions calling themselves.


How do you make two loops run at the same time in c plus plus?

With two threads.


C program to print numbers 1 to n?

how do we use loops in c plus plus programing and what are basic differences between do,for and while loop


C programs using loops?

Loops are very important part of a C-language. If we have to run our programe multiple time then we use Loops of C.


What is label in c plus plus?

Labels are used to mark the start of a code segment. They are often used in conjunction with goto statements, allowing procedural jumps and loops to be formed.


What are loops in C?

In very simple terms, Loops in any language are used to perform a task repetitively.


What are loops in c language?

In very simple terms, Loops in any language are used to perform a task repetitively.


Where do you get loop the loop straws?

C, for loops, while loops, and do while loops are control structures forFor example, the following programs are functionally identical: While loop


What loops does not need a counter?

Random example: while ((c= getchar()) != EOF) putchar (c);


What is a six letter word for phases or loops starting with c?

cycles


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c


What is c plus c plus 2c plus c plus c equal?

c + c + 2c + c + c = 6c