This question is related to my previous question :
Jsp iterate trough object list
I want to insert counter that starts from 0 in my for loop, I've tried several combinations so far :
<c:forEach var="tableEntity" items='${requestScope.tables}'> <c:forEach var="rowEntity" items='${tableEntity.rows}' varStatus="count"> <c:out value="${count}" /> </c:forEach> </c:forEach>
<c:set var="count" value="0" scope="page" /> <c:forEach var="tableEntity" items='${requestScope.tables}'> <c:forEach var="rowEntity" items='${tableEntity.rows}'> <%=count++%> <c:out value="${count}" /> </c:forEach> </c:forEach>
Problem with first approach is that outer loop has 3 items and inner loop has 7 items, so for each outer item the count starts from 0. The second one I get compile error. Here is basically what I want :
counter = 0; outer for loop inner for loop counter++; //cout/echo/print counter value should start from 0 end inner loop end outer loop
I'm just not totally familiar with the syntax. thank you
Increment variable in loop python In python, to increment a variable value in a loop, we can use the while loop directly for increasing or decreasing the iteration value. After writing the above code (increment variable in loop python), Ones you will print “my_list[i]” then the output will appear as an “ 11 13 15 ”.
A for loop doesn't increment anything. Your code used in the for statement does. It's entirely up to you how/if/where/when you want to modify i or any other variable for that matter.
The most simple way to increment/decrement a variable is by using the + and - operators. This method allows you increment/decrement the variable by any value you want.
A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus: for(int i = 0; i != 5; ++i,++j) do_something(i,j);
Try the following:
<c:set var="count" value="0" scope="page" /> //in your loops <c:set var="count" value="${count + 1}" scope="page"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With