Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38225
| Path | csiph.com!aioe.org!.POSTED!not-for-mail |
|---|---|
| From | Arne Vajhøj <arne@vajhoej.dk> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Exit from loop? |
| Date | Wed, 4 Apr 2018 15:25:52 -0400 |
| Organization | Aioe.org NNTP Server |
| Lines | 50 |
| Message-ID | <pa38s0$35o$1@gioia.aioe.org> (permalink) |
| References | <6f19094e-c652-44e0-8a7d-fb3b7791c372@googlegroups.com> |
| NNTP-Posting-Host | PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Complaints-To | abuse@aioe.org |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 |
| X-Notice | Filtered by postfilter v. 0.8.3 |
| Content-Language | en-US |
| Xref | csiph.com comp.lang.java.programmer:38225 |
Show key headers only | View raw
On 4/4/2018 3:05 PM, Eric Douglas wrote:
> Does it cause any problems to exit from loop, or is it considered bad form?
>
> ie can you do this?
>
>
> public static int randomMethod() {
> int i = 0;
> int tries = 0;
> for (int ctr = 0; ctr < 100; ctr++) {
> i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
> tries++;
> if (i == 3) {
> return tries;
> }
> }
> return tries;
> }
>
> or should you do this?
>
> public static int randomMethod() {
> int i = 0;
> int tries = 0;
> for (int ctr = 0; ctr < 100; ctr++) {
> i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
> tries++;
> if (i == 3) {
> break;
> }
> }
> return tries;
> }
>
> This is of course a simple sample, where the value getting assigned in the loop, and/or the value returned from the method are normally complex objects.
Between those two I would probably do the last, but
I see no big problems with the first.
If I had the choice I would probably use a do while loop.
int ctr = 0;
X o = new X();
do {
o.something();
ctr++;
} while(ctr < 100 && ThreadLocalRandom.current().nextInt(1, 10) != 3);
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Exit from loop? Eric Douglas <e.d.programmer@gmail.com> - 2018-04-04 12:05 -0700
Re: Exit from loop? Arne Vajhøj <arne@vajhoej.dk> - 2018-04-04 15:25 -0400
Re: Exit from loop? Eric Douglas <e.d.programmer@gmail.com> - 2018-04-04 12:44 -0700
Re: Exit from loop? Arne Vajhøj <arne@vajhoej.dk> - 2018-04-04 19:51 -0400
Re: Exit from loop? Martin Gregorie <martin@mydomain.invalid> - 2018-04-05 10:46 +0000
Re: Exit from loop? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-04-06 18:19 +0200
csiph-web