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 trial

Android

do while loops

am stuck on how to apply the do wile loop without getting a bunch of syntax errors

3 Answers

Something like:

do {
   // knock knock
console.printf("Knock Knock.\n")
   // who's there?
String who = console.readLine("Who's there? ");

} while (who != "Orange"); // or while (who == banana);

console.printf("Orange you glad I didn't say Banana again?!\n")

Does that do the job?

It'll loop constantly until the answer given is "Orange" (case sensitive).

hey steve. thanks for that but im not yet familiar with != AND ==

Hi Natasha,

The do ... while loop needs a 'Boolean' expression to determine the route the code execution takes. That's an expression that gives a result of true or false.

So, the loop in this code will continue, erm, looping if the expression in the brackets after the while keyword is true. So, in my code above, the loop is saying "Do all the knock/knock stuff while the answer is not 'Orange'". Similarly, the while loop can be "Do all the knock/knock stuff while the answer is banana" - as there's only two possible answers in this example, these two things are the same statement.

So, != means not equal to. When I put while (who != "Orange") that means the whole loop will repeat as long as Orange is not entered as the answer. The alternative, while(who == "banana") is testing for equality, i.e. does the contents of the variable who equal "banana".

Alessandro Calorì
Alessandro Calorì
10,241 Points

Steve, for strings you can't use == and/or != because that would check if the string instance is the same. The correct condition is !who.equals("Orange"). Beware of the leading exclamation mark which stands for "NOT". Moreover, in that case, I would use "equalsIgnoreCase" instead of "equals".

Very good point! Apologies!!

Can you show us the code you've got?

The loop should look something like:

do {

    //this code executes 

} while ( //a condition is true)

/* 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 ====

thank you. Let me try it out