I have tried to use a do-while outer loop with a for inner loop to implement bubble sort, but it appears to be executing once through the for loop and then not satisfying the while condition. The code is as follows:

int main(void)
    int n = 5;
    int values[] = {0,5,4,3,2};

    // Advance through unsorted part of list

    int repeater = 0;
    int k = 0;

        int repeater = 0;

        // Iterate through array    
        for (int i = 0; i < n - k - 1; i++)

            // Swap if left element is larger than right element
            if (values[i] > values[i + 1])
                int temp = values[i + 1];
                values[i + 1] = values[i];
                values[i] = temp;
        // Increment number of sorted elements
    while (repeater > 0);

When I run this code, the array values ends up as {0,4,3,2,5} didn't really help explain what was going on.

On the first time through the for loop nothing happens (as expected because 0 < 5). On the 2, 3, and 4 times through the value 5 is correctly swapped and the variable repeater increments by 1.

The next time, the for loop does not execute because i == n (not < n). The code appropriately then skips to increment k. If in I print the value of repeater after k is incremented, it still shows 3.

But when the next curly brace is executed, the value of repeater suddenly changes to 0, which does not satisfy the while condition, and therefore terminates the program.

Can anyone explain what's going on here? I'm asking more for the sake of learning, because I plan to stop using a do-while loop for sort. This is my second attempt at doing it!

1 Answer 1


you're defining repeater twice, once outside of the do..while loop and the other one is inside.

the one defined inside shadows the one defined outside in the scope of the body of do. the one defined inside also goes out of scope as the closing brace of the do is reached.

the one while (repeater > 0) sees is actually the one declared outside which remains 0. so the loop executes only once then the condition is evaluated to false so control leaves the loop.

  • Very helpful. For learning purposes, is it possible to use this do-while construction? If I declare the variable OUTSIDE the do-while loop only, then I have no way of resetting it to zero each time the loop reruns (and therefore no way of satisfying the condition when the inner loop does not make a replacement). If I declare the variable INSIDE the do-while loop only, then it's not accessible to the while statement at the end. Or do I just need to declare it outside and then change the value inside? Jun 25, 2015 at 16:27
  • @BrianPoindexter indeed it's possible. you just need to know that when assigning a value to a variable that was previously defined and still in scope, you don't mention the data type again. just variableName = value;.
    – kzidane
    Jun 25, 2015 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .