Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 98,767 Java Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,030 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert

Register to Make This Box Go Away!


While loop question

2 Pages V  1 2 >  
Reply to this topicStart new topic

While loop question

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


D.I.C Head

**
Joined: 7 Nov, 2005
Posts: 176


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

Go to the top of the page


n_b
post 18 Aug, 2007 - 10: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

Go to the top of the page

manickavasakam
post 18 Aug, 2007 - 11: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 - 11:27 PM
User is offlineProfile CardPM

Go to the top of the page

jstephens
post 18 Aug, 2007 - 01:51 PM
Post #4


D.I.C Head

**
Joined: 7 Nov, 2005
Posts: 176


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

Go to the top of the page

alpha02
post 18 Aug, 2007 - 03:35 PM
Post #5


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
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

Go to the top of the page

jstephens
post 18 Aug, 2007 - 04:24 PM
Post #6


D.I.C Head

**
Joined: 7 Nov, 2005
Posts: 176


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

Go to the top of the page

alpha02
post 18 Aug, 2007 - 04:27 PM
Post #7


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
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

Go to the top of the page

jstephens
post 18 Aug, 2007 - 04:37 PM
Post #8


D.I.C Head

**
Joined: 7 Nov, 2005
Posts: 176


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

Go to the top of the page

alpha02
post 18 Aug, 2007 - 09:41 PM
Post #9


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
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

Go to the top of the page

Mastergeek666
post 26 Aug, 2007 - 05:24 PM
Post #10


D.I.C Head

Group Icon
Joined: 10 Aug, 2007
Posts: 120



Dream Kudos: 75
My Contributions


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

Go to the top of the page

alpha02
post 26 Aug, 2007 - 06:42 PM
Post #11


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
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 - 06:45 PM
User is offlineProfile CardPM

Go to the top of the page

Mastergeek666
post 26 Aug, 2007 - 07:18 PM
Post #12


D.I.C Head

Group Icon
Joined: 10 Aug, 2007
Posts: 120



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

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 7/20/08 03:39PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->