Question

In: Computer Science

What are one advantage and one disadvantage of allowing the modification of loop variables in the...

What are one advantage and one disadvantage of allowing the modification of loop variables in the loop body?

Solutions

Expert Solution

Advantage of allowing the modification of loop variables in the loop body:

To control the infinitely execution of certain loop statements.

Example:

#include <iostream>

using namespace std;



int main() {

int count=1; // Initially count is 1

while(count<=10) // Loop runs until count lessthan or equal to 10

{

cout<<count<<" "; // print count

count=count+1; // increment count everytime by 1

}

}

Output:

  • In the above program count = 11 while(11<10) it successfully terminate the program.
  • In this the program runs for finite number of times.

Dis advantage of allowing the modification of loop variables in the loop body:

Execution of certain loop statements infinitely.

Example:

#include <iostream>

using namespace std;



int main() {

int count=1; // Initially count is 1

while(count!=-1) // Loop runs until count lessthan or equal to 10

{

count=count*2; // multiply count everytime by 2

cout<<"The value of count ";

if(count%2==1) // check count is odd

{

cout<<"is odd"<<endl;

break;

}

}

}

Output:

Since the values of numbers after the count=count*2; statement are 2,4,6,8,......2n all are even numbers.

The break statement execute when the number is odd, so break statement not execute at all.

If we do not modify loop variables count in the loop body then it runs for 1 time.

Program and its output are given below

#include <iostream>

using namespace std;



int main() {

int count=1; // Initially count is 1

while(count!=-1) // Loop runs until count lessthan or equal to 10

{

count=count*2; // multiply count everytime by 2

cout<<"The value of count ";

if(count%2==1) // check count is odd

{

cout<<"is odd"<<endl;

break;

}

}

}

Output:


Related Solutions

What are three general approaches to obtaining an expert system? Give one advantage and one disadvantage...
What are three general approaches to obtaining an expert system? Give one advantage and one disadvantage for each approach.
what the advantage and disadvantage of cybersecurity in the ministry of health ?
what the advantage and disadvantage of cybersecurity in the ministry of health ?
What is a single-use bioreactor, what is it advantage and disadvantage?
What is a single-use bioreactor, what is it advantage and disadvantage?
Name one advantage of associating user threads with kernel thread? Suggest an advantage and disadvantage of...
Name one advantage of associating user threads with kernel thread? Suggest an advantage and disadvantage of using mailboxes over shared memory for inter-process communication.
One advantage and one disadvantage of minimum wage with respect to poverty and inequality, and give...
One advantage and one disadvantage of minimum wage with respect to poverty and inequality, and give one example of each one?
describe one advantage and one disadvantage of having PLCs. Would you want to be on a...
describe one advantage and one disadvantage of having PLCs. Would you want to be on a school site that uses PLCs to analyze student data? Explain.
Give one advantage and one disadvantage of using a plasma excitation source (as oppaosed to a...
Give one advantage and one disadvantage of using a plasma excitation source (as oppaosed to a flame) for emission spectroscopy.
what are the advantage and disadvantage of strategic coupling for developing countries ?
what are the advantage and disadvantage of strategic coupling for developing countries ?
what is the Symbolic Learning? what is advantage and disadvantage for Symbolic Learning? and what is...
what is the Symbolic Learning? what is advantage and disadvantage for Symbolic Learning? and what is advantage and disadvantage for Symbolic Learning in python? where we can use it? why we use it ? provide abstract and conclusion on symbolic learning? and provide three example on symbolic learning in Python.
Discuss ONE advantage and ONE disadvantage of supporting to include off-balance sheet activities
Discuss ONE advantage and ONE disadvantage of supporting to include off-balance sheet activities
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT