0

I have a "foreach" function in the code that's supposed to shift all objects by two elements. For some reason no output is given at the end. I have no idea what's the problem.

Here is the source code:

using System;

namespace testy
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "W", "Y", "Z" };

            string[] lol = {"a", "b", "r", "f", "d", "q", "u"};
            int i = 0;
            int por = 0;
            string[] output = new string[lol.Length];
            while(lol.Length == i)
            {
                while(lol[i] == alphabet[por])
                {
                    por++;
                }
                output[i + 2] = alphabet[por];
                por = 0;
                i++;
            }
            foreach(string lol123 in output)
            {
                Console.WriteLine(lol123);
            }
            Console.ReadKey();
        }
    }
}
3
  • 2
    while(lol.Length == i) is false (lol.Length == 7, but i==0), so it never enters the loop. Btw, try to use debugger to execute the programm step by step if there are any uncertainties about the code Commented Jul 26, 2019 at 7:20
  • 1
    How should the output look like ? Commented Jul 26, 2019 at 7:25
  • Worth reading. Commented Jul 26, 2019 at 7:51

3 Answers 3

2

lol.Length is not equal to i, which is zero. While loops don't do anything when their condition is false in the first place.

Sign up to request clarification or add additional context in comments.

Comments

1
int i = 0;
....
while(lol.Length == i){ ... }

Here, you are iterating while lol is 0 in length. Since lol is initialized to be 7 elements long, the whole loop is skipped.

Comments

0

Result of while(lol.Length == i) condition is false due to which while loop is not getting executed and the last foreach loop is iterating through output array which is initialized but having null values in each element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.