I’m currently working through Part 3 of the new Foundational C# with Microsoft Course and I found it very good so far for me as a newbie in C#. This evening I reached unit 5 of 8 of the module “Add looping logic to your code using the do-while and while statements in C#” found in said course part 3 called “Add logic to C# console applications (Get started with C#, Part 3)”. This unit is called “Exercise - Complete a challenge activity to differentiate between do and while iteration statements”. This is the first time in this whole course that I have problems to follow the material. Suddenly I have to deal with nullable types, managing user input, string handling and int.TryParse stuff. Has anybody noticed the lack of prior explanation of these topics or did I miss something? I did the course in German, so may be these topics were introduced in the English course. I will check that out tomorrow. Not to complain too much, but this slows down my progress and I wanted to ask you, if you have a good source to get some additional training and information about when to use do while and when to use while? Thanks in advance and have a great time.
Hello fellow freeCodeCamp friends. In the meantime I checked if my confusion came from doing the course in German but that was not the case. I finally could find solutions for the first two assignments, but I’m still stuck into the third. It was equally demanding as rewarding to dig into the topics I had not learned before. I hope to finish the 3. solution tomorrow. I need to restart it, because I thought I could do it with a foreach. Maybe one could, but I will go with a for iteration like Microsoft shows in their solution. If anybody has done the 3. exercise with a foreach, please let me know. Have a great time, folks.
They might have went a little two advanced in their discussion for the beginner course. But I have already completed the challenge though. Underneath the details and blur tag is my solution. It has been blurred and obscured to give others a fair chance at finding the answer on their own. But yes it is possible.
My solution
string[] myStrings = new string[2] { "I like pizza. I like roast chicken. I like salad", "I like all three of the menu choices" };
int periodLocation;
foreach (string myString in myStrings)
{
periodLocation = myString.IndexOf('.');
string sentence = myString;
if (periodLocation >= 0)
{
do
{
string printMe = sentence.Substring(0, periodLocation);
sentence = sentence.Remove(0, periodLocation + 1);
Console.WriteLine(printMe.TrimStart());
periodLocation = sentence.IndexOf('.');
} while (periodLocation > -1);
Console.WriteLine(sentence.TrimStart());
}
else
{
Console.WriteLine(sentence);
}
}
Hello there. Thanks a lot. I appreciate your support. I just checked it out quickly and it works well. I will go through it in detail tomorrow to see where I got stuck. Very nice, thanks again.