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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:40:11+00:00 2026-05-21T12:40:11+00:00

I have a counter which counts to 4 in a loop, and a counter

  • 0

I have a counter which counts to 4 in a loop, and a counter which counts how many of these loops have passed.

In my code below both labels setVisibility to true at count 1.
But I want label1 to appear at count 1 and label2 at count 2.

My question to you is:

What’s more efficient, to set the labels visibility with only if statements, or
can I use do-while statements multiple times?

class counters2 implements ActionListener
{
    int count = 0, alternativecounter = 0;
    public void actionPerformed(ActionEvent e)
    {
        int fakeSecond = (count++ % 4) + 1;
        if(fakeSecond == 1)
        {
            alternativecounter += 1;
            if(alternativecounter == 3)
            {
                do
                {
                    label1.setVisible(true);
                }while(count == 1);
                do
                {
                    label2.setVisible(true);
                }while(count == 2);
            }
        }
    }
}
  • 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-21T12:40:12+00:00Added an answer on May 21, 2026 at 12:40 pm

    A do while is meant for loops – that is, repeatedly executing code. Did you notice that, if alternativecounter is 3, the code enters an infinite loop? Further, the body of a do while is always executed at least once, so (infinite loop aside) this code is not correct.

    If the code to loop you’re trying to conditionally execute shouldn’t be executed more than once (and here, it’s clear that it really shouldn’t), use an if statement.


    As others have said, this is also a ridiculous way to try to optimize your code – it’s premature optimization and micro-optimization at that.

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

Sidebar

Related Questions

I have this piece of code on my website which counts the number of
I have a simple piece of Scala code. I loop sequentially through a List
-(void)determineClosestLocationToUser:(NSArray *)allLocations locationOfUser:(MapLocation *)userLocationIn { // Determine how many objects you have in the
I am trying to have a counter (count seconds and minutes) and update it
gcc 4.4.4 c89 Is there a better way to do this? I have the
I know a few questions have been asked like this one before, such as
I'm doing K&R Exercise 5-4 (p107). Write the function strend(s,t) , which returns 1
I have having some trouble printing a char from an ASCII value stored in
In my appengine python app, I have sharded counters that count the number of
I've looked over this about 15 times by now to no avail. I cannot

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.