C
C#3mo ago
blunt

✅ while loops

while loops keep repeating the same
Console.WriteLine("");
Console.WriteLine("");
over and over without waiting for the user to respond to the
String a = Console.ReadLine();
String a = Console.ReadLine();
anyone know how to solve this?
18 Replies
ero
ero3mo ago
$code
MODiX
MODiX3mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
ero
ero3mo ago
$ask
MODiX
MODiX3mo ago
How to get the best help :catpog: Make a post in #help or one of the topic channels under Development. Avoid asking :catthinking: Can anybody help me? :catthinking: Has anyone used XYZ? :catthinking: Why doesn't my code work? C# is a big area! No one knows they can help unless you tell them about the small area you're trying to work in. Explain what you are doing, and potentially why for as much context as possible. Avoid screenshots where possible, share code directly in Discord. Type $code into chat to learn how to post code. See https://www.nohello.net and https://dontasktoask.com if you want common help chat room etiquette.
ero
ero3mo ago
tldr show your actual code
blunt
bluntOP3mo ago
what is tldr
Keswiik
Keswiik3mo ago
too long; didn't read
blunt
bluntOP3mo ago
Console.WriteLine("enter your name");
String name = Console.ReadLine();
while (name == "")
{
Console.WriteLine("Please enter a name");
}
Console.WriteLine("enter your name");
String name = Console.ReadLine();
while (name == "")
{
Console.WriteLine("Please enter a name");
}
my code .
TheRanger
TheRanger3mo ago
well, in the while loop u never asked for user input
Keswiik
Keswiik3mo ago
^ if you put in an empty name you're gonna get stuck looping forever
blunt
bluntOP3mo ago
how do i not i did in visual studio
Keswiik
Keswiik3mo ago
the code you just sent has your ReadLine outside of the while loop, so you definitely didn't
TheRanger
TheRanger3mo ago
code wise i mean
Keswiik
Keswiik3mo ago
and this is one of the situations where a do-while makes sense
do {
// your code here to get the name
} while (name is invalid)
do {
// your code here to get the name
} while (name is invalid)
blunt
bluntOP3mo ago
ohh tysm
Keswiik
Keswiik3mo ago
I'd also recommend using string.IsNullOrEmpty or string.IsNullOrWhitespace over someString == ""
blunt
bluntOP3mo ago
ok thanks
Keswiik
Keswiik3mo ago
:PepoSalute:

Did you find this page helpful?