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 8800469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:35:47+00:00 2026-06-14T00:35:47+00:00

More often than not we need loops like this do { Type value(GetCurrentValue()); Process(value);

  • 0

More often than not we need loops like this

do
{
     Type value(GetCurrentValue());
     Process(value);
}while(condition(value));

Unfortunately this will not compile, because value‘s scope ends at }. Which means that I will have to declare it outside the loop.

Type value;
do
{
    value = GetCurrentValue(); 
    Process(value);
}while(condition(value));

I don’t like this for at least two reasons. For one, I like declaring things locally. And second, this is a problem if value is not assignable or default-constructible, but only copy-constructible.

So, my question has two sides. First, I’d like to know if there was a particular reason/difficulty in extending the do while’s scope to the final condition as well (just as the scope of variables declared in for loop includes the body of the for loop despite it physically being outside of the braces). And if you believe that the answer to my first question is “It’s just the way it is. Don’t ask why questions.” then I’d like to know if there are idioms that can help write do-while loops similar to the ones in my example but without the downsides I mentioned.

Hope the questions are clear.

  • 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-06-14T00:35:48+00:00Added an answer on June 14, 2026 at 12:35 am

    If you’d like to keep value locally scoped for the while loop, you can do this instead:

    do
    {
         Type value(GetCurrentValue());
         Process(value);
         if (! condition(value) )
             break;
    } while(true);
    

    This is just personal preference, but I find while loops structured like the following more readable (while instead of do-while):

    while(true) {
        Type value(GetCurrentValue());
        Process(value);
        if (! condition(value) ) {
            break;
        }
    }
    

    The scoping rules in C/C++ works as follows: Local variables declared within a brace {...} block is local / visible only to that block. For example:

    int a = 1;
    int b = 2; 
    {
        int c = 3;
    }
    std::cout << a;
    std::cout << b;
    std::cout << c;
    

    will complain about c being undeclared.

    As for rationale – it’s just a matter of consistency and “that’s just how the language is defined”

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

Sidebar

Related Questions

Obviously I need to update accurately. However not more often than once every 1
I have settings dialog with a number of ComboBoxes. More often than not, these
More often than I like when designers edit some of our sites' pages, they
It happens more often than not that I have to comment several lines at
More often than I'd have hoped I seem to come across method/property definitions in
Code is read more often then updated. Writing more readable code is better than
Probably this is often problem, but I can't solve it. I need to automaticallly
The question is, how can I remove elements that appear more often than once
I need to implement multilevel expandable list view, something like ExpandableListView but with more
I've been using debug() more often now, but sometimes I wonder which functions have

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.