It is the "Enhanced for loop".
It makes the looping easier for the collections and Arrays.
Here how it works;
ShoppingCartItem is a user defined class.
scItem is the reference of the object, which will be assigned while looping (the object will be retrieve from the collection "item")
items is the reference of the object, which is a Collection and type of ShoppingCartItem (mostly)
You can read the segment like this,
"for each object (ShoppingCartItem type)in the collection item, retrieve it and assign it to the scItem reference and do some work inside the loop body"
I hope it helps.
=-- Anything less than immortality is a complete waste of time --=
caleb momanyi wrote:thank you paul it helped a lot.
@Winstone that's not the whole of the code. I left out the rest of it because i was intrested with the explanation for this part only. But it works.
Don't want to offend you but Winston is right. the code snippet you wrote above is wrong. it won't compile. see for yourself in the if syntax. secondly you should follow forum rules and if somebody tell you to put code tags it is for your own good , so that your question can be answered quickly and clearly. code which is not properly formatted won't attract ranchers to answer it. so please in future, pay attention to these things.
you were right my friend, there's something wrong with the code in that whenever I add a new cart to a session and try to add a new product I still end up without a product in the cart.
following the flow of code in the debugger I realized that a new product variable is seen just befor the for loop but not added to cart. I don't know where the problem is
You should read the web app's logs, or if you haven't started using logging yet, read the System.out and / or System.err output files. You have a runtime error that occurs in the loop which only happens when there is a new item and prevents the loop from completing. The error will throw a runtime exception and, assuming you don't catch it yourself, would be caught and logged by your application server.
I won't tell you what the exception is, because learning to find and use your logs is key to making functional code for a web application.
And if all else fails, write a simple local test which does not use a web application but instead just makes a cart and adds some items to it to see what happens (that is the best part about using POJOs in web applications, you can test them without the web server).