How to close open divs in post loop

You can simply do it by wrapping closing divs in if condition where current_post is equal to 2. I have added it in following code

<?php if ($query ->current_post == 0) { ?>
<div> Post # 1</div>
<?php } ?>
<?php if ($query ->current_post == 2) { ?>
<div class="extra">
    <div class ="extra2>
<?php } ?>  
<?php if ($query ->current_post >= 2) { ?>  
    <div> Post # 3</div>
    <div> Post # 4</div>
    <div> Post # 5</div>
 <?php } ?> 
<?php if ($query ->current_post == 2) { ?>
    </div>
</div>
<?php } ?>
<?php if ($query ->current_post == 1) { ?>
<div> Post # 2</div>
 <?php } ?>