Use a while loop
posts[$i]) {
$j = 0;
$post = $query->posts[$i++];
if ($images = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'))
){
while ($count < 5 && $images[$j]) {
$count++;
$image = $images[$j++];
..
}
}
}
?>