Update Sorry, I originally thought you were building nav and just showing simplified code. If you just want to list the categories then this should work:
{% nav category in categories %}
{{- category.title -}}
{% if not loop.last %}, {% endif %}
{% children %}
{% endnav %}
If your trying to build a nav system, then this should work. In either case just place your 'loop' before the children tag. See this answer for another example.
<ul id="nav">
{% nav category in categories %}
{% if loop.first %}
<li class="first">
{% elseif loop.last %}
<li class="last">
{% else %}
<li>
{% endif %}
<a href="{{ category.url }}">{{ category.title }}</a>
{% ifchildren %}
<ul>
{% children %}
</ul>
{% endifchildren %}
</li>
{% endnav %}
</ul>