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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:09:26+00:00 2026-05-12T22:09:26+00:00

If flag is true I have to perform step no. 1 otherwise skip it.

  • 0

If “flag” is true I have to perform step no. 1 otherwise skip it. Is there a way out to skip this unnecessary repetitive check within the loop. (As the value of flag is not changing while the execution of the loop)

private void method(boolean flag) {
        while (man > woman) {
            if (flag) {
                // Step no. 1
                System.out.println(flag);
            }
        }
    }
  • 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-12T22:09:27+00:00Added an answer on May 12, 2026 at 10:09 pm

    I’m not sure it is productive to worry about optimizations at this level. Generally it is more important to get the program working and move on to the next problem.

    Having said that, there is an optimization called loop unswitching that some compilers will do for you. They duplicate the loop, once with and once without the conditional, and move the conditional outward to select the loop. (In your example you could make the entire loop conditional but I presume that’s just an artifact of simplification for Stack Overflow.)

    But this is just one more reason not to worry too much about optimizations, at least, not until you have a profile and you know that this region of code is responsible for detectable amounts of runtime.

    Still, it’s best to write code as cleanly as you can and puzzling through issues like this will teach you good things…

    In fact, loop-invariant conditionals bother me too. I don’t believe there is a general answer. There are fancy answers involving higher order functions or lambdas, “leave-it-to-the-compiler” answers, refactor-the-whole-outer-routine answers … I would generally approve of whatever makes the code appear smaller. You have to prioritize in order to discriminate…

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

Sidebar

Related Questions

I have this function that about the following bool flag = true; while(true){ if(flag){
Is there a gcc flag to skip resolution of symbols by the compile-time linker
I have XML with the following structure, for example <root> <node flag=false/> <node flag=true/>
I am currently have two seperate applications, both perform seperate tasks, but there is
Is there a flag or other reliable method to detect if a compiled C++
Is there a flag to prevent the CDHTMLDialog from use the IE history (back,
Can I use a flag to select different WHERE clause? Something like this. SELECT
I have asked this question before - but I have spent some time thinking
I have an AcctMgr_Flag that designates a person as the account manager. This is
I am using Ruby on Rails 3 and I have this situation: In a

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.