Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

if loop inside for loop problem  RSS feed

 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3997
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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
Fedora Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Fedora Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try like

>
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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
Fedora Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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: 55678
161
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!