Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,293 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 935 people online right now. Registration is fast and FREE... Join Now!




While loop question

 
Reply to this topicStart new topic

While loop question

jstephens
18 Aug, 2007 - 09:14 AM
Post #1

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 214



Thanked: 1 times
My Contributions
Can some one help me to understand why this works.
CODE

public static boolean askForAnotherRound(String prompt)
{
    while(true)
    {
        String Answer;
        System.out.println("\n" + prompt + " (Y or N) ");
        answer = sc.next();

        if(answere.equalsIgnoreCase("Y"));
         return true;
        else if (answer.equalsIgnoreCase("N"));
          return false;
    }
}


I understand that while(true) creates an Infinte loop but why does it stop if you press Y or N. Wouldn't a return True keep the loop going?


User is offlineProfile CardPM
+Quote Post


n_b
RE: While Loop Question
18 Aug, 2007 - 09:58 AM
Post #2

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 8


My Contributions
No!

The return statement transfers controll back to the calling method, thus breaking free of the loop.
User is offlineProfile CardPM
+Quote Post

manickavasakam
RE: While Loop Question
18 Aug, 2007 - 10:12 AM
Post #3

New D.I.C Head
*

Joined: 18 Aug, 2007
Posts: 1


My Contributions
Try the following code instead of one that you have written

CODE
public static boolean askForAnotherRound(String prompt)
{
    while(true)
    {
        String Answer;
        System.out.println("\n" + prompt + " (Y or N) ");
        answer = sc.next();

        if(answere.equalsIgnoreCase("Y"))
                continue;
        else if (answer.equalsIgnoreCase("N"))
                break;
    }
}
EDIT: PLEASE USE CODE TAGS

Problems with the earlier code was

i) the RETURN statement that will cause the program to return from that function to calling function

ii) the same as with the second RETURN statement also.

iii) I think you want to continue when the input is "Y" and exit the loop while
the input is "N".

iv) So, I have replaced those two statements with CONTINUE and BREAK statements respectively.

This post has been edited by PennyBoki: 26 Aug, 2007 - 10:27 PM
User is offlineProfile CardPM
+Quote Post

jstephens
RE: While Loop Question
18 Aug, 2007 - 12:51 PM
Post #4

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 214



Thanked: 1 times
My Contributions
QUOTE(manickavasakam @ 18 Aug, 2007 - 12:12 PM) *

Try the following code instead of one that you have written

public static boolean askForAnotherRound(String prompt)
{
while(true)
{
String Answer;
System.out.println("\n" + prompt + " (Y or N) ");
answer = sc.next();

if(answere.equalsIgnoreCase("Y"))
continue;
else if (answer.equalsIgnoreCase("N"))
break;
}
}

Problems with the earlier code was

i) the RETURN statement that will cause the program to return from that function to calling function

ii) the same as with the second RETURN statement also.

iii) I think you want to continue when the input is "Y" and exit the loop while
the input is "N".

iv) So, I have replaced those two statements with CONTINUE and BREAK statements respectively.


It is a book example and we want the statement to exit on a Y or N but keep asking if something else is input. I am just confused on why the return true when someone answers Y will not keep the loop running.
User is offlineProfile CardPM
+Quote Post

alpha02
RE: While Loop Question
18 Aug, 2007 - 02:35 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687



Thanked: 4 times
Dream Kudos: 850
My Contributions
Let's explain it simpler: the return instruction ends the current method, no matter if you are 1000 loops deep! Return is the magic keyword to end the method, that's it.
User is offlineProfile CardPM
+Quote Post

jstephens
RE: While Loop Question
18 Aug, 2007 - 03:24 PM
Post #6

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 214



Thanked: 1 times
My Contributions
QUOTE(alpha02 @ 18 Aug, 2007 - 04:35 PM) *

Let's explain it simpler: the return instruction ends the current method, no matter if you are 1000 loops deep! Return is the magic keyword to end the method, that's it.


Ok I think I understand. So return acts like break then.
User is offlineProfile CardPM
+Quote Post

alpha02
RE: While Loop Question
18 Aug, 2007 - 03:27 PM
Post #7

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687



Thanked: 4 times
Dream Kudos: 850
My Contributions
QUOTE(jstephens @ 18 Aug, 2007 - 07:24 PM) *

QUOTE(alpha02 @ 18 Aug, 2007 - 04:35 PM) *

Let's explain it simpler: the return instruction ends the current method, no matter if you are 1000 loops deep! Return is the magic keyword to end the method, that's it.


Ok I think I understand. So return acts like break then.


Not exactly.

"Break" exits the current loop and continues the code after it. It works with while, do and for loops. Also used to exit a switch case.

"Return" exits the loop AND ends the method.
User is offlineProfile CardPM
+Quote Post

jstephens
RE: While Loop Question
18 Aug, 2007 - 03:37 PM
Post #8

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 214



Thanked: 1 times
My Contributions
QUOTE(alpha02 @ 18 Aug, 2007 - 05:27 PM) *

QUOTE(jstephens @ 18 Aug, 2007 - 07:24 PM) *

QUOTE(alpha02 @ 18 Aug, 2007 - 04:35 PM) *

Let's explain it simpler: the return instruction ends the current method, no matter if you are 1000 loops deep! Return is the magic keyword to end the method, that's it.


Ok I think I understand. So return acts like break then.


Not exactly.

"Break" exits the current loop and continues the code after it. It works with while, do and for loops. Also used to exit a switch case.

"Return" exits the loop AND ends the method.


Thanks, that cleared a couple of questions I had.
User is offlineProfile CardPM
+Quote Post

alpha02
RE: While Loop Question
18 Aug, 2007 - 08:41 PM
Post #9

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687



Thanked: 4 times
Dream Kudos: 850
My Contributions
QUOTE(jstephens @ 18 Aug, 2007 - 07:37 PM) *

QUOTE(alpha02 @ 18 Aug, 2007 - 05:27 PM) *

QUOTE(jstephens @ 18 Aug, 2007 - 07:24 PM) *

QUOTE(alpha02 @ 18 Aug, 2007 - 04:35 PM) *

Let's explain it simpler: the return instruction ends the current method, no matter if you are 1000 loops deep! Return is the magic keyword to end the method, that's it.


Ok I think I understand. So return acts like break then.


Not exactly.

"Break" exits the current loop and continues the code after it. It works with while, do and for loops. Also used to exit a switch case.

"Return" exits the loop AND ends the method.


Thanks, that cleared a couple of questions I had.


You're welcome. Any more questions then just ask.
User is offlineProfile CardPM
+Quote Post

Mastergeek666
RE: While Loop Question
26 Aug, 2007 - 04:24 PM
Post #10

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 137



Thanked: 1 times
Dream Kudos: 75
My Contributions
Hey guys what does the sc in
CODE
answer = sc.next();
mean???
User is offlineProfile CardPM
+Quote Post

alpha02
RE: While Loop Question
26 Aug, 2007 - 05:42 PM
Post #11

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687



Thanked: 4 times
Dream Kudos: 850
My Contributions
QUOTE(Mastergeek666 @ 26 Aug, 2007 - 08:24 PM) *

Hey guys what does the sc in
CODE
answer = sc.next();
mean???


Post the full code, I'll tell ya. Many objects have the next() function, and they all return different values. Example, the java.sql.ResultSet class has it, and it returns a boolean that tells if another result is available. Post the full code, and I will tell you smile.gif

This post has been edited by alpha02: 26 Aug, 2007 - 05:45 PM
User is offlineProfile CardPM
+Quote Post

Mastergeek666
RE: While Loop Question
26 Aug, 2007 - 06:18 PM
Post #12

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 137



Thanked: 1 times
Dream Kudos: 75
My Contributions
QUOTE(alpha02 @ 26 Aug, 2007 - 06:42 PM) *

QUOTE(Mastergeek666 @ 26 Aug, 2007 - 08:24 PM) *

Hey guys what does the sc in
CODE
answer = sc.next();
mean???


Post the full code, I'll tell ya. Many objects have the next() function, and they all return different values. Example, the java.sql.ResultSet class has it, and it returns a boolean that tells if another result is available. Post the full code, and I will tell you smile.gif




no no I was just wondering from the code at the top because I wasn't sure what it stood for. Thats all.
User is offlineProfile CardPM
+Quote Post

alpha02
RE: While Loop Question
26 Aug, 2007 - 06:23 PM
Post #13

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687



Thanked: 4 times
Dream Kudos: 850
My Contributions
QUOTE(Mastergeek666 @ 26 Aug, 2007 - 10:18 PM) *

QUOTE(alpha02 @ 26 Aug, 2007 - 06:42 PM) *

QUOTE(Mastergeek666 @ 26 Aug, 2007 - 08:24 PM) *

Hey guys what does the sc in
CODE
answer = sc.next();
mean???


Post the full code, I'll tell ya. Many objects have the next() function, and they all return different values. Example, the java.sql.ResultSet class has it, and it returns a boolean that tells if another result is available. Post the full code, and I will tell you smile.gif




no no I was just wondering from the code at the top because I wasn't sure what it stood for. Thats all.


Ok it is a Scanner
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:06PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month