1

In C++ for loop is same as while loop

 for(int i=0; i<10; i++)
    {
    }

  int j;
  while(j<10)
   {
    cout<<" ";
    j++;
   }

I want to convert this for loop in while loop using MATLAB.

3
  • Why is the same for and while in C++? Commented Oct 3, 2014 at 4:56
  • I dont understand how the second code is that much different from the first? Just make 2 nested while loops... Commented Oct 3, 2014 at 4:59
  • 1
    Why would you want to convert this into a while loop if it works? while offers no built in control over a loop variable, which you obvioulsy need, so why not leave it as a for loop instead of having to code the loop variable handling yourself? Commented Oct 3, 2014 at 5:18

2 Answers 2

2

The loops are not exactly the same as you actually... Still, I don't know matlab, but since you posted a C++ example I will assume you will be able to convert this to matlab, if it helps.

int i, j;
i = j = 2;

while(...) {//i condition

   while(...) { //j condition
      <...commands...>

    j++:
   }

 j = 2; //reset inner while counter to w/e value you need
 i++;
}
Sign up to request clarification or add additional context in comments.

Comments

1
i = 2;
while i <= zoom_r -1
    j = 2;
    while j<= zoom_c -1
        ... executable code block goes here
        j = j+1;
    end
    i = i+1;
end

1 Comment

Please comment here and tell me How this loop works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.