Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single entry, single exit.

 
Kyle Ross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My teacher wants us to enforce having one entry and one exit in a program. What do I put on that line to have the loop restart? Because you want to continue the program, but you didn't supply a name last time, so it can't drop down to the Output box. break; only exits the program.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You can either just put nothing there (at line 18), then line 22 will be executed and at line 24 it will jump back to line 7 for the next iteration of the loop.

You can also put continue; at line 18; then the program will immediately jump to line 7 for the next iteration (skipping the rest of the loop body, so line 22 will not be executed).

Note: The continueLoop variable in your code isn't doing anything useful. Note that the break; in line 15 jumps out of the loop, no matter what the value of continueLoop is.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been specifically told that you have to put something on line 18 ? Because the way I'd do it is to put an else on line 21 (so that it didn't print Hello null) and then delete lines 15 - 18.

I'd also change line 12 to
if(usrOption == JOptionPane.YES_OPTION)
as it makes the code a lot clearer and you then no longer need the comment.
 
Kyle Ross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
You can also put continue; at line 18; then the program will immediately jump to line 7 for the next iteration (skipping the rest of the loop body, so line 22 will not be executed).


Thank you, i have been looking for a while something that restarts a loop.

Note: The continueLoop variable in your code isn't doing anything useful. Note that the break; in line 15 jumps out of the loop, no matter what the value of continueLoop is.

But the break isn't hit until you say you want to exit the application. without the break and the continueLoop this wouldn't work.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Ross wrote:But the break isn't hit until you say you want to exit the application. without the break and the continueLoop this wouldn't work.

I didn't say "without the break". But the continueLoop variable is unnecessary. When you do a "break", the loop exits. The loop condition (what's in the brackets after the "while") is not checked.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Ross wrote:My teacher wants us to enforce having one entry and one exit in a program...

Kyle,

Please DontWriteLongLines (←click). I've broken them up this time, but please read the link and follow its advice.

Thanks.

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Ross wrote:Thank you, i have been looking for a while something that restarts a loop.

My advice: Don't.

Look to have your loop finish when (and ONLY when) it's supposed to.

BTW: There is a construct that us old-fashioned bods use that you may find useful:
Winston
 
Daryle Foulke
Greenhorn
Posts: 8
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also don't think this line is what your looking for



I think you want this. usrName is not going to be null if someone enter or the OK button with no name entered.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic