Shaghayegh Tavakoli
Shaghayegh Tavakoli

Reputation: 530

What is the difference between these two loops?

I want to write a loop in which I increase my variable i, until arr[i] is less or equal than v.

I've tried these two loops but only the first loop is working and I can't tell the difference.

first loop:

do{
   i++;
   if(arr[i] >= v)
     break;
}while(true);

second loop:

do{
   i++;
}while(arr[i] <= v)

I was wondering what exactly the second loop is doing that I don't get the expected result.

Upvotes: 0

Views: 24

Answers (1)

static const
static const

Reputation: 943

In the first one you are breaking when the value is greater than or equal to v

In the second one you are breaking when the value is greater than v

The break conditions are different for each loop

For the second one to work correctly,

do{
   i++;
}while(arr[i] < v)

Upvotes: 2

Related Questions