What tasks can be performed in the three sections of a for loop in addition to initializing a single variable, testing the variable, and incrementing it?
What will be an ideal response?
Although the three sections of the for loop are most commonly used for initializing a variable, testing it, and incrementing it, you can also perform other tasks:
* You can initialize more than one variable by placing commas between the separate statements, as in the following:
for(g = 0, h = 1; g < 6; ++g)
* You can declare a new variable, as in the following:
for(int k = 0; k < 5; ++k)
This technique is used frequently when the variable exists only to control the loop and for no other purpose. When a variable is declared inside a loop, as k is in this example, it can be referenced only for the duration of the loop body; then it is out of scope.
* You can perform more than one test by evaluating compound conditions, as in the following:
for(g = 0; g < 3 && h > 1; ++g)
* You can decrement or perform some other task at the end of the loop's execution, as in:
for(g = 5; g >= 1; --g)
* You can perform multiple tasks at the end of the loop's execution by separating the actions with commas, as in the following:
for(g = 0; g < 5; ++g, ++h)
* You can leave one or more portions of the for expression empty, although the two semicolons are still required as placeholders to separate the three sections.
You might also like to view...
The command ________ executes a Java application.
a. run b. javac c. java d. None of the above
The ________ IP addressing scheme will provide more available IP addresses by using 8 groups of 16-bit numbers
Fill in the blank(s) with correct word
Every item stored within a hash is assigned both a key as well as a unique index position.
Answer the following statement true (T) or false (F)
How can process patterns assist a development team build software products efficiently?
What will be an ideal response?