Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBaw Gadwe
Courses Plus Student 326 PointsI don't understand why did we use a while loop here: What will happen if we don't use it?
I don't understand why did we use a while loop here: What will happen if we don't use it? If I just removed the while(true) and the break, the program will convert text just when the user typed the text and hit the enter button, ain't I right?
while(true)
{
Console.Write(": ");
string input = Console.ReadLine();
if(string.IsNullOrWhiteSpace(input))
{
break;
}
string output = MorseCodeTranslator.ToMorse(input);
Console.WriteLine(output);
}
1 Answer
andren
28,558 PointsHe used a while
loop because he wants the program to keep prompting for input, so that you can have it translate multiple sentences to morse code.
Without the loop the code will still work fine but it will only translate one sentence then end the program, which is not how Jeremy wanted the program to act.
Using an infinite while
loop that only ends when it encounters a break
statement is a pretty common way of creating user prompts when you don't know how many times a user will want to enter info. It's something you'll see more of and probably use yourself when you get farther into programming more complex applications.
Baw Gadwe
Courses Plus Student 326 PointsBaw Gadwe
Courses Plus Student 326 PointsGot it. Thank you :)