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 trialPedram Namiranian
550 PointsHonestly stuck
The exercise is to create the ever annoying "knock knock who's there banana banana who?" joke, the problem is that I don't quite get the do-while and if concepts, can someone help explain them to me?
/* So the age old knock knock joke goes like this:
Person A: Knock Knock.
Person B: Who's there?
Person A: Banana
Person B: Banana who?
...as long as Person A has answered Banana the above repeats endlessly
...assuming the person answers Orange we'd see
Person B: Orange who?
...and then the punchline.
Person A: Orange you glad I didn't say Banana again?
(It's a really bad joke that makes it sound like "Aren't you glad I didn't say Banana again?")
Let's just assume the only two words passed in from the console from Person B are either banana or orange.
*/
// ====BEGIN PROMPTING CODE====
// Person A asks:
console.printf("Knock Knock.\n");
// Person B asks and Person A's response is stored in the String who:
String who = console.readLine("Who's there? ");
// Person B responds:
console.printf("%s who?\n", who);
// ==== END PROMPTING CODE ====
2 Answers
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHey there Pedram,
You first want to wrap the code in a do while loop, which has the syntax
do { //code to run while (condition to be met that breaks loop)
So this would look like.
String who;
do {
console.printf("Knock Knock.\n");
// Person B asks and Person A's response is stored in the String who:
who = console.readLine("Who's there? ");
// Person B responds:
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));
console.printf("%s you glad I didn't say Banana?", who);
Remember to declare who outside of the loop so that it can be used by console.printf statement after the loop, because without it the scope would only make the variable accessible to the loop.
Thanks, let me know if you are still left with questions, and I'll help further.
Alexander Bueno
5,398 PointsAn easy way I learned do-while loops is to understand the difference between them and a regular while loop.
while loop: is considered a pre-check. This means it will check if the conditions are true (which are in the parenthesis "( conditions placed here )" after the word "while") and then proceed to do the code placed after the curly braces "{code here}", and WHILE the condition is true it will continue to repeat the code in the curly braces until the condition reaches false.
Example format
while ( condition placed here){
code you want the loop to perform placed here;
"}"
do-while: Instead of a pre-check it is the opposite. It is a post check, meaning it will run code after the "do" and curly braces ONCE without having to be checked in the WHILE condition, and then check the conditions afterwards in the parenthesis after the word WHILE. If the condition in the while section is true it will repeat the code after the "do" over and over again until the conditions in the while section is FALSE.
Example format
do{
place your code here that you want to be done;
" ^remember that code above will be done once without having to check the WHILE CONDITION, but only once. Every time after the do it needs to check with the WHILE CONDITION."
} while ( set condition here )```
Hopefully you were able to get a better understanding of how those loops work. If you still have trouble don't be hesitant to ask.