Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 817251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:57:49+00:00 2026-05-15T01:57:49+00:00

I found myself confronted with an interview question where the goal was to write

  • 0

I found myself confronted with an interview question where the goal was to write a sorting algorithm that sorts an array of unsorted int values:

int[] unsortedArray = { 9, 6, 3, 1, 5, 8, 4, 2, 7, 0 };

Now I googled and found out that there are so many sorting algorithms out there!
Finally I could motivate myself to dig into Bubble Sort because it seemed pretty simple to start with.

I read the sample code and came to a solution looking like this:

    static int[] BubbleSort(ref int[] array)
    {
        long lastItemLocation = array.Length - 1;
        int temp;
        bool swapped;

        do
        {
            swapped = false;
            for (int itemLocationCounter = 0; itemLocationCounter < lastItemLocation; itemLocationCounter++)
            {
                if (array[itemLocationCounter] > array[itemLocationCounter + 1])
                {
                    temp = array[itemLocationCounter];
                    array[itemLocationCounter] = array[itemLocationCounter + 1];
                    array[itemLocationCounter + 1] = temp;

                    swapped = true;
                }
            }

        } while (swapped);

        return array;
    }

I clearly see that this is a situation where the do { //work } while(cond) statement is a great help to be and prevents the use of another helper variable.

But is this the only case that this is more useful or do you know any other application where this condition has been used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:57:50+00:00Added an answer on May 15, 2026 at 1:57 am

    In general:

    • use do...while when you want the body to be executed at least once.
    • use while... when you may not want the body to be executed at all.

    EDIT: I’d say the first option comes up about 10% of the time and the second about 90%. You can always re-factor to use either, in either circumstance. Use the one that’s closest to what you want to say.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found myself writing the following a lot: int location =2; vector<int> vec; vector<int>::iterator
I have found myself designing a language for fun that is a cross between
I recently found myself writing a piece of code that executed a Core Data
I found myself wanting to implement an IAwaitable class (something that implements asynchronous calls
I found myself staring at code that looks similar to this: private double? _foo;
I've found myself asking the question: should I make one repository for all the
I've found myself having to write some VBA code recently and just wondered if
I just found myself absent-mindedly using from as an identifier. I realise that it
I found myself confused with the array and slice data types. From Go docs,
I've found myself coding this type of thing a few times. for (int i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.