Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (anonymous):

Im new to C# and I am having difficulty understanding Do while loops. My project asks for one functions that reads a word, and gives a number placement for a letter. How would a use a do while loop for this (my teacher said this would be easiest however i can use other methods) static void WordAnalyzer() { string word; char letter; do { Console.WriteLine("Given a word I will be able to let you know what letters are used; and their order number in the word:"); while (letter != 1); {

OpenStudy (woodrow73):

I'm not really a C guy, but I'll show an example of how I can practically implement do while loops to help your understanding. First I'll explain it a little. In a traditional while loop, you *first* check the boolean expression before executing the code block: ``` while(theChefCooks) { i_eat(); chefThinksIfHeWantsToKeepCooking(); } ``` Then after the code block executes, it will test the boolean expression again, and if it's true, it repeats. However, in a do-while loop, it will *always* execute the code block once before testing the boolean expression, and before it decides it wants to loop. I typically use do-while loops when doing input validation, here's one cheesy but practical example asking the user to input the year they were born. ``` boolean is4Characters = false; boolean isANumber = false; boolean isWithin120Years = false; do{ String input = askUserTheirBirthYear(); String message = ""; if(input.length() != 4) { is4Characters = false; message = "Your input was not 4 characters long. "; } else { is4Characters = true; } if(!isValidNumber(input)) { isANumber = false; message += "Your input was not a valid number. "; } else { isANumber = true; } if(!isANumberWithinLast120Years(input)) { isWithin120Years = false; if(isANumber && is4Characters) { message += "Your input was not a number within the last 120 years."; } } else { isWithin120Years = true; } if(isANumber && is4Characters && isWithin120Years) { message = "valid input."; } printMessageToUser(message); }while(!is4Characters || !isANumber || !isWithin120Years); ```

OpenStudy (rsmith6559):

do/while loops mirror while/do loops. The differences are that because the condition is tested at the end, the code in a while/do loop will execute at least once. A do/while loop won't execute if it's condition is never true. When I read files byte by byte in C, I use a do/while loop and test for end of file after each byte is read.

OpenStudy (rsmith6559):

BTW, the code in your question will probably work if you move the while statement after the '}'.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!