Online Course Discussion Forum

AP CSA Break/For/WHile loop return

Picture of Dr. Kevin Wang
Re: AP CSA Break/For/WHile loop return
by Dr. Kevin Wang - Tuesday, August 16, 2022, 12:16 AM

Selina, did you put the code into an actual Java file and run it?  It is the sure way to test what you are asking.  Please get into the habit of testing with the real programming, instead of just looking at the code.

Having "return" inside a loop is perfectly okay.  In fact it was discussed in one of our lectures.  Look at the slides of the corresponding lesson.  The way you described above is good in terms of the algorithm, but the syntax of what you wrote is not correct Java syntax.

Also, if you use an extra variable to indicate whether it is time to get out of the loop and return, it is perfectly fine too.  There is no such thing as "excessive use of data" error.  The AP reader will also grade it as correct, given that you don't have error elsewhere.