• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me clarify the for loop in the below code  RSS feed

 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ArrayNumberCheck
{
private static boolean checkInArray(int checkNum, int[] myArray)
{

boolean found = false;

for (int i = 0; !found && (i < myArray.length); i++) // how its behaving ?
{

found = (myArray[i] == checkNum);
}

return found;
}


public static void main(String[] args)
{
int[] arr = {10,5,15,20,25,200};
int checkNum = 2;

boolean valid = ArrayNumberCheck.checkInArray(checkNum,arr);

if (valid)
{

System.out.print("Value in Array");

}

else

{

System.out.print("Value Not In Array");

}


//System.out.print("Number exists in array");
}
}
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You should post code in code tags, like I've just done for you here.

What problem are you having?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, just saw the comment in the code.
Is it this part of the for loop?
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would appear to be part of the for loop. I presume the code prints not found? You should always tell us what the problem is; how can we answer if we don't know what the question is?
 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,that is the problem

inside the for loop how that is actually behaving??

!found what is the significance of this and how its substituting the 'If' statement
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The middle bit of a for loop is simply a boolean check, if true it continues looping.

So what does the statement say?


If that expression is true then the loop continues, otherwise it exits.
So 'found' has to be false and i has to be less than the length of the array.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
! is the negation operator and you simply read it as "not".

!found is read as "not found"

So if found is false, !found evaluates to the opposite, which is true
If found is true, !found will be false

You read the for statement condition as:
Continue iterating if not found and i is less than the length of myArray

Adding !found to the loop condition allows you to eliminate this if statement from inside the loop body:
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did that code come from? I am so used to seeing the ! operator that I thought you knew what it was. You can read about it in the Java™ Tutorials and the Java® Language Specification (=JLS). Beware; the JLS can be hard to read.
 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah but here the ! operator is used in the for loop for break which i was not sure of

so where can i get this kind of code any book or tutorial ???

Please give me the reference (any books,links or tutorial )as i am starting out on java it will be really helpful
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haraprasad mohapatra wrote:yeah but here the ! operator is used in the for loop for break which i was not sure of

so where can i get this kind of code any book or tutorial ???

Please give me the reference (any books,links or tutorial )as i am starting out on java it will be really helpful


You were already given links to read, just one post above the one I'm quoting.
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought Junilu had already explained that. By using !found (pronounced “not found”) in your loop heading, you have no need to “break” out of the loop.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haraprasad mohapatra wrote:Please give me the reference (any books,links or tutorial )as i am starting out on java it will be really helpful


Haraprasad, as a beginning programmer, it is understandable that you would be confused. However, you have to man up and learn to help yourself. Google is just a click away and it's actually easier to type "Java boolean operations" into Google search than it is to type "Please give me the reference (any books, blah blah blah" and post a message that, frankly, annoys the people here who have already given you a lot of tips. We try to be friendly and patient here but showing some effort instead of asking to be spoon fed also goes a long way in gaining respect and empathy.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!