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 trialNatenda Manyau
3,005 Pointsdo while loops
am stuck on how to apply the do wile loop without getting a bunch of syntax errors
3 Answers
Steve Hunter
57,712 PointsSomething 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).
Steve Hunter
57,712 PointsCan you show us the code you've got?
The loop should look something like:
do {
//this code executes
} while ( //a condition is true)
Natenda Manyau
3,005 Points/* 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 ====
Natenda Manyau
3,005 Pointsthank you. Let me try it out
Natenda Manyau
3,005 PointsNatenda Manyau
3,005 Pointshey steve. thanks for that but im not yet familiar with != AND ==
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi 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 oftrue
orfalse
.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 variablewho
equal "banana".Alessandro Calorì
10,241 PointsAlessandro Calorì
10,241 PointsSteve, 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".
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsVery good point! Apologies!!