• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

if loop inside for loop problem

 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




This produces the output as follows



I want to restrict this code with the correct output
Please help me
Thanks in advance
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: Using a break; statement in a loop causes the loop (inner most loop if you have more than one) to terminate and jump out of it.
 
Rudradutt Joshi
Ranch Hand
Posts: 45
Google Web Toolkit Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:Hint: Using a break; statement in a loop causes the loop (inner most loop if you have more than one) to terminate and jump out of it.



Also labels are available in java to tell JVM to get out of which loop.
 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I tried giving break


In the above case if i want to check if lion exists it prints

In this case i guess break will not help.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most importantly here, an if statement or if block is not a loop.
 
Suresh Sajja
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can move "If.. else.." block out of "for" loop.
break the "for" loop when your condition is met.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I'm just dense, but I don't see why you're printing something in the else {...} block if you don't want it to be printed.
 
Rudradutt Joshi
Ranch Hand
Posts: 45
Google Web Toolkit Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try like

>
 
Campbell Ritchie
Marshal
Posts: 77157
370
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rudradutt Joshi wrote:Why don't you try like

>

. . . because there is only one thing worse than break; and that is labelled break;.

I know a lot of people will disagree with me here. You have already been given correct solutions, by Darryl Burke and Suresh Sajja.

If you want to do something in that loop, you need a flag, and this is how you can do a linear searchRemember you can have problems if you pass null or the array contains a null There are ways round that problem.
 
Rudradutt Joshi
Ranch Hand
Posts: 45
Google Web Toolkit Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A cleanly written code is as much as readable as it is with or without a break,continue or equivalent.

But I like the way Campbell has given and I would try using that one now onwards.
 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
Thanks a lot Ritchie for letting me know a nice solution.

I also thank others for helping me with this doubt.
 
fred rosenberger
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be a pain...

There is no such thing as an "if loop". It is an if condition or if statement.
 
Sean Clark
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

That is a really nice clean solution and something that is really simple but which is so easily overlooked, thanks for the example and +1

Sean
 
Campbell Ritchie
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

Have you worked out what to do about nulls yet?

And of course you can use an int instead of the booleanThat allows you to return the index, which is how linear searches usually work. You get index if found and -1 otherwise
 
reply
    Bookmark Topic Watch Topic
  • New Topic