For-Loop doesnt work.

I cant fully understand, why its not working? The variable is an integer. Why is it giving me an error?
43 Replies
ero
ero2y ago
threadCounter is only assigned to when readLine2 was > 0 and < 100
IchgrüßemeinOpa
Yes. I made a goto, if its below 0 and made threadCounter = readLine2 if its above 100
ero
ero2y ago
i'm not seeing any of that in what you provided
IchgrüßemeinOpa
second
ero
ero2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
IchgrüßemeinOpa
how can i share it with you? ill do it different
IchgrüßemeinOpa
better? so why is it giving me an error :/
ero
ero2y ago
it's not better, no. the bot very clearly showed how you properly post code. anyway, you're not covering all possibilities there are times when threadCounter is still never assigned
TheRanger
TheRanger2y ago
$codegif
IchgrüßemeinOpa
but where? ohh, if its 0?
ero
ero2y ago
when readLine2 is == 0 or == 100
IchgrüßemeinOpa
ohhhh i see. i covered them. but still giving me the error i did by <=0 and >=100
TheRanger
TheRanger2y ago
then theres another possibility u didnt cover yet
ero
ero2y ago
mhm
IchgrüßemeinOpa
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 > 0 && readLine2 < 100)
{
hesSure:
threadCounter = readLine2;
}
else if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key != ConsoleKey.Y && ignoreWarning.Key != ConsoleKey.N)
{
Console.Clear();
goto reask;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}

for (int i = threadCounter; threadCounter > 0; i--)
{
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 > 0 && readLine2 < 100)
{
hesSure:
threadCounter = readLine2;
}
else if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key != ConsoleKey.Y && ignoreWarning.Key != ConsoleKey.N)
{
Console.Clear();
goto reask;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}

for (int i = threadCounter; threadCounter > 0; i--)
{
ero
ero2y ago
though the branching might honestly just be too confusing for the compiler it might work if you change the warning logic to if-else-if-else if (N) else if (Y) else
IchgrüßemeinOpa
I dont quite understand
ero
ero2y ago
if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}
if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}
IchgrüßemeinOpa
nope didnt fix it the error is: Use of the unassigned local variable "threadCounter".
TheRanger
TheRanger2y ago
oh i think i see the issue now
ero
ero2y ago
it's just the branching roslyn needs an else branch to know everything is covered if you have
if (readLine2 > 0 && readLine2 < 100) { }
else if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
if (readLine2 > 0 && readLine2 < 100) { }
else if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
then yes, everything is covered but the compiler doesn't understand that
IchgrüßemeinOpa
yes i have exactly that
ero
ero2y ago
so you need
if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
else { }
if (readLine2 >= 100) { }
else if (readLine2 <= 0) { }
else { }
IchgrüßemeinOpa
got them all covered
ero
ero2y ago
please re-read what i said
IchgrüßemeinOpa
ohh
Pascal
Pascal2y ago
the issue with your code is that threadCounter, is sometimes unassigned. So the compiler does not know what to do when the value is unassigned and yet you are trying to access a value from it. you can fix this by assigning a default value to threadCounter. Something like int threadCounter = 0 or whatever your default value is.
ero
ero2y ago
that would be another way to fix it, yes
IchgrüßemeinOpa
seems to work
ero
ero2y ago
but i think it's good to understand branching it will become crucial for more complex logic for null handling especially
IchgrüßemeinOpa
i see but even tho in any way i covered all possibilities, i can confuse the compiler with that? because at the end it wouldve gotten an value anyways, right? wait
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}

}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}
else
{
hesSure:
threadCounter = readLine2;
}


for (int i = threadCounter; threadCounter > 0; i--)
{
int threadCounter;

reloop:

Console.WriteLine("Number Of Threads:\n");

int readLine2 = int.Parse(Console.ReadLine());

if (readLine2 >= 100)
{
reask:

Console.WriteLine("Still want to continue? [Y]/[N]");

var ignoreWarning = Console.ReadKey();

if (ignoreWarning.Key == ConsoleKey.N)
{
Console.Clear();
goto reloop;
}
else if (ignoreWarning.Key == ConsoleKey.Y)
{
threadCounter = readLine2;
}
else
{
Console.Clear();
goto reask;
}

}
else if (readLine2 <= 0)
{
Console.WriteLine("\nPlease enter a valid number.");
Thread.Sleep(3000);
Console.Clear();
goto reloop;
}
else
{
hesSure:
threadCounter = readLine2;
}


for (int i = threadCounter; threadCounter > 0; i--)
{
ero
ero2y ago
well again, it needs that final else block
IchgrüßemeinOpa
i have it like this now, and it works..
ero
ero2y ago
yup
IchgrüßemeinOpa
ye. okay. so it gets confused there. i see
ero
ero2y ago
the final else block basically says "there cannot be any other possibility here" which means everything must be covered
IchgrüßemeinOpa
okay good. Thanks mates! :3

Did you find this page helpful?