4 Replies - 225 Views - Last Post: 26 October 2019 - 06:09 AM Rate Topic: -----

#1 Warpz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-October 19

For Loop Won't Initiate

Posted 26 October 2019 - 03:59 AM

Hi guys

I have this loop:

for (int i = 0; i == prev_Income.Length; i++)
            {
                Console.WriteLine(prev_Income[i]);
            }



I think I am just missing something obvious, but the length of
 prev_Income 
is 9. Yet the loop just gets skipped every time.

full code is as follows:

static void load_Income()
        {
            Console.WriteLine("Loading Previous Income:");

            string[] lines = File.ReadAllLines("C:\\Users\\jorda\\OneDrive\\Documents\\My App Data\\Income.txt");
            List<string> Income = new List<string>();
            foreach (string amount in lines)
            {
                Income.Add(amount);
            }

            string[] prev_Income = Income.ToArray();
for (int i = 0; i == prev_Income.Length; i++)
            {
                Console.WriteLine(prev_Income[i]);
            }




Please Help.

Is This A Good Question/Topic? 0
  • +

Replies To: For Loop Won't Initiate

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: For Loop Won't Initiate

Posted 26 October 2019 - 04:43 AM

Recall that a for loop is just syntactic sugar for a while loop. A while loop checks the condition at before entering the loop body.

In other words:
for(int i = 0; i == prev_Income.Length; i++)
{
    // body
}



is the same as:
int i = 0;
while (i == prev_Income.Length)
{
    // body
    i++;
}


Was This Post Helpful? 1
  • +
  • -

#3 Warpz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-October 19

Re: For Loop Won't Initiate

Posted 26 October 2019 - 04:50 AM

View PostSkydiver, on 26 October 2019 - 04:43 AM, said:

Recall that a for loop is just syntactic sugar for a while loop. A while loop checks the condition at before entering the loop body.

In other words:
for(int i = 0; i == prev_Income.Length; i++)
{
    // body
}



is the same as:
int i = 0;
while (i == prev_Income.Length)
{
    // body
    i++;
}



I assume you meant
int I = 0;
while (i != prev_Income.Length)
{

Sorry, that sent early.

i assume you meant

[code]

int i = 0
while (i != prev_Income.length)
{
i++;
}



okay... well that wasn't meant to happen.... Can't even get the forum to work for me atm.

Regardless... the While loop worked. Thankyou
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,414
  • Joined: 21-June 11

Re: For Loop Won't Initiate

Posted 26 October 2019 - 06:09 AM

View PostWarpz, on 26 October 2019 - 01:50 PM, said:

i assume you meant [...]

while (i != prev_Income.length)



No, that's not the equivalent version. That's the version that actually works. The one with == is the one to which your original code is equivalent and that's why your original code does not work.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: For Loop Won't Initiate

Posted 26 October 2019 - 06:09 AM

No. I didn't mean that. I put in the == in both the for loop and the while loop because that is what you had in you original post. The compiler won't magically negate the your boolean condition when it translates the for loop into a while loop. I put there to let you see that the condition you were using was incorrect.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1