C
C#2w ago
retro!

✅ Else condition refusing to work- looks correct.

first one is what i'm trying to solve, second one is what the error shows, and the third is something else that looks the same but still doesn't work.
No description
No description
No description
18 Replies
canton7
canton72w ago
The ; at the end of your if statement shouldn't be there
retro!
retro!OP2w ago
also
No description
canton7
canton72w ago
(The compiler's even highlighted it with a yellow squiggle for you)
retro!
retro!OP2w ago
ah gonna get rid of that
canton7
canton72w ago
What's happening is that an if statement needs to be followed by a block of code. If you put a ; after the if statement then that "block of code" is empty (it's everything up to the ;), which means that the stuff following the if statement isn't actually part of the if
MODiX
MODiX2w ago
canton7
REPL Result: Success
if (false);
Console.WriteLine("Here");
if (false);
Console.WriteLine("Here");
Console Output
Here
Here
Compile: 395.230ms | Execution: 35.396ms | React with ❌ to remove this embed.
canton7
canton72w ago
That's the same as:
MODiX
MODiX2w ago
canton7
REPL Result: Success
if (false) { }
Console.WriteLine("Here");
if (false) { }
Console.WriteLine("Here");
Console Output
Here
Here
Compile: 383.948ms | Execution: 22.672ms | React with ❌ to remove this embed.
canton7
canton72w ago
So what you wrote is equivalent to:
if (ans1 == 93) { }
Console.WriteLine("Correct!");
else
{

}
if (ans1 == 93) { }
Console.WriteLine("Correct!");
else
{

}
See how that else is out on its own without a corresponding if? That's what the compiler's complaining about
retro!
retro!OP2w ago
gonna try this
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
canton7
canton72w ago
Then you have beginners being very frustrated by NRT warnings 😛 Especially as Console.ReadLine returns a nullable string
retro!
retro!OP2w ago
whoops- forgot to reply this worcked! worcked
canton7
canton72w ago
Glad to hear! $close
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
canton7
canton72w ago
If someone's just getting started with a "Guess the number" game, IMO it's far too early to be introducing NRTs
MODiX
MODiX2w ago
TeBeCo
but at the same time driving them to insanity
React with ❌ to remove this embed.

Did you find this page helpful?