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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:34:46+00:00 2026-05-26T19:34:46+00:00

if (theArray[i-1] < theArray.Length && theArray[i-1] != null) Is what i got after some

  • 0
 if (theArray[i-1] < theArray.Length && theArray[i-1] != null)

Is what i got after some research. But it gives just gives me out of index error, because i do i-1.

But i want the if statement to check whether the key/index on i-1 exists, then do something..

See what i want to do is plus the values of the indexes next to the current index.

for (int i = 0; i < theArray.Length; i++)
{
    Left = 0;
    Right = 0;
            if (i != 0)
            {
                Left = theArray[i - 1];
            }
            if (theArray[i + 1] < theArray.Length)
            {
                Right = theArray[i + 1];
            }

    calc = Left + Right;
    output2.Text += calc + ", ";
}

So if the left is there then change the left value (default is 0, right and left), and if right is there then change the right value. And then calculate both values taken from theArray[].

E.g if its on theArray[16] it should take the left, theArray[15] and theArray[17] and plus together.

  • 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-26T19:34:46+00:00Added an answer on May 26, 2026 at 7:34 pm

    I think you are looking for:

    if(i < theArray.Length)
    

    Or possibly:

    if(i < theArray.Length && theArray[i] != null)
    

    I would normally be using i in a loop like this:

    for(int i = 0; i < theArray.Length; i++)
    

    Update:

    You can’t use i-1, because on the first iteration, when i == 0, this would evaluate to -1. This is an illegal index in an array.


    Update 2:

    I think I understand what you are trying to do now. Here is one way to do it:

    for (int i = 0; i < theArray.Length; i++)
    {
        Left = 0;
        Right = 0;
        if (i > 0)
        {
            Left = theArray[i-1];
        }
        if (i < theArray.Length - 1)
        {
            Right = theArray[i+1];
        }
    
        calc = Left + Right;
        output2.Text += calc + ", ";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Silly little C program, but for the life of me I can't figure out
This is well known code to compute array length in C: sizeof(array)/sizeof(type) But I
I think I have implemented some of my code wrong. I cannot figure out
I can decode the points, I just need to figure out how to loop
In C# I use the Length property embedded to the array I'd like to
How best to put the array (100 or more length) in the database (MySQL)?
A SDK is returning me an array with multiple dimensions such as: int[,,] theArray
I'm getting the error Invalid argument supplied for foreach() even though the array being
I have the following code in some app: int lowRange=50; int[] ageRangeIndividual = {6,
I cannot get the array to display rooks or bishops (r & b). I

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.