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

  • Home
  • SEARCH
  • 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 971379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:57:23+00:00 2026-05-16T02:57:23+00:00

Inside a large loop, I currently have a statement similar to if (ptr ==

  • 0

Inside a large loop, I currently have a statement similar to

if (ptr == NULL || ptr->calculate() > 5) 
  {do something} 

where ptr is an object pointer set before the loop and never changed.

I would like to avoid comparing ptr to NULL in every iteration of the loop. (The current final program does that, right?) A simple solution would be to write the loop code once for (ptr == NULL) and once for (ptr != NULL). But this would increase the amount of code making it more difficult to maintain, plus it looks silly if the same large loop appears twice with only one or two lines changed.

What can I do? Use dynamically-valued constants maybe and hope the compiler is smart? How?

Many thanks!

EDIT by Luther Blissett. The OP wants to know if there is a better way to remove the pointer check here:

loop {
 A; 
 if (ptr==0 || ptr->calculate()>5) B;
 C;
}

than duplicating the loop as shown here:

if (ptr==0) 
loop {
 A; 
 B;
 C;
} 
else loop {
 A;
 if (ptr->calculate()>5) B;
 C;
}
  • 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-16T02:57:24+00:00Added an answer on May 16, 2026 at 2:57 am

    In C++, although completely overkill you can put the loop in a function and use a template. This will generate twice the body of the function, but eliminate the extra check which will be optimized out. While I certainly don’t recommend it, here is the code:

    template<bool ptr_is_null>
    void loop() {
        for(int i = x; i != y; ++i) {
           /**/
           if(ptr_is_null || ptr->calculate() > 5) {
              /**/
           }
           /**/
        }
    }
    

    You call it with:

    if (ptr==NULL) loop<true>(); else loop<false>();
    

    You are better off without this “optimization”, the compiler will probably do the RightThing(TM) for you.

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

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out this Ruby interface to the HTML Tidy library. May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer This will return you top 10 users: SELECT u.*, (… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer Well there is not just one thing to do to… May 16, 2026 at 8:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have one large div with one smaller div inside it. The smaller div
I have a vector containing large number of elements. Now I want to write
I have developed a quite large application using MFC. Naturaly, I used GDI for
What I have to do I'm trying to manipulate some rather large amounts of
I am using a foreach loop within PHP similar to this: foreach ($class->getAttributes() as
I have a perl script (or any executable) E which will take a file
I currently try to write a program for the VFP unit of the iPhone
I have programmed an embedded software (using C of course) and now I'm considering
Inside of a stored procedure, I populate a table of items (#Items). Just basic
I've got this nasty problem where sending multiple, large messages in quick succession from

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.