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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:05:46+00:00 2026-06-01T21:05:46+00:00

int Cnt = 4; for(int i=0; i<Cnt; i++) { } Inside this loop I

  • 0
int Cnt = 4;

for(int i=0; i<Cnt; i++)  {
}

Inside this loop I want to make different decisions based on the value of i.

So what is the best way to implmenet this ??

Should i use the following way ??

for(int i=0; i<Cnt; i++) {
    if (i==0) {
        // some code 
    }
    else if(i==1) {
    // some code 
    }
}

Or will this way it will look good ??

for (int i=0; i<Cnt; i++) {
    String str = Bag[0].value ;
    String str22 = Bag[1].value     
}

I want to ask is that how can I know what the maximum length of array can be?

What is the least error prone way for doing this?

  • 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-01T21:05:48+00:00Added an answer on June 1, 2026 at 9:05 pm

    Ok I would also advocate using some sort of switch function, and would structure my code as follows

    public void fooOperation(int[] numbers) {
        for(int i=0; i < numbers.length; i++) { 
            handleValue(i);
        }
    }
    
    public void handleValue(int value)  {
        switch(i) {
            case 1:
                // some code
                break;
            case 2:
                // some code
               break;
            default:
                break;
        }
    }
    

    Something else worthy of note is if this switch statement is going to get very big its not the best solution you could use the strategy pattern to avoid such code. It does however depend on what is held in your array, the ints may represent something else (the value of an enum value for example) at which point you can convert to a strongly typed case and have a behavior for that case. This question is a matter of software engineering and really depends on the scope of what is received. If you are only ever going to have 3 cases there’s an argument that says simple a switch is best because it is just that, Simple.

    The reason I have not done the switch inline is to make that method easier to unit test without entering a loop meaning each case can be tested in isolation and when you change it you do not have to worry about accidently modifying the loop code… its tidier

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

Sidebar

Related Questions

double s_deviation(double data[],int cnt, double mean) { int i; double sum= 0; double sdeviation;
Can anyone tell me how to best possible way to convert the following code
int pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) I understand that pcap_dispatch calls
I know this should be real simple, but I have googled this problem and
Sorry if this is a noob question :( . Piece of C code. int
What is the best way to approach removing items from a collection in C#,
Recently I came across a structure definition, struct arr { int cnt; struct {
I want to modify xml file using dom ,but when I make node.getNodeValue(); it
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
int main() { int var = 0;; // Typo which compiles just fine }

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.