32 Some experiments Move the declaration of the loop control

3.2 Some experiments

Move the declaration of the loop control variable outside the loop.

Move the declaration and initialization of the loop control outside the loop

Switch the increment portion to use the post-increment operator

Switch the increment portion to use the pre-increment operator

Solution

Experiment one.

/* moving the declaration of the loop control variable outside the loop does not have any effect on the program */

#include \"iostream\"
using namespace std;
int main()
{
       int a;
       for (a = 10; a < 20; a = a +1)
       {
           cout << \"Value of a: \" << a << endl;
       }
       return 0;
}

/* sample output

*/

/* moving the declaration and initialization of the loop control variable outside the loop does not have any effect on the program */

#include \"iostream\"
using namespace std;
int main()
{
       int a=10;
       for (; a < 20; a = a +1)
       {
           cout << \"Value of a: \" << a << endl;
       }
       return 0;
}

/* sample output

*/

/* switching the increment portion to use the post-increment operator does not have any effect on the program, it will be the same as the first program. */

#include \"iostream\"
using namespace std;
int main()
{
       for (int a = 10; a < 20; a++)
       {
           cout << \"Value of a: \" << a << endl;
       }
       return 0;
}

/* sample output

*/

/* switching the increment portion to use the pre-increment operator does not have any effect on the program, it will be the same as the first program. the increment portion gets executed only after completion of the iteration the loop pre-increment will also be the same of the first program.*/

#include \"iostream\"
using namespace std;
int main()
{
       for (int a = 10; a < 20; ++a)
       {
           cout << \"Value of a: \" << a << endl;
       }
       return 0;
}

/* sample output

*/

3.2 Some experiments Move the declaration of the loop control variable outside the loop. Move the declaration and initialization of the loop control outside the
3.2 Some experiments Move the declaration of the loop control variable outside the loop. Move the declaration and initialization of the loop control outside the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site