-1

Can't seem to figure out where it's not working. In the center I need a line wiht only ++++++. But I can't seem to find it to work. I've got it to work with a while-loop and a for loop but not the do while loop.

It's gotta look like this

          -------+
          ------++
          -----+++
          ----++++
          ---+++++
          --++++++
          -+++++++
          ++++++++
          -+++++++

etc. But the middle line is not a a full line of + characters.

     <?php
        echo"<p>Do While loop</p>";
        $x1=8;
        $x2=1;
        do {
           $x3=1;
           do {
              echo"--";
              $x3++;
           }while($x3<$x1);
           $x4=1;
           do {
              echo"+";
              $x4++;
           }while($x4<=$x2);
           echo"<br>";
           $x1--;
           $x2++;
       }while($x1>=0 and $x2<=8);

       $y1=1;
       $y2=7;
       do {
          $y3=1;
          do {
             echo"--";
             $y3++;
          }while($y3<=$y1);
       $y4=1;
       do {
          echo"+";
          $y4++;
       }while($y4<=$y2);
       echo"<br>";
       $y1++;
       $y2--;
       }while($y1<=7 and $y2>=1);

   ?>
Deedss
  • 21
  • 3
  • you are in deep do do's [chuckles]. in all seriousness, you say you have an answer to your problem (actually 2), but you're trying to use a construct that is rarely used (in my experience) - i'd stick with what 1) works; 2) is easiest for another developer to understand; 3) runs the quickest... – Wee Zel Sep 22 '17 at 19:31
  • No one is going to debug this for you. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Sep 22 '17 at 21:30

2 Answers2

0

It will never happen.

do { /*Line 4*/
    echo"--";
    $x3++;
}while($x3<$x1);

The echo will be evaluated before the break question.

This is the basic difference between do while, and while loop. In while loop, the condition is evaluated before anything else. In the do while loop, everything else is evaluated before the first check of the condition.

Paulo Lima
  • 65
  • 1
  • 6
0

well I figured it out in the end. By putting an if($x1<=1){break;} after the first internal do loop i'll force the loop to skip and proceed on from there.

Deedss
  • 21
  • 3