Conditional loop example in JSTL

In this post we have seen how to loop through collection in JSTL. Here we will see how to use conditions while looping through JSTL. Consider we are printing a collection of book in JSP. Now let’s say that when we print the ?rst ten books, we want to highlight the ?rst ?ve. This is done by adding in a collaboration with the conditional action.

 <c:forEach var="book" items="${books}" varStatus="status" begin="0" end="9"> 
	<c:when test="${status.count < 4}"> 
	  <td bgcolor="#FFFF00"> <c:out value="${book.title}"/> </td> 
	  <td><c:out value="${book.title}"/></td>