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'm writing a counter class which doesn't have a nice name yet. It counts
Hello I have a loop which goes counts different records in my MySQL database
I have this piece of code on my website which counts the number of
I have implemented a page hit counter which counts the number of page views
I have a counter that counts up every 1 second and add 1 to
i have a column family use counter as create table command below: (KEY i
I have created one testing app for running deep counter loop. I run the
I have the following code which should be inserting into the database x amount
I have the code below running in my application and it takes approx 2
I am trying to drop tables, which have a particular suffix(passed as argument $1),

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.