Computer Science (MATLAB)

posted by .

I had a test in my computer science course in which I was asked if any while loop can be replaced with a for loop. I put true. I guess the answer was false. My professor said that if you had a while loop in which you asked the user to input a certain value that had to be within a certain range and kept on iterating over the loop until the user inputted a value within the range that you wouldn't be able to do this with a for loop.

I however thought that any for loop can be written with a while loop and any while loop can be rewritten with a for loop. I think I may have even read something about this in my textbook but am unable to come up away with doing what my professor said with a for loop but believe that it's possible. Can anyone please come up with a way to do such a thing with a for loop?

for example a while loop

A=0;
while A<1
x=input('Enter a value');
if x>4 && x<10
A=1
else
end

this would force the user to enter a number between 4 and 10, not including 4 and 10, and would just keep on iterating over the loop until the user does.

  • Computer Science (MATLAB) -

    The while loop exits whenever a user-specified condition is reached, in this case when x≤4 or x≥10.

    A for loops exits (normally) when the stated number of cycles is reached, i.e. not an arbitrary user condition.

    There are different ways to "misuse" the for loop.

    A common one is to create an infinite for-loop and make an independent exit condition, such as:

    for(i=0;i<1000;i=i){ // this line actually compiles and runs in Java
    // enter x
    if(x<=4 || x>=10)break;
    }

    Another way to "force" a while loop capability in languages such as Java, C and C++ would be:
    int x=5;
    for(int i=0;x>4&&x<10;i++){ // bad style!
    if(i>2)x=0; // or an input for x
    System.out.println("printing");
    }

    But these are exceptional uses of the for-loop and are considered bad style, even the syntax is correct.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. physics

    A loop of wire is lying flat on a tabletop. A uniform magnetic field is directed vertically UPWARDS such that it is perpendicular to the tabletop (and the loop). Beyond the edge of the table, the magnetic field is zero. Imagine that …
  2. physics

    A loop of wire is lying flat on a tabletop. A uniform magnetic field is directed vertically UPWARDS such that it is perpendicular to the tabletop (and the loop). Beyond the edge of the table, the magnetic field is zero. Imagine that …
  3. physics

    A loop of wire is lying flat on a tabletop. A uniform magnetic field is directed vertically UPWARDS such that it is perpendicular to the tabletop (and the loop). Beyond the edge of the table, the magnetic field is zero. Imagine that …
  4. physics

    A loop of wire is lying flat on a tabletop. A uniform magnetic field is directed vertically UPWARDS such that it is perpendicular to the tabletop (and the loop). Beyond the edge of the table, the magnetic field is zero. Imagine that …
  5. physics

    A loop of wire is lying flat on a tabletop. A uniform magnetic field is directed vertically UPWARDS such that it is perpendicular to the tabletop (and the loop). Beyond the edge of the table, the magnetic field is zero. Imagine that …
  6. Computer Science (MATLAB)

    I had a test in my computer science course in which I was asked if any while loop can be replaced with a for loop. I put true. I guess the answer was false. My professor said that if you had a while loop in which you asked the user …
  7. PHYSICSI

    The rotating loop in an AC generator is a square 11.0 cm on a side. It is rotated at 60.0 Hz in a uniform field of 0.800 T. Calculate the following quantities as functions of time t, where t is in seconds. (a) the flux through the …
  8. Fundamental of computer science

    Pseudocode CountPositive integer totalPos,number Begin loop Input numbers If number>0 then totalPos<-totalPos +1 Loop while number<>0 Print totalPos Convert the above Pseudocode into a pascal program.
  9. College Physics

    A student of mass M = 82 kg takes a ride in a frictionless loop-the-loop at an amusement park. The radius of the loop-the-loop is R = 15 m. The force due to the seat on the student at the top of the loop-the-loop is FN = 696 N and …
  10. Computer science

    While you can pass only one argument into a function, you can return as many values as needed from a function. True False A library function saves time for the programmer because it allows for code reuse. True False Each function should …

More Similar Questions