Loop through Matrix Block fields within a Switch Case and retain HTML structure
You'll want to implement something like this:

{% for block in entry.menuBuilder %}

  {% set type = block.type.handle %}
  {% set isFirstConsecutiveBlockOfType = (loop.first or type != block.getPrev().type.handle) %}
  {% set isLastConsecutiveBlockOfType = (loop.last or type != block.getNext().type.handle) %}

  {% switch block.type %}
  {% case 'menuItem' %}

    {% if isFirstConsecutiveBlockOfType %}
      &lt;ul&gt;
    {% endif %}

      &lt;li&gt;
        &lt;h2&gt;{{ block.dishName }}&lt;/h2&gt;
        &lt;p&gt;{{ block.dishDescription }}&lt;/p&gt;
        &lt;p&gt;{{ block.dishPrice }}&lt;/p&gt;
      &lt;/li&gt;

    {% if isLastConsecutiveBlockOfType %}
      &lt;/ul&gt;
    {% endif %}

  {% case 'subHeading' %}

    &lt;header&gt;
      &lt;h2&gt;{{ block.heading }}&lt;/h2&gt;
    &lt;/header&gt;

  {% endswitch %}
{% endfor %}


Basically, for each block we're checking if it is the first or last item in the loop, or if that block's type doesn't match the previous or next block's type. This will let us know if the block we're dealing with is the first or the last in a series of menuItem blocks. Then in the switch/case we can use our new variables to conditionally include any wrapping html.