answersLogoWhite

0

What is the meaning of for in c plus plus?

Updated: 8/20/2019
User Avatar

Wiki User

10y ago

Best Answer

The for statement introduces a loop, typically a counting loop. The for statement has three optional expressions separated by semi-colons: initial expression(s); conditional expression; and action expression(s). If any expressions are omitted, its semi-colon must still be present. Multiple initial or action expressions may be separated with commas while the conditional expression must be single boolean expression that evaluate zero or non-zero (false or true).

The syntax is as follows:

for ([initial_expr]; [conditional_expr]; [action])

{

statement;

}

Note that braces are optional when there is only one statement, but required for multi-line statements. Either way, the statement is known as the statement body.

The initial expression(s) is/are evaluated before entering the loop.

The conditional expression is evaluated before each iteration of the loop. If the evaluation is non-zero, execution passes to the statement body, otherwise execution passes to the statement that follows the satement body.

The action expression is evaluated after each iteration of the statement body, before the conditional expression is re-evaluated.

For loops are typically used for counting iterations:

for (int x=0; x<10; ++x)

{

std::cout<<x;

}

In the above example, the loop will iterate 10 times. When the loop is first encountered, x is instantiated and initialised to zero. The conditional expression evaluates true (0<10==true), thus the statement body executes. In this case, the statement body simply prints the value of x. So, after the first iteration, the output will be:

0

The action expression then increments x and the condition is re-evaluated. 1<10==true so the output is now:

01

x is incremented again and again until the output is:

0123456789

At this point, when x is incremented to 10, the conditional expression evaluates false (10<10==false) thus the loop terminates.

Note that since x was instantiated within the for statement, it is deemed local to the loop and will fall from scope when the loop terminates. If we wish to retain the value of x, we must declare x outside of the loop. In this case, the initial expression is not required (or we can use it to initialise another variable).

int x=0;

for (;x<10;++x)

{

std::cout<<x;

}

We can also use the statement body to execute the action expression:

int x=0;

for (;x<10;)

{

std::cout<<x++;

}

Note that we use the postfix increment operator in the statement body rather than the prefix increment operator. They both do the same job (incrementing x) but in this example we want the value of x BEFORE it is incremented, hence we use the postfix operator (fetch x, then increment).

Although we can use the action to execute multiple actions (separated by commas), using the statement body to perform the actions can often be simpler, especially if the actions are also conditional.

If we also elminate the conditional expression, we end up with an infinite loop:

int x=0;

for (;;)

{

std::cout<<x++;

}

This loop will never end because the statement body has no exit condition. However, we can still terminate the loop by placing the conditional expression in the statement body:

int x=0;

for (;;)

{

std::cout<<x++;

if (x<10)

continue;

else

break;

}

We can simplify this expression as:

int x=0;

for (;;)

{

std::cout<<x++;

if (!(x<10))

break;

}

Note that both continue and break mark the end of an iteration. While break terminates the loop, continue starts a new iteration, but evaluates the for loop's conditional expression (if present) first. By using the statement body to perform conditional expressions, we can create more complex or multiple expressions than might otherwise be possible with the conditional expression of the loop.

This makes the for loop extremely flexible. However, if the condition expression is the only portiobn of the for loop that is required, we can simplify things and make our code more readable by using a while loop instead:

int x=0;

while (x<10)

{

std::cout<<x++;

}

Note that while loops only have a conditional expression, which is non-optional.

Both for and while can be used interchangeably in many loop structures, however it's best to use the one that makes most sense in the type of loop you are constructing or that aids the readability of your code.

One type of loop the for and while loops cannot cater for is one where the loop must execute at least once. For instance, the following loop will fail to execute if x is already 10:

while (x<10)

{

std::cout<<x++;

}

If we wish to ensure the statement body executes at least once, we must use a do-while loop instead:

do

{

std::cout<<x++;

} while (x<10);

In this case, the conditional expression is evaluated after each iteration, not before.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the meaning of for in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What do the two plus stand for in C plus plus?

The ++ in C++ refers to the postfix increment operator (operator++()). It's literal meaning is "the successor to C", in reference to the C language upon which the C++ language is based.


How does the use of the 'void' keyword differ in C plus plus vs. C?

It doesn't. Void has the same meaning in both.


Main meaning of turbo c plus plus?

Name of a compiler (and IDE) from Borland.


What is the meaning of plus plus in c?

++a (plus plus a) is pre-incrementing operator to aa=10;printf("%d",++a); /* it will print 11 as ++a increment first a by 1 then prints it */printf("%d",a++); /*it will printf 10 as it is post _ increment operator , it prints the value a first then increment it by 1 */


How do you prove if a is less than b then a plus c is less than b plus c?

I believe that's usually treated as an axiom, meaning you don't prove it.


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


B plus b plus b plus c plus c plus c plus c equals?

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


Symplify c plus c plus c plus c?

4c


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

c + c + c + c + c = 5 * c.


Primary and secondary key in c and c plus plus?

There are no "primary and secondary keys" in c and c plus plus.


What is the meaning of star d in c plus plus programming?

If d is a pointer variable, then *d is the value stored in the memory address pointed to by d.