What are some unconventional uses of the three sections inside the parentheses of a for loop? Show at least three examples using code.

What will be an ideal response?


Initialization of more than one variable int the first section by placing commas between the separate statements, as in the following:for(g = 0, h = 1; g < 6; ++g)Performance of more than one test using compound conditions in the second section, as in the following:for(g = 0; g < 3 && h > 1; ++g)Decrementation or performance of some other task in the third section, as in the following:for(g = 5; g >= 1; --g)Performing multiple actions in the third section, separated by commas, as in the following:for(g = 0; g < 10; ++g, ++h, sum += g)You can leave one or more portions of a for loop empty, although the two semicolons are still required as placeholders. For example, if x has been initialized in a previous program statement, you might write the following:for(; x < 10; ++x)Declare a variable within a for statement, as in the following:
for(int val = 1; val < 11; ++val)
You might use method calls in any section of the for statement, as in the following example. Here, the isFinished() method would be required to return a Boolean value and the alter() method would be required to return a data type accepted by x.for(x = initMethod(); isFinished(); x = alter(x))

Computer Science & Information Technology

You might also like to view...

Hyperlinks can be assigned to any object

Indicate whether the statement is true or false

Computer Science & Information Technology

Which of the following is not a command used in the hostname configuration of a JURT?

a. set host-name Juniper b. commit host-name c. configure d. edit system

Computer Science & Information Technology

When using the window.confirm() method, if the user hits the Cancel button, ____.

A. a loop will execute B. a value of true is returned C. the dialog box remains open D. a value of false is returned

Computer Science & Information Technology

How many disks are needed to implement a RAID 1 + 0?

A. 2 B. 3 C. 4 D. 5

Computer Science & Information Technology