Regular “for” loop: The “for” loop is also referred as count-controlled loop and it contains three processes as follows: Initialization Compare Update It is possible to initialize more than one variable in loop expression. In some cases it is necessary to track number of times the loop gets executed; so C++ provides “for” loop. Syntax: The syntax for the “for” loop is as follows: //for loop condition for (initialization; compare; update) { //statement } In the above format, The “initialization” represents the initialization variable for the loop. The “compare” represents the comparison condition which is used to exit from the loop when the condition becomes failure. The “update” represents the counter variable to count each iteration in the loop. The “statement” represents the statements which are needs to be executed during the loop iteration. Range based for loop: The range based “for” loop is the one which iterates each element once from the array. On iteration, the loop copies element from the array to variable each time. For example, consider an array consists of six elements; so, the loop will iterate for 6 times to retrieve each element from the array. Syntax: The syntax for the range based “for” loop is as follows: for(datatype rangeVariable: array) Statement; In the above format, The “datatype” is the data type followed by the range variable; the range variable uses the same data type as the array follows or else it follows a type to which the element of the array can be automatically converted. The “rangeVariable” is the name which receives the value from array for each loop iteration; this variable changes the value on each loop iteration like first, second and so on. The “array” represents the name of the array that the loop needs to operate; from this array, the loop iterates once for every element. The “statement” represents the statement that needs to be executed during the loop iteration. Algorithm : Start the program Declare the variable “name” as “int” and assign the size of the variable “name” with 20 elements. Then, write the regular “for” loop and range based for loop to print each element of the array.
Regular “for” loop: The “for” loop is also referred as count-controlled loop and it contains three processes as follows: Initialization Compare Update It is possible to initialize more than one variable in loop expression. In some cases it is necessary to track number of times the loop gets executed; so C++ provides “for” loop. Syntax: The syntax for the “for” loop is as follows: //for loop condition for (initialization; compare; update) { //statement } In the above format, The “initialization” represents the initialization variable for the loop. The “compare” represents the comparison condition which is used to exit from the loop when the condition becomes failure. The “update” represents the counter variable to count each iteration in the loop. The “statement” represents the statements which are needs to be executed during the loop iteration. Range based for loop: The range based “for” loop is the one which iterates each element once from the array. On iteration, the loop copies element from the array to variable each time. For example, consider an array consists of six elements; so, the loop will iterate for 6 times to retrieve each element from the array. Syntax: The syntax for the range based “for” loop is as follows: for(datatype rangeVariable: array) Statement; In the above format, The “datatype” is the data type followed by the range variable; the range variable uses the same data type as the array follows or else it follows a type to which the element of the array can be automatically converted. The “rangeVariable” is the name which receives the value from array for each loop iteration; this variable changes the value on each loop iteration like first, second and so on. The “array” represents the name of the array that the loop needs to operate; from this array, the loop iterates once for every element. The “statement” represents the statement that needs to be executed during the loop iteration. Algorithm : Start the program Declare the variable “name” as “int” and assign the size of the variable “name” with 20 elements. Then, write the regular “for” loop and range based for loop to print each element of the array.
Process or set of rules that allow for the solving of specific, well-defined computational problems through a specific series of commands. This topic is fundamental in computer science, especially with regard to artificial intelligence, databases, graphics, networking, operating systems, and security.
Chapter 7, Problem 41RQE
Program Plan Intro
Regular “for” loop:
The “for” loop is also referred as count-controlled loop and it contains three processes as follows:
Initialization
Compare
Update
It is possible to initialize more than one variable in loop expression. In some cases it is necessary to track number of times the loop gets executed; so C++ provides “for” loop.
Syntax:
The syntax for the “for” loop is as follows:
//for loop condition
for (initialization; compare; update)
{
//statement
}
In the above format,
The “initialization” represents the initialization variable for the loop.
The “compare” represents the comparison condition which is used to exit from the loop when the condition becomes failure.
The “update” represents the counter variable to count each iteration in the loop.
The “statement” represents the statements which are needs to be executed during the loop iteration.
Range based for loop:
The range based “for” loop is the one which iterates each element once from the array. On iteration, the loop copies element from the array to variable each time.
For example, consider an array consists of six elements; so, the loop will iterate for 6 times to retrieve each element from the array.
Syntax:
The syntax for the range based “for” loop is as follows:
for(datatype rangeVariable: array)
Statement;
In the above format,
The “datatype” is the data type followed by the range variable; the range variable uses the same data type as the array follows or else it follows a type to which the element of the array can be automatically converted.
The “rangeVariable” is the name which receives the value from array for each loop iteration; this variable changes the value on each loop iteration like first, second and so on.
The “array” represents the name of the array that the loop needs to operate; from this array, the loop iterates once for every element.
The “statement” represents the statement that needs to be executed during the loop iteration.
Algorithm:
Start the program
Declare the variable “name” as “int” and assign the size of the variable “name” with 20 elements.
Then, write the regular “for” loop and range based for loop to print each element of the array.